# $SnapHashLicense:
#
# SnapLogic - Open source data services
#
# Copyright (C) 2008, SnapLogic, Inc. All rights reserved.
#
# See http://www.snaplogic.org for more information about
# the SnapLogic project.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
#
# "SnapLogic" is a trademark of SnapLogic, Inc.
#
#
# $
# $Id: RssElem.py 831 2008-01-09 23:22:39Z dhiraj $
"""
Module for RSS Element object.
This module contains the RssElem object as the base class for Feed and Item objects. There are a couple of
common interfaces in addtion to the fact thet both Feed and Item objects are using same data structure for
object attributes to justify this base class.
The RssElem object shields the underlying 3rdparty feedparser object from the users, so that the user code does
not tightly depend on the 3rdparty package, which could be replaced in the future.
"""
# Imports
from feedparser import FeedParserDict
class RssElem:
"""
This class provides intefaces for the users to access information of a RSS basic element. It is the base class
for RssFeed and RssItem classes.
"""
def __init__(self):
"""
Initialization.
"""
self._elem = FeedParserDict()
def __str__(self):
"""
Print the item contents.
"""
r = list()
r.append("==== RssElem ====")
r.append("title: " + repr(self._elem.get('title', '')))
for k in self._elem.keys():
if k != 'title':
r.append(k + ": " + str(self._elem[k]))
r.append("==================")
return '\n'.join(r)
def getAttribute(self, attr):
"""
Get specified attribute value of the element.
@param attr: The attribute name.
@type attr: string
@return: The attribute value; None, if not found.
"""
return self._elem.get(attr, None)
def setAttributes(self, attrs):
"""
Set attributes to this element.
@param attrs: The attributes being set.
@type attrs: dict
"""
for k, v in attrs.iteritems():
# A few attributes are list of values
if k == 'links':
if not self._elem.has_key('links'):
self._elem['links'] = []
for a in v:
link = FeedParserDict()
for m, n in a.iteritems():
if n != None: link[m] = n
self._elem.links.append(link)
elif k == 'contributors':
if not self._elem.has_key('contributors'):
self._elem['contributors'] = []
for a in v:
contributor = FeedParserDict()
for m, n in a.iteritems():
if n != None: contributor[m] = n
self._elem.contributors.append(contributor)
elif k == 'content':
if not self._elem.has_key('content'):
self._elem['content'] = []
for a in v:
content = FeedParserDict()
for m, n in a.iteritems():
if n != None: content[m] = n
self._elem.content.append(content)
elif k == 'tags':
if not self._elem.has_key('tags'):
self._elem['tags'] = []
for a in v:
tag = FeedParserDict()
for m, n in a.iteritems():
if n != None: tag[m] = n
self._elem.tags.append(tag)
elif k == 'namespaces':
if not self._elem.has_key('namespaces'):
self._elem['namespaces'] = {}
for p, u in v.iteritems():
if u != None: self._elem.namespaces[p] = u
elif v != None:
self._elem[k] = v
|