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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import glob
  8. import os
  9. from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
  10. from calibre.ebooks.metadata.opf2 import OPF
  11. from calibre.ptempfile import TemporaryDirectory
  12. from calibre.ebooks.pdf.writer import PDFWriter, ImagePDFWriter, PDFMetadata
  13. from calibre.ebooks.pdf.pageoptions import UNITS, PAPER_SIZES, ORIENTATIONS
  14.  
  15. class PDFOutput(OutputFormatPlugin):
  16.     name = 'PDF Output'
  17.     author = 'John Schember and Kovid Goyal'
  18.     file_type = 'pdf'
  19.     options = set([
  20.         OptionRecommendation(name = 'unit', recommended_value = 'inch', level = OptionRecommendation.LOW, short_switch = 'u', choices = UNITS.keys(), help = _('The unit of measure. Default is inch. Choices are %s Note: This does not override the unit for margins!') % UNITS.keys()),
  21.         OptionRecommendation(name = 'paper_size', recommended_value = 'letter', level = OptionRecommendation.LOW, choices = PAPER_SIZES.keys(), help = _('The size of the paper. This size will be overridden when an output profile is used. Default is letter. Choices are %s') % PAPER_SIZES.keys()),
  22.         OptionRecommendation(name = 'custom_size', recommended_value = None, help = _('Custom size of the document. Use the form widthxheight EG. `123x321` to specify the width and height. This overrides any specified paper-size.')),
  23.         OptionRecommendation(name = 'orientation', recommended_value = 'portrait', level = OptionRecommendation.LOW, choices = ORIENTATIONS.keys(), help = _('The orientation of the page. Default is portrait. Choices are %s') % ORIENTATIONS.keys()),
  24.         OptionRecommendation(name = 'preserve_cover_aspect_ratio', recommended_value = False, help = _('Preserve the aspect ratio of the cover, instead of stretching it to fill the ull first page of the generated pdf.'))])
  25.     
  26.     def convert(self, oeb_book, output_path, input_plugin, opts, log):
  27.         self.oeb = oeb_book
  28.         self.input_plugin = input_plugin
  29.         self.opts = opts
  30.         self.log = log
  31.         self.output_path = output_path
  32.         self.metadata = oeb_book.metadata
  33.         self.cover_data = None
  34.         OEB_STYLES = OEB_STYLES
  35.         XPath = XPath
  36.         import calibre.ebooks.oeb.base
  37.         stylesheet = None
  38.         for item in self.oeb.manifest:
  39.             if item.media_type.lower() in OEB_STYLES:
  40.                 stylesheet = item
  41.                 break
  42.                 continue
  43.         
  44.         if stylesheet is not None:
  45.             CSSRule = CSSRule
  46.             import cssutils.css
  47.             classes = set([
  48.                 '.calibre'])
  49.             for x in self.oeb.spine:
  50.                 root = x.data
  51.                 body = XPath('//h:body[@class]')(root)
  52.                 if body:
  53.                     classes.add('.' + body[0].get('class'))
  54.                     continue
  55.             
  56.             for rule in stylesheet.data.cssRules.rulesOfType(CSSRule.STYLE_RULE):
  57.                 if rule.selectorList.selectorText in classes:
  58.                     rule.style.removeProperty('page-break-before')
  59.                     rule.style.removeProperty('page-break-after')
  60.                     continue
  61.             
  62.         
  63.         if input_plugin.is_image_collection:
  64.             log.debug('Converting input as an image collection...')
  65.             self.convert_images(input_plugin.get_images())
  66.         else:
  67.             log.debug('Converting input as a text based book...')
  68.             self.convert_text(oeb_book)
  69.  
  70.     
  71.     def convert_images(self, images):
  72.         self.write(ImagePDFWriter, images)
  73.  
  74.     
  75.     def get_cover_data(self):
  76.         g = self.oeb.guide
  77.         m = self.oeb.manifest
  78.         if 'titlepage' not in g:
  79.             if 'cover' in g:
  80.                 href = g['cover'].href
  81.                 urlnormalize = urlnormalize
  82.                 import calibre.ebooks.oeb.base
  83.                 for item in m:
  84.                     if item.href == urlnormalize(href):
  85.                         self.cover_data = item.data
  86.                         continue
  87.                 
  88.             
  89.         
  90.  
  91.     
  92.     def convert_text(self, oeb_book):
  93.         self.log.debug('Serializing oeb input to disk for processing...')
  94.         self.get_cover_data()
  95.         
  96.         try:
  97.             oeb_dir = _[1]
  98.             plugin_for_output_format = plugin_for_output_format
  99.             import calibre.customize.ui
  100.             oeb_output = plugin_for_output_format('oeb')
  101.             oeb_output.convert(oeb_book, oeb_dir, self.input_plugin, self.opts, self.log)
  102.             opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0]
  103.             opf = OPF(opfpath, os.path.dirname(opfpath))
  104.             []([], [ s.path for s in opf.spine ])
  105.         finally:
  106.             pass
  107.  
  108.  
  109.     
  110.     def write(self, Writer, items):
  111.         writer = Writer(self.opts, self.log, cover_data = self.cover_data)
  112.         close = False
  113.         if not hasattr(self.output_path, 'write'):
  114.             close = True
  115.             if not os.path.exists(os.path.dirname(self.output_path)) and os.path.dirname(self.output_path) != '':
  116.                 os.makedirs(os.path.dirname(self.output_path))
  117.             
  118.             out_stream = open(self.output_path, 'wb')
  119.         else:
  120.             out_stream = self.output_path
  121.         out_stream.seek(0)
  122.         out_stream.truncate()
  123.         self.log.debug('Rendering pages to PDF...')
  124.         writer.dump(items, out_stream, PDFMetadata(self.metadata))
  125.         if close:
  126.             out_stream.close()
  127.         
  128.  
  129.  
  130.