Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/snipem/python-feedgen int…
Browse files Browse the repository at this point in the history
…o merge-test
  • Loading branch information
lkiesow committed May 11, 2014
2 parents bf6b8af + 966b7f2 commit a787b22
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
36 changes: 22 additions & 14 deletions feedgen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@
from feedgen.feed import FeedGenerator
import sys

def print_enc(string):
version = sys.version_info[0]

if version == 2:
print(string)
elif version == 3:
sys.stdout.buffer.write(string)



if __name__ == '__main__':
if len(sys.argv) != 2 or not (
sys.argv[1].endswith('rss') \
or sys.argv[1].endswith('atom') \
or sys.argv[1].endswith('podcast') ):
print 'Usage: %s ( <file>.atom | atom | <file>.rss | rss | podcast )' % \
'pythom -m feedgen'
print ''
print ' atom -- Generate ATOM test output and print it to stdout.'
print ' rss -- Generate RSS test output and print it to stdout.'
print ' <file>.atom -- Generate ATOM test feed and write it to file.atom.'
print ' <file>.rss -- Generate RSS test teed and write it to file.rss.'
print ' podcast -- Generator Podcast test output and print it to stdout.'
print ''
print_enc ('Usage: %s ( <file>.atom | atom | <file>.rss | rss | podcast )' % \
'pythom -m feedgen')
print_enc ('')
print_enc (' atom -- Generate ATOM test output and print it to stdout.')
print_enc (' rss -- Generate RSS test output and print it to stdout.')
print_enc (' <file>.atom -- Generate ATOM test feed and write it to file.atom.')
print_enc (' <file>.rss -- Generate RSS test teed and write it to file.rss.')
print_enc (' podcast -- Generator Podcast test output and print it to stdout.')
print_enc ('')
exit()

arg = sys.argv[1]
Expand Down Expand Up @@ -59,9 +67,9 @@
fe.author( name='Lars Kiesow', email='[email protected]' )

if arg == 'atom':
print fg.atom_str(pretty=True)
print_enc (fg.atom_str(pretty=True))
elif arg == 'rss':
print fg.rss_str(pretty=True)
print_enc (fg.rss_str(pretty=True))
elif arg == 'podcast':
# Load the podcast extension. It will automatically be loaded for all
# entries in the feed, too. Thus also for our “fe”.
Expand All @@ -76,18 +84,18 @@
'consectetur adipiscing elit. ' + \
'Verba tu fingas et ea dicas, quae non sentias?')
fe.podcast.itunes_author('Lars Kiesow')
print fg.rss_str(pretty=True)
print_enc (fg.rss_str(pretty=True))

elif arg == 'dc.atom':
fg.load_extension('dc')
fg.dc.dc_contributor('Lars Kiesow')
fe.dc.dc_contributor('Lars Kiesow')
print fg.atom_str(pretty=True)
print_enc (fg.atom_str(pretty=True))

elif arg == 'dc.rss':
fg.load_extension('dc')
fg.dc.dc_contributor('Lars Kiesow')
print fg.rss_str(pretty=True)
print_enc (fg.rss_str(pretty=True))

elif arg.endswith('atom'):
fg.atom_file(arg)
Expand Down
18 changes: 16 additions & 2 deletions feedgen/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from feedgen.entry import FeedEntry
from feedgen.util import ensure_format
import feedgen.version
import sys


_feedgen_version = feedgen.version.version_str
Expand Down Expand Up @@ -977,8 +978,15 @@ def add_entry(self, feedEntry=None):
if feedEntry is None:
feedEntry = FeedEntry()

version = sys.version_info[0]

if version == 2:
items = self.__extensions.iteritems()
else:
items = self.__extensions.items()

# Try to load extensions:
for extname,ext in self.__extensions.iteritems():
for extname,ext in items:
try:
feedEntry.load_extension( extname, ext['atom'], ext['rss'] )
except ImportError:
Expand Down Expand Up @@ -1011,10 +1019,16 @@ def entry(self, entry=None, replace=False):
if replace:
self.__feed_entries = []

version = sys.version_info[0]

if version == 2:
items = self.__extensions.iteritems()
else:
items = self.__extensions.items()

# Try to load extensions:
for e in entry:
for extname,ext in self.__extensions.iteritems():
for extname,ext in items:
try:
e.load_extension( extname, ext['atom'], ext['rss'] )
except ImportError:
Expand Down
20 changes: 17 additions & 3 deletions feedgen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:copyright: 2013, Lars Kiesow <[email protected]>
:license: FreeBSD and LGPL, see license.* for more details.
'''

import sys


def ensure_format(val, allowed, required, allowed_values=None, defaults=None):
Expand Down Expand Up @@ -36,13 +36,27 @@ def ensure_format(val, allowed, required, allowed_values=None, defaults=None):
if not isinstance(elem, dict):
raise ValueError('Invalid data (value is no dictionary)')
# Set default values
for k,v in defaults.iteritems():

version = sys.version_info[0]

if version == 2:
items = defaults.iteritems()
else:
items = defaults.items()

for k,v in items:
elem[k] = elem.get(k, v)
if not set(elem.keys()) <= allowed:
raise ValueError('Data contains invalid keys')
if not set(elem.keys()) >= required:
raise ValueError('Data contains not all required keys')
for k,v in allowed_values.iteritems():

if version == 2:
values = allowed_values.iteritems()
else:
values = allowed_values.items()

for k,v in values:
if elem.get(k) and not elem[k] in v:
raise ValueError('Invalid value for %s' % k )
return val

0 comments on commit a787b22

Please sign in to comment.