<s> """<STR_LIT>""" <EOL> import datetime <EOL> import urlparse <EOL> from airy . utils . xmlutils import SimplerXMLGenerator <EOL> from airy . utils . encoding import force_unicode , iri_to_uri <EOL> from airy . utils import datetime_safe <EOL> def rfc2822_date ( date ) : <EOL> months = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> days = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> date = datetime_safe . new_datetime ( date ) <EOL> dow = days [ date . weekday ( ) ] <EOL> month = months [ date . month - <NUM_LIT:1> ] <EOL> time_str = date . strftime ( '<STR_LIT>' % ( dow , month ) ) <EOL> if date . tzinfo : <EOL> offset = date . tzinfo . utcoffset ( date ) <EOL> timezone = ( offset . days * <NUM_LIT> * <NUM_LIT> ) + ( offset . seconds / <NUM_LIT> ) <EOL> hour , minute = divmod ( timezone , <NUM_LIT> ) <EOL> return time_str + "<STR_LIT>" % ( hour , minute ) <EOL> else : <EOL> return time_str + '<STR_LIT>' <EOL> def rfc3339_date ( date ) : <EOL> date = datetime_safe . new_datetime ( date ) <EOL> if date . tzinfo : <EOL> time_str = date . strftime ( '<STR_LIT>' ) <EOL> offset = date . tzinfo . utcoffset ( date ) <EOL> timezone = ( offset . days * <NUM_LIT> * <NUM_LIT> ) + ( offset . seconds / <NUM_LIT> ) <EOL> hour , minute = divmod ( timezone , <NUM_LIT> ) <EOL> return time_str + "<STR_LIT>" % ( hour , minute ) <EOL> else : <EOL> return date . strftime ( '<STR_LIT>' ) <EOL> def get_tag_uri ( url , date ) : <EOL> """<STR_LIT>""" <EOL> url_split = urlparse . urlparse ( url ) <EOL> hostname = getattr ( url_split , '<STR_LIT>' , url_split [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] ) <EOL> path = url_split [ <NUM_LIT:2> ] <EOL> fragment = url_split [ <NUM_LIT:5> ] <EOL> d = '<STR_LIT>' <EOL> if date is not None : <EOL> d = '<STR_LIT>' % datetime_safe . new_datetime ( date ) . strftime ( '<STR_LIT>' ) <EOL> return u'<STR_LIT>' % ( hostname , d , path , fragment ) <EOL> class SyndicationFeed ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , title , link , description , language = None , author_email = None , <EOL> author_name = None , author_link = None , subtitle = None , categories = None , <EOL> feed_url = None , feed_copyright = None , feed_guid = None , ttl = None , ** kwargs ) : <EOL> to_unicode = lambda s : force_unicode ( s , strings_only = True ) <EOL> if categories : <EOL> categories = [ force_unicode ( c ) for c in categories ] <EOL> if ttl is not None : <EOL> ttl = force_unicode ( ttl ) <EOL> self . feed = { <EOL> '<STR_LIT:title>' : to_unicode ( title ) , <EOL> '<STR_LIT>' : iri_to_uri ( link ) , <EOL> '<STR_LIT:description>' : to_unicode ( description ) , <EOL> '<STR_LIT>' : to_unicode ( language ) , <EOL> '<STR_LIT>' : to_unicode ( author_email ) , <EOL> '<STR_LIT>' : to_unicode ( author_name ) , <EOL> '<STR_LIT>' : iri_to_uri ( author_link ) , <EOL> '<STR_LIT>' : to_unicode ( subtitle ) , <EOL> '<STR_LIT>' : categories or ( ) , <EOL> '<STR_LIT>' : iri_to_uri ( feed_url ) , <EOL> '<STR_LIT>' : to_unicode ( feed_copyright ) , <EOL> '<STR_LIT:id>' : feed_guid or link , <EOL> '<STR_LIT>' : ttl , <EOL> } <EOL> self . feed . update ( kwargs ) <EOL> self . items = [ ] <EOL> def add_item ( self , title , link , description , author_email = None , <EOL> author_name = None , author_link = None , pubdate = None , comments = None , <EOL> unique_id = None , enclosure = None , categories = ( ) , item_copyright = None , <EOL> ttl = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> to_unicode = lambda s : force_unicode ( s , strings_only = True ) <EOL> if categories : <EOL> categories = [ to_unicode ( c ) for c in categories ] <EOL> if ttl is not None : <EOL> ttl = force_unicode ( ttl ) <EOL> item = { <EOL> '<STR_LIT:title>' : to_unicode ( title ) , <EOL> '<STR_LIT>' : iri_to_uri ( link ) , <EOL> '<STR_LIT:description>' : to_unicode ( description ) , <EOL> '<STR_LIT>' : to_unicode ( author_email ) , <EOL> '<STR_LIT>' : to_unicode ( author_name ) , <EOL> '<STR_LIT>' : iri_to_uri ( author_link ) , <EOL> '<STR_LIT>' : pubdate , <EOL> '<STR_LIT>' : to_unicode ( comments ) , <EOL> '<STR_LIT>' : to_unicode ( unique_id ) , <EOL> '<STR_LIT>' : enclosure , <EOL> '<STR_LIT>' : categories or ( ) , <EOL> '<STR_LIT>' : to_unicode ( item_copyright ) , <EOL> '<STR_LIT>' : ttl , <EOL> } <EOL> item . update ( kwargs ) <EOL> self . items . append ( item ) <EOL> def num_items ( self ) : <EOL> return len ( self . items ) <EOL> def root_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def add_root_elements ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def item_attributes ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def add_item_elements ( self , handler , item ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def write ( self , outfile , encoding ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def writeString ( self , encoding ) : <EOL> """<STR_LIT>""" <EOL> from StringIO import StringIO <EOL> s = StringIO ( ) <EOL> self . write ( s , encoding ) <EOL> return s . getvalue ( ) <EOL> def latest_post_date ( self ) : <EOL> """<STR_LIT>""" <EOL> updates = [ i [ '<STR_LIT>' ] for i in self . items if i [ '<STR_LIT>' ] is not None ] <EOL> if len ( updates ) > <NUM_LIT:0> : <EOL> updates . sort ( ) <EOL> return updates [ - <NUM_LIT:1> ] <EOL> else : <EOL> return datetime . datetime . now ( ) <EOL> class Enclosure ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , url , length , mime_type ) : <EOL> "<STR_LIT>" <EOL> self . length , self . mime_type = length , mime_type <EOL> self . url = iri_to_uri ( url ) <EOL> class RssFeed ( SyndicationFeed ) : <EOL> mime_type = '<STR_LIT>' <EOL> def write ( self , outfile , encoding ) : <EOL> handler = SimplerXMLGenerator ( outfile , encoding ) <EOL> handler . startDocument ( ) <EOL> handler . startElement ( u"<STR_LIT>" , self . rss_attributes ( ) ) <EOL> handler . startElement ( u"<STR_LIT>" , self . root_attributes ( ) ) <EOL> self . add_root_elements ( handler ) <EOL> self . write_items ( handler ) <EOL> self . endChannelElement ( handler ) <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> def rss_attributes ( self ) : <EOL> return { u"<STR_LIT:version>" : self . _version , <EOL> u"<STR_LIT>" : u"<STR_LIT>" } <EOL> def write_items ( self , handler ) : <EOL> for item in self . items : <EOL> handler . startElement ( u'<STR_LIT>' , self . item_attributes ( item ) ) <EOL> self . add_item_elements ( handler , item ) <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> def add_root_elements ( self , handler ) : <EOL> handler . addQuickElement ( u"<STR_LIT:title>" , self . feed [ '<STR_LIT:title>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT:description>" , self . feed [ '<STR_LIT:description>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , None , { u"<STR_LIT>" : u"<STR_LIT>" , u"<STR_LIT>" : self . feed [ '<STR_LIT>' ] } ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> for cat in self . feed [ '<STR_LIT>' ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , cat ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , rfc2822_date ( self . latest_post_date ( ) ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> def endChannelElement ( self , handler ) : <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> class RssUserland091Feed ( RssFeed ) : <EOL> _version = u"<STR_LIT>" <EOL> def add_item_elements ( self , handler , item ) : <EOL> handler . addQuickElement ( u"<STR_LIT:title>" , item [ '<STR_LIT:title>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT:description>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT:description>" , item [ '<STR_LIT:description>' ] ) <EOL> class Rss201rev2Feed ( RssFeed ) : <EOL> _version = u"<STR_LIT>" <EOL> def add_item_elements ( self , handler , item ) : <EOL> handler . addQuickElement ( u"<STR_LIT:title>" , item [ '<STR_LIT:title>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT:description>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT:description>" , item [ '<STR_LIT:description>' ] ) <EOL> if item [ "<STR_LIT>" ] and item [ "<STR_LIT>" ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , "<STR_LIT>" % ( item [ '<STR_LIT>' ] , item [ '<STR_LIT>' ] ) ) <EOL> elif item [ "<STR_LIT>" ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ "<STR_LIT>" ] ) <EOL> elif item [ "<STR_LIT>" ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ "<STR_LIT>" ] , { u"<STR_LIT>" : u"<STR_LIT>" } ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , rfc2822_date ( item [ '<STR_LIT>' ] ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , '<STR_LIT>' , <EOL> { u"<STR_LIT:url>" : item [ '<STR_LIT>' ] . url , u"<STR_LIT>" : item [ '<STR_LIT>' ] . length , <EOL> u"<STR_LIT:type>" : item [ '<STR_LIT>' ] . mime_type } ) <EOL> for cat in item [ '<STR_LIT>' ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , cat ) <EOL> class Atom1Feed ( SyndicationFeed ) : <EOL> mime_type = '<STR_LIT>' <EOL> ns = u"<STR_LIT>" <EOL> def write ( self , outfile , encoding ) : <EOL> handler = SimplerXMLGenerator ( outfile , encoding ) <EOL> handler . startDocument ( ) <EOL> handler . startElement ( u'<STR_LIT>' , self . root_attributes ( ) ) <EOL> self . add_root_elements ( handler ) <EOL> self . write_items ( handler ) <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> def root_attributes ( self ) : <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> return { u"<STR_LIT>" : self . ns , u"<STR_LIT>" : self . feed [ '<STR_LIT>' ] } <EOL> else : <EOL> return { u"<STR_LIT>" : self . ns } <EOL> def add_root_elements ( self , handler ) : <EOL> handler . addQuickElement ( u"<STR_LIT:title>" , self . feed [ '<STR_LIT:title>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , "<STR_LIT>" , { u"<STR_LIT>" : u"<STR_LIT>" , u"<STR_LIT>" : self . feed [ '<STR_LIT>' ] } ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , "<STR_LIT>" , { u"<STR_LIT>" : u"<STR_LIT>" , u"<STR_LIT>" : self . feed [ '<STR_LIT>' ] } ) <EOL> handler . addQuickElement ( u"<STR_LIT:id>" , self . feed [ '<STR_LIT:id>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , rfc3339_date ( self . latest_post_date ( ) ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . startElement ( u"<STR_LIT>" , { } ) <EOL> handler . addQuickElement ( u"<STR_LIT:name>" , self . feed [ '<STR_LIT>' ] ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT:email>" , self . feed [ '<STR_LIT>' ] ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> for cat in self . feed [ '<STR_LIT>' ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , "<STR_LIT>" , { u"<STR_LIT>" : cat } ) <EOL> if self . feed [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , self . feed [ '<STR_LIT>' ] ) <EOL> def write_items ( self , handler ) : <EOL> for item in self . items : <EOL> handler . startElement ( u"<STR_LIT>" , self . item_attributes ( item ) ) <EOL> self . add_item_elements ( handler , item ) <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> def add_item_elements ( self , handler , item ) : <EOL> handler . addQuickElement ( u"<STR_LIT:title>" , item [ '<STR_LIT:title>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT>" , u"<STR_LIT>" , { u"<STR_LIT>" : item [ '<STR_LIT>' ] , u"<STR_LIT>" : u"<STR_LIT>" } ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , rfc3339_date ( item [ '<STR_LIT>' ] ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . startElement ( u"<STR_LIT>" , { } ) <EOL> handler . addQuickElement ( u"<STR_LIT:name>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT:email>" , item [ '<STR_LIT>' ] ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> handler . endElement ( u"<STR_LIT>" ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> unique_id = item [ '<STR_LIT>' ] <EOL> else : <EOL> unique_id = get_tag_uri ( item [ '<STR_LIT>' ] , item [ '<STR_LIT>' ] ) <EOL> handler . addQuickElement ( u"<STR_LIT:id>" , unique_id ) <EOL> if item [ '<STR_LIT:description>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT:description>' ] , { u"<STR_LIT:type>" : u"<STR_LIT:html>" } ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , '<STR_LIT>' , <EOL> { u"<STR_LIT>" : u"<STR_LIT>" , <EOL> u"<STR_LIT>" : item [ '<STR_LIT>' ] . url , <EOL> u"<STR_LIT>" : item [ '<STR_LIT>' ] . length , <EOL> u"<STR_LIT:type>" : item [ '<STR_LIT>' ] . mime_type } ) <EOL> for cat in item [ '<STR_LIT>' ] : <EOL> handler . addQuickElement ( u"<STR_LIT>" , u"<STR_LIT>" , { u"<STR_LIT>" : cat } ) <EOL> if item [ '<STR_LIT>' ] is not None : <EOL> handler . addQuickElement ( u"<STR_LIT>" , item [ '<STR_LIT>' ] ) <EOL> DefaultFeed = Rss201rev2Feed </s>
