home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1005 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.4 KB  |  148 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 textwrap
  9. from xml.sax.saxutils import escape
  10. from itertools import repeat
  11. from lxml import etree
  12. from calibre.ebooks.oeb.base import XPath, XPNSMAP
  13. from calibre import guess_type
  14. from calibre.library.comments import comments_to_html
  15.  
  16. class Jacket(object):
  17.     JACKET_TEMPLATE = textwrap.dedent(u'    <html xmlns="%(xmlns)s">\n        <head>\n            <title>%(title)s</title>\n            <meta name="calibre-content" content="jacket"/>\n        </head>\n        <body>\n            <div class="calibre_rescale_100">\n                <div style="text-align:center">\n                    <h1 class="calibre_rescale_180">%(title)s</h1>\n                    <h2 class="calibre_rescale_140">%(jacket)s</h2>\n                    <div class="calibre_rescale_100">%(series)s</div>\n                    <div class="calibre_rescale_100">%(rating)s</div>\n                    <div class="calibre_rescale_100">%(tags)s</div>\n                </div>\n                <div style="margin-top:2em" class="calibre_rescale_100">\n                    %(comments)s\n                </div>\n            </div>\n        </body>\n    </html>\n    ')
  18.     
  19.     def remove_first_image(self):
  20.         path = XPath('//h:img[@src]')
  21.         for i, item in enumerate(self.oeb.spine):
  22.             if i > 2:
  23.                 break
  24.             
  25.             for img in path(item.data):
  26.                 href = item.abshref(img.get('src'))
  27.                 image = self.oeb.manifest.hrefs.get(href, None)
  28.                 if image is not None:
  29.                     self.log('Removing first image', img.get('src'))
  30.                     self.oeb.manifest.remove(image)
  31.                     img.getparent().remove(img)
  32.                     return None
  33.             
  34.         
  35.  
  36.     
  37.     def get_rating(self, rating):
  38.         ans = ''
  39.         if rating is None:
  40.             return None
  41.         
  42.         try:
  43.             num = float(rating) / 2
  44.         except:
  45.             rating is None
  46.             return ans
  47.  
  48.         num = max(0, num)
  49.         num = min(num, 5)
  50.         if num < 1:
  51.             return ans
  52.         (id, href) = self.oeb.manifest.generate('star', 'star.png')
  53.         self.oeb.manifest.add(id, href, 'image/png', data = I('star.png', data = True))
  54.         ans = 'Rating: ' + ''.join(repeat('<img style="vertical-align:text-top" alt="star" src="%s" />' % href, num))
  55.         return ans
  56.  
  57.     
  58.     def insert_metadata(self, mi):
  59.         self.log('Inserting metadata into book...')
  60.         comments = mi.comments
  61.         if not comments:
  62.             
  63.             try:
  64.                 comments = unicode(self.oeb.metadata.description[0])
  65.             comments = ''
  66.  
  67.         
  68.         if not comments.strip():
  69.             comments = ''
  70.         
  71.         orig_comments = comments
  72.         if comments:
  73.             comments = comments_to_html(comments)
  74.         
  75.         series = None + '<b>Series: </b>'(escape if mi.series else '')
  76.         if mi.series and mi.series_index is not None:
  77.             series += escape(' [%s]' % mi.format_series_index())
  78.         
  79.         if not mi.series:
  80.             series = ''
  81.         
  82.         tags = mi.tags
  83.         if not tags:
  84.             
  85.             try:
  86.                 tags = map(unicode, self.oeb.metadata.subject)
  87.             tags = []
  88.  
  89.         
  90.         if tags:
  91.             tags = '<b>Tags: </b>' + self.opts.dest.tags_to_string(tags)
  92.         else:
  93.             tags = ''
  94.         
  95.         try:
  96.             title = None if mi.title else unicode(self.oeb.metadata.title[0])
  97.         except:
  98.             title = _('Unknown')
  99.  
  100.         
  101.         def generate_html(comments):
  102.             return self.JACKET_TEMPLATE % dict(xmlns = XPNSMAP['h'], title = escape(title), comments = comments, jacket = escape(_('Book Jacket')), series = series, tags = tags, rating = self.get_rating(mi.rating))
  103.  
  104.         (id, href) = self.oeb.manifest.generate('jacket', 'jacket.xhtml')
  105.         RECOVER_PARSER = RECOVER_PARSER
  106.         XPath = XPath
  107.         import calibre.ebooks.oeb.base
  108.         
  109.         try:
  110.             root = etree.fromstring(generate_html(comments), parser = RECOVER_PARSER)
  111.         except:
  112.             (None, None, None, None, None)
  113.             root = etree.fromstring(generate_html(escape(orig_comments)), parser = RECOVER_PARSER)
  114.  
  115.         jacket = XPath('//h:meta[@name="calibre-content" and @content="jacket"]')
  116.         found = None
  117.         for item in list(self.oeb.spine)[:4]:
  118.             
  119.             try:
  120.                 if jacket(item.data):
  121.                     found = item
  122.                     break
  123.             continue
  124.             continue
  125.             continue
  126.  
  127.         
  128.         if found is None:
  129.             item = self.oeb.manifest.add(id, href, guess_type(href)[0], data = root)
  130.             self.oeb.spine.insert(0, item, True)
  131.         else:
  132.             self.log('Found existing book jacket, replacing...')
  133.             found.data = root
  134.  
  135.     
  136.     def __call__(self, oeb, opts, metadata):
  137.         self.oeb = oeb
  138.         self.opts = opts
  139.         self.log = oeb.log
  140.         if opts.remove_first_image:
  141.             self.remove_first_image()
  142.         
  143.         if opts.insert_metadata:
  144.             self.insert_metadata(metadata)
  145.         
  146.  
  147.  
  148.