home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1003 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.8 KB  |  209 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import sys
  9. from xml.sax.saxutils import escape
  10. from lxml import etree
  11. from calibre import guess_type, strftime
  12. from calibre.ebooks.BeautifulSoup import BeautifulSoup
  13. from calibre.ebooks.oeb.base import XPath, XHTML_NS, XHTML
  14. from calibre.library.comments import comments_to_html
  15. JACKET_XPATH = '//h:meta[@name="calibre-content" and @content="jacket"]'
  16.  
  17. class Jacket(object):
  18.     
  19.     def remove_images(self, item, limit = 1):
  20.         path = XPath('//h:img[@src]')
  21.         removed = 0
  22.         for img in path(item.data):
  23.             if removed >= limit:
  24.                 break
  25.             
  26.             href = item.abshref(img.get('src'))
  27.             image = self.oeb.manifest.hrefs.get(href, None)
  28.             if image is not None:
  29.                 self.oeb.manifest.remove(image)
  30.                 img.getparent().remove(img)
  31.                 removed += 1
  32.                 continue
  33.         
  34.         return removed
  35.  
  36.     
  37.     def remove_first_image(self):
  38.         for item in self.oeb.spine:
  39.             removed = self.remove_images(item)
  40.             if removed > 0:
  41.                 self.log('Removed first image')
  42.                 break
  43.                 continue
  44.         
  45.  
  46.     
  47.     def insert_metadata(self, mi):
  48.         self.log('Inserting metadata into book...')
  49.         
  50.         try:
  51.             tags = map(unicode, self.oeb.metadata.subject)
  52.         except:
  53.             tags = []
  54.  
  55.         
  56.         try:
  57.             comments = unicode(self.oeb.metadata.description[0])
  58.         except:
  59.             comments = ''
  60.  
  61.         
  62.         try:
  63.             title = unicode(self.oeb.metadata.title[0])
  64.         except:
  65.             title = _('Unknown')
  66.  
  67.         root = render_jacket(mi, self.opts.output_profile, alt_title = title, alt_tags = tags, alt_comments = comments)
  68.         (id, href) = self.oeb.manifest.generate('calibre_jacket', 'jacket.xhtml')
  69.         item = self.oeb.manifest.add(id, href, guess_type(href)[0], data = root)
  70.         self.oeb.spine.insert(0, item, True)
  71.  
  72.     
  73.     def remove_existing_jacket(self):
  74.         for x in self.oeb.spine[:4]:
  75.             if XPath(JACKET_XPATH)(x.data):
  76.                 self.remove_images(x, limit = sys.maxint)
  77.                 self.oeb.manifest.remove(x)
  78.                 self.log('Removed existing jacket')
  79.                 break
  80.                 continue
  81.         
  82.  
  83.     
  84.     def __call__(self, oeb, opts, metadata):
  85.         self.oeb = oeb
  86.         self.opts = opts
  87.         self.log = oeb.log
  88.         self.remove_existing_jacket()
  89.         if opts.remove_first_image:
  90.             self.remove_first_image()
  91.         
  92.         if opts.insert_metadata:
  93.             self.insert_metadata(metadata)
  94.         
  95.  
  96.  
  97.  
  98. def get_rating(rating, rchar):
  99.     ans = ''
  100.     
  101.     try:
  102.         num = float(rating) / 2
  103.     except:
  104.         return ans
  105.  
  106.     num = max(0, num)
  107.     num = min(num, 5)
  108.     if num < 1:
  109.         return ans
  110.     ans = rchar * int(num)
  111.     return ans
  112.  
  113.  
  114. def render_jacket(mi, output_profile, alt_title = _('Unknown'), alt_tags = [], alt_comments = ''):
  115.     css = P('jacket/stylesheet.css', data = True).decode('utf-8')
  116.     
  117.     try:
  118.         title_str = None if mi.title else alt_title
  119.     except:
  120.         title_str = _('Unknown')
  121.  
  122.     title = '<span class="title">%s</span>' % escape(title_str)
  123.     series = None(escape if mi.series else '')
  124.     if mi.series and mi.series_index is not None:
  125.         series += escape(' [%s]' % mi.format_series_index())
  126.     
  127.     if not mi.series:
  128.         series = ''
  129.     
  130.     
  131.     try:
  132.         pubdate = strftime(u'%Y', mi.pubdate.timetuple())
  133.     except:
  134.         pubdate = ''
  135.  
  136.     rating = get_rating(mi.rating, output_profile.ratings_char)
  137.     tags = None if mi.tags else alt_tags
  138.     if tags:
  139.         tags = output_profile.tags_to_string(tags)
  140.     else:
  141.         tags = ''
  142.     comments = None if mi.comments else alt_comments
  143.     comments = comments.strip()
  144.     orig_comments = comments
  145.     if comments:
  146.         comments = comments_to_html(comments)
  147.     
  148.     
  149.     def generate_html(comments):
  150.         args = dict(xmlns = XHTML_NS, title_str = title_str, css = css, title = title, pubdate_label = _('Published'), pubdate = pubdate, series_label = _('Series'), series = series, rating_label = _('Rating'), rating = rating, tags_label = _('Tags'), tags = tags, comments = comments, footer = '')
  151.         generated_html = P('jacket/template.xhtml', data = True).decode('utf-8').format(**args)
  152.         soup = BeautifulSoup(generated_html)
  153.         if not series:
  154.             series_tag = soup.find('tr', attrs = {
  155.                 'class': 'cbj_series' })
  156.             series_tag.extract()
  157.         
  158.         if not rating:
  159.             rating_tag = soup.find('tr', attrs = {
  160.                 'class': 'cbj_rating' })
  161.             rating_tag.extract()
  162.         
  163.         if not tags:
  164.             tags_tag = soup.find('tr', attrs = {
  165.                 'class': 'cbj_tags' })
  166.             tags_tag.extract()
  167.         
  168.         if not pubdate:
  169.             pubdate_tag = soup.find('tr', attrs = {
  170.                 'class': 'cbj_pubdate' })
  171.             pubdate_tag.extract()
  172.         
  173.         if output_profile.short_name != 'kindle':
  174.             hr_tag = soup.find('hr', attrs = {
  175.                 'class': 'cbj_kindle_banner_hr' })
  176.             hr_tag.extract()
  177.         
  178.         return soup.renderContents(None)
  179.  
  180.     RECOVER_PARSER = RECOVER_PARSER
  181.     import calibre.ebooks.oeb.base
  182.     
  183.     try:
  184.         root = etree.fromstring(generate_html(comments), parser = RECOVER_PARSER)
  185.     except:
  186.         (None, None, None, None, None, None, None, None)
  187.         
  188.         try:
  189.             root = etree.fromstring(generate_html(escape(orig_comments)), parser = RECOVER_PARSER)
  190.         root = etree.fromstring(generate_html(''), parser = RECOVER_PARSER)
  191.  
  192.  
  193.     return root
  194.  
  195.  
  196. def linearize_jacket(oeb):
  197.     for x in oeb.spine[:4]:
  198.         if XPath(JACKET_XPATH)(x.data):
  199.             for e in XPath('//h:table|//h:tr|//h:th')(x.data):
  200.                 e.tag = XHTML('div')
  201.             
  202.             for e in XPath('//h:td')(x.data):
  203.                 e.tag = XHTML('span')
  204.             
  205.             break
  206.             continue
  207.     
  208.  
  209.