Allow iteration over RSS objects

Useful for JSON
master
pictuga 2013-12-22 12:44:33 +01:00
parent d97a72bc0b
commit 0badec72a9
1 changed files with 23 additions and 0 deletions

View File

@ -76,6 +76,27 @@ class FeedBase(object):
Base for xml-related classes, which provides simple wrappers around xpath Base for xml-related classes, which provides simple wrappers around xpath
selection and item creation selection and item creation
""" """
def __getitem__(self, item):
return getattr(self, item)
def __setitem__(self, item, value):
setattr(self, item, value)
def __delitem__(self, item):
delattr(self, item)
def __iter__(self):
for element in self.dic:
value = self[element]
if isinstance(value, FeedList):
value = [dict(x) for x in value]
elif isinstance(value, datetime):
value = value.isoformat()
yield element, value
def xpath(self, path): def xpath(self, path):
""" Test xpath rule on xml tree """ """ Test xpath rule on xml tree """
return self.root.xpath(path, namespaces=NSMAP) return self.root.xpath(path, namespaces=NSMAP)
@ -270,6 +291,7 @@ class FeedParser(FeedBase):
itemsClass = 'FeedItem' itemsClass = 'FeedItem'
mimetype = 'application/xml' mimetype = 'application/xml'
base = '<?xml?>' base = '<?xml?>'
dic = ('title', 'desc', 'items')
def __init__(self, xml=None, tag='atom:feed'): def __init__(self, xml=None, tag='atom:feed'):
if xml is None: if xml is None:
@ -387,6 +409,7 @@ class FeedParserAtom(FeedParser):
class FeedItem(FeedBase): class FeedItem(FeedBase):
timeFormat = '' timeFormat = ''
dic = ('title', 'link', 'isPermaLink', 'desc', 'content', 'id', 'time', 'updated')
def __init__(self, xml=None, tag='atom:feed'): def __init__(self, xml=None, tag='atom:feed'):
if xml is None: if xml is None: