home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1422 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  13.3 KB  |  423 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __name__ = 'PyRSS2Gen'
  5. __version__ = (1, 0, 0)
  6. __author__ = 'Andrew Dalke <dalke@dalkescientific.com>'
  7. _generator_name = __name__ + '-' + '.'.join(map(str, __version__))
  8. import datetime
  9.  
  10. class WriteXmlMixin:
  11.     
  12.     def write_xml(self, outfile, encoding = 'iso-8859-1'):
  13.         saxutils = saxutils
  14.         import xml.sax
  15.         handler = saxutils.XMLGenerator(outfile, encoding)
  16.         handler.startDocument()
  17.         self.publish(handler)
  18.         handler.endDocument()
  19.  
  20.     
  21.     def to_xml(self, encoding = 'iso-8859-1'):
  22.         
  23.         try:
  24.             import cStringIO as StringIO
  25.             StringIO
  26.         except ImportError:
  27.             import StringIO
  28.  
  29.         f = StringIO.StringIO()
  30.         self.write_xml(f, encoding)
  31.         return f.getvalue()
  32.  
  33.  
  34.  
  35. def _element(handler, name, obj, d = { }):
  36.     if isinstance(obj, basestring) or obj is None:
  37.         handler.startElement(name, d)
  38.         if obj is not None:
  39.             handler.characters(obj)
  40.         
  41.         handler.endElement(name)
  42.     else:
  43.         obj.publish(handler)
  44.  
  45.  
  46. def _opt_element(handler, name, obj):
  47.     if obj is None:
  48.         return None
  49.     _element(handler, name, obj)
  50.  
  51.  
  52. def _format_date(dt):
  53.     return '%s, %02d %s %04d %02d:%02d:%02d GMT' % ([
  54.         'Mon',
  55.         'Tue',
  56.         'Wed',
  57.         'Thu',
  58.         'Fri',
  59.         'Sat',
  60.         'Sun'][dt.weekday()], dt.day, [
  61.         'Jan',
  62.         'Feb',
  63.         'Mar',
  64.         'Apr',
  65.         'May',
  66.         'Jun',
  67.         'Jul',
  68.         'Aug',
  69.         'Sep',
  70.         'Oct',
  71.         'Nov',
  72.         'Dec'][dt.month - 1], dt.year, dt.hour, dt.minute, dt.second)
  73.  
  74.  
  75. class IntElement:
  76.     element_attrs = { }
  77.     
  78.     def __init__(self, name, val):
  79.         self.name = name
  80.         self.val = val
  81.  
  82.     
  83.     def publish(self, handler):
  84.         handler.startElement(self.name, self.element_attrs)
  85.         handler.characters(str(self.val))
  86.         handler.endElement(self.name)
  87.  
  88.  
  89.  
  90. class DateElement:
  91.     
  92.     def __init__(self, name, dt):
  93.         self.name = name
  94.         self.dt = dt
  95.  
  96.     
  97.     def publish(self, handler):
  98.         _element(handler, self.name, _format_date(self.dt))
  99.  
  100.  
  101.  
  102. class Category:
  103.     
  104.     def __init__(self, category, domain = None):
  105.         self.category = category
  106.         self.domain = domain
  107.  
  108.     
  109.     def publish(self, handler):
  110.         d = { }
  111.         if self.domain is not None:
  112.             d['domain'] = self.domain
  113.         
  114.         _element(handler, 'category', self.category, d)
  115.  
  116.  
  117.  
  118. class Cloud:
  119.     
  120.     def __init__(self, domain, port, path, registerProcedure, protocol):
  121.         self.domain = domain
  122.         self.port = port
  123.         self.path = path
  124.         self.registerProcedure = registerProcedure
  125.         self.protocol = protocol
  126.  
  127.     
  128.     def publish(self, handler):
  129.         _element(handler, 'cloud', None, {
  130.             'domain': self.domain,
  131.             'port': str(self.port),
  132.             'path': self.path,
  133.             'registerProcedure': self.registerProcedure,
  134.             'protocol': self.protocol })
  135.  
  136.  
  137.  
  138. class Image:
  139.     element_attrs = { }
  140.     
  141.     def __init__(self, url, title, link, width = None, height = None, description = None):
  142.         self.url = url
  143.         self.title = title
  144.         self.link = link
  145.         self.width = width
  146.         self.height = height
  147.         self.description = description
  148.  
  149.     
  150.     def publish(self, handler):
  151.         handler.startElement('image', self.element_attrs)
  152.         _element(handler, 'url', self.url)
  153.         _element(handler, 'title', self.title)
  154.         _element(handler, 'link', self.link)
  155.         width = self.width
  156.         if isinstance(width, int):
  157.             width = IntElement('width', width)
  158.         
  159.         _opt_element(handler, 'width', width)
  160.         height = self.height
  161.         if isinstance(height, int):
  162.             height = IntElement('height', height)
  163.         
  164.         _opt_element(handler, 'height', height)
  165.         _opt_element(handler, 'description', self.description)
  166.         handler.endElement('image')
  167.  
  168.  
  169.  
  170. class Guid:
  171.     
  172.     def __init__(self, guid, isPermaLink = 1):
  173.         self.guid = guid
  174.         self.isPermaLink = isPermaLink
  175.  
  176.     
  177.     def publish(self, handler):
  178.         d = { }
  179.         if self.isPermaLink:
  180.             d['isPermaLink'] = 'true'
  181.         else:
  182.             d['isPermaLink'] = 'false'
  183.         _element(handler, 'guid', self.guid, d)
  184.  
  185.  
  186.  
  187. class TextInput:
  188.     element_attrs = { }
  189.     
  190.     def __init__(self, title, description, name, link):
  191.         self.title = title
  192.         self.description = description
  193.         self.name = name
  194.         self.link = link
  195.  
  196.     
  197.     def publish(self, handler):
  198.         handler.startElement('textInput', self.element_attrs)
  199.         _element(handler, 'title', self.title)
  200.         _element(handler, 'description', self.description)
  201.         _element(handler, 'name', self.name)
  202.         _element(handler, 'link', self.link)
  203.         handler.endElement('textInput')
  204.  
  205.  
  206.  
  207. class Enclosure:
  208.     
  209.     def __init__(self, url, length, type):
  210.         self.url = url
  211.         self.length = length
  212.         self.type = type
  213.  
  214.     
  215.     def publish(self, handler):
  216.         _element(handler, 'enclosure', None, {
  217.             'url': self.url,
  218.             'length': str(self.length),
  219.             'type': self.type })
  220.  
  221.  
  222.  
  223. class Source:
  224.     
  225.     def __init__(self, name, url):
  226.         self.name = name
  227.         self.url = url
  228.  
  229.     
  230.     def publish(self, handler):
  231.         _element(handler, 'source', self.name, {
  232.             'url': self.url })
  233.  
  234.  
  235.  
  236. class SkipHours:
  237.     element_attrs = { }
  238.     
  239.     def __init__(self, hours):
  240.         self.hours = hours
  241.  
  242.     
  243.     def publish(self, handler):
  244.         if self.hours:
  245.             handler.startElement('skipHours', self.element_attrs)
  246.             for hour in self.hours:
  247.                 _element(handler, 'hour', str(hour))
  248.             
  249.             handler.endElement('skipHours')
  250.         
  251.  
  252.  
  253.  
  254. class SkipDays:
  255.     element_attrs = { }
  256.     
  257.     def __init__(self, days):
  258.         self.days = days
  259.  
  260.     
  261.     def publish(self, handler):
  262.         if self.days:
  263.             handler.startElement('skipDays', self.element_attrs)
  264.             for day in self.days:
  265.                 _element(handler, 'day', day)
  266.             
  267.             handler.endElement('skipDays')
  268.         
  269.  
  270.  
  271.  
  272. class RSS2(WriteXmlMixin):
  273.     rss_attrs = {
  274.         'version': '2.0' }
  275.     element_attrs = { }
  276.     
  277.     def __init__(self, title, link, description, language = None, copyright = None, managingEditor = None, webMaster = None, pubDate = None, lastBuildDate = None, categories = None, generator = _generator_name, docs = 'http://blogs.law.harvard.edu/tech/rss', cloud = None, ttl = None, image = None, rating = None, textInput = None, skipHours = None, skipDays = None, items = None):
  278.         self.title = title
  279.         self.link = link
  280.         self.description = description
  281.         self.language = language
  282.         self.copyright = copyright
  283.         self.managingEditor = managingEditor
  284.         self.webMaster = webMaster
  285.         self.pubDate = pubDate
  286.         self.lastBuildDate = lastBuildDate
  287.         if categories is None:
  288.             categories = []
  289.         
  290.         self.categories = categories
  291.         self.generator = generator
  292.         self.docs = docs
  293.         self.cloud = cloud
  294.         self.ttl = ttl
  295.         self.image = image
  296.         self.rating = rating
  297.         self.textInput = textInput
  298.         self.skipHours = skipHours
  299.         self.skipDays = skipDays
  300.         if items is None:
  301.             items = []
  302.         
  303.         self.items = items
  304.  
  305.     
  306.     def publish(self, handler):
  307.         handler.startElement('rss', self.rss_attrs)
  308.         handler.startElement('channel', self.element_attrs)
  309.         _element(handler, 'title', self.title)
  310.         _element(handler, 'link', self.link)
  311.         _element(handler, 'description', self.description)
  312.         self.publish_extensions(handler)
  313.         _opt_element(handler, 'language', self.language)
  314.         _opt_element(handler, 'copyright', self.copyright)
  315.         _opt_element(handler, 'managingEditor', self.managingEditor)
  316.         _opt_element(handler, 'webMaster', self.webMaster)
  317.         pubDate = self.pubDate
  318.         if isinstance(pubDate, datetime.datetime):
  319.             pubDate = DateElement('pubDate', pubDate)
  320.         
  321.         _opt_element(handler, 'pubDate', pubDate)
  322.         lastBuildDate = self.lastBuildDate
  323.         if isinstance(lastBuildDate, datetime.datetime):
  324.             lastBuildDate = DateElement('lastBuildDate', lastBuildDate)
  325.         
  326.         _opt_element(handler, 'lastBuildDate', lastBuildDate)
  327.         for category in self.categories:
  328.             if isinstance(category, basestring):
  329.                 category = Category(category)
  330.             
  331.             category.publish(handler)
  332.         
  333.         _opt_element(handler, 'generator', self.generator)
  334.         _opt_element(handler, 'docs', self.docs)
  335.         if self.cloud is not None:
  336.             self.cloud.publish(handler)
  337.         
  338.         ttl = self.ttl
  339.         if isinstance(self.ttl, int):
  340.             ttl = IntElement('ttl', ttl)
  341.         
  342.         _opt_element(handler, 'tt', ttl)
  343.         if self.image is not None:
  344.             self.image.publish(handler)
  345.         
  346.         _opt_element(handler, 'rating', self.rating)
  347.         if self.textInput is not None:
  348.             self.textInput.publish(handler)
  349.         
  350.         if self.skipHours is not None:
  351.             self.skipHours.publish(handler)
  352.         
  353.         if self.skipDays is not None:
  354.             self.skipDays.publish(handler)
  355.         
  356.         for item in self.items:
  357.             item.publish(handler)
  358.         
  359.         handler.endElement('channel')
  360.         handler.endElement('rss')
  361.  
  362.     
  363.     def publish_extensions(self, handler):
  364.         pass
  365.  
  366.  
  367.  
  368. class RSSItem(WriteXmlMixin):
  369.     element_attrs = { }
  370.     
  371.     def __init__(self, title = None, link = None, description = None, author = None, categories = None, comments = None, enclosure = None, guid = None, pubDate = None, source = None):
  372.         if title is None and description is None:
  373.             raise TypeError("must define at least one of 'title' or 'description'")
  374.         description is None
  375.         self.title = title
  376.         self.link = link
  377.         self.description = description
  378.         self.author = author
  379.         if categories is None:
  380.             categories = []
  381.         
  382.         self.categories = categories
  383.         self.comments = comments
  384.         self.enclosure = enclosure
  385.         self.guid = guid
  386.         self.pubDate = pubDate
  387.         self.source = source
  388.  
  389.     
  390.     def publish(self, handler):
  391.         handler.startElement('item', self.element_attrs)
  392.         _opt_element(handler, 'title', self.title)
  393.         _opt_element(handler, 'link', self.link)
  394.         self.publish_extensions(handler)
  395.         _opt_element(handler, 'description', self.description)
  396.         _opt_element(handler, 'author', self.author)
  397.         for category in self.categories:
  398.             if isinstance(category, basestring):
  399.                 category = Category(category)
  400.             
  401.             category.publish(handler)
  402.         
  403.         _opt_element(handler, 'comments', self.comments)
  404.         if self.enclosure is not None:
  405.             self.enclosure.publish(handler)
  406.         
  407.         _opt_element(handler, 'guid', self.guid)
  408.         pubDate = self.pubDate
  409.         if isinstance(pubDate, datetime.datetime):
  410.             pubDate = DateElement('pubDate', pubDate)
  411.         
  412.         _opt_element(handler, 'pubDate', pubDate)
  413.         if self.source is not None:
  414.             self.source.publish(handler)
  415.         
  416.         handler.endElement('item')
  417.  
  418.     
  419.     def publish_extensions(self, handler):
  420.         pass
  421.  
  422.  
  423.