RssElem.py :  » Development » SnapLogic » snaplogic » common » Rss » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SnapLogic 
SnapLogic » snaplogic » common » Rss » RssElem.py
# $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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.