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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os.path as os
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  7. __docformat__ = 'restructuredtext en'
  8. import glob
  9. import os
  10. import shutil
  11. from calibre.customize.conversion import InputFormatPlugin
  12. from calibre.ptempfile import TemporaryDirectory
  13. from calibre.utils.zipfile import ZipFile
  14. from calibre.ebooks.pml.pmlconverter import PML_HTMLizer
  15. from calibre.ebooks.metadata.toc import TOC
  16. from calibre.ebooks.metadata.opf2 import OPFCreator
  17.  
  18. class PMLInput(InputFormatPlugin):
  19.     name = 'PML Input'
  20.     author = 'John Schember'
  21.     description = 'Convert PML to OEB'
  22.     file_types = set([
  23.         'pml',
  24.         'pmlz'])
  25.     
  26.     def process_pml(self, pml_path, html_path, close_all = False):
  27.         pclose = False
  28.         hclose = False
  29.         if not hasattr(pml_path, 'read'):
  30.             pml_stream = open(pml_path, 'rb')
  31.             pclose = True
  32.         else:
  33.             pml_stream = pml_path
  34.             pml_stream.seek(0)
  35.         if not hasattr(html_path, 'write'):
  36.             html_stream = open(html_path, 'wb')
  37.             hclose = True
  38.         else:
  39.             html_stream = html_path
  40.         ienc = None if pml_stream.encoding else 'cp1252'
  41.         if self.options.input_encoding:
  42.             ienc = self.options.input_encoding
  43.         
  44.         self.log.debug('Converting PML to HTML...')
  45.         hizer = PML_HTMLizer()
  46.         html = hizer.parse_pml(pml_stream.read().decode(ienc), html_path)
  47.         html_stream.write('<html><head><title /></head><body>%s</body></html>' % html.encode('utf-8', 'replace'))
  48.         if pclose:
  49.             pml_stream.close()
  50.         
  51.         if hclose:
  52.             html_stream.close()
  53.         
  54.         return hizer.get_toc()
  55.  
  56.     
  57.     def get_images(self, stream, tdir, top_level = False):
  58.         images = []
  59.         imgs = []
  60.         if top_level:
  61.             imgs = glob.glob(os.path.join(tdir, '*.png'))
  62.         
  63.         if not imgs:
  64.             if hasattr(stream, 'name'):
  65.                 imgs = glob.glob(os.path.join(tdir, os.path.splitext(os.path.basename(stream.name))[0] + '_img', '*.png'))
  66.             
  67.         
  68.         if not imgs:
  69.             imgs = glob.glob(os.path.join(os.path.join(tdir, 'images'), '*.png'))
  70.         
  71.         if imgs:
  72.             os.makedirs(os.path.join(os.getcwd(), 'images'))
  73.         
  74.         for img in imgs:
  75.             pimg_name = os.path.basename(img)
  76.             pimg_path = os.path.join(os.getcwd(), 'images', pimg_name)
  77.             images.append('images/' + pimg_name)
  78.             shutil.copy(img, pimg_path)
  79.         
  80.         return images
  81.  
  82.     
  83.     def convert(self, stream, options, file_ext, log, accelerators):
  84.         self.options = options
  85.         self.log = log
  86.         pages = []
  87.         images = []
  88.         toc = TOC()
  89.         if file_ext == 'pmlz':
  90.             log.debug('De-compressing content to temporary directory...')
  91.             
  92.             try:
  93.                 tdir = _[1]
  94.                 zf = ZipFile(stream)
  95.                 zf.extractall(tdir)
  96.                 pmls = glob.glob(os.path.join(tdir, '*.pml'))
  97.                 for pml in pmls:
  98.                     html_name = os.path.splitext(os.path.basename(pml))[0] + '.html'
  99.                     html_path = os.path.join(os.getcwd(), html_name)
  100.                     pages.append(html_name)
  101.                     log.debug('Processing PML item %s...' % pml)
  102.                     ttoc = self.process_pml(pml, html_path)
  103.                     toc += ttoc
  104.                 
  105.                 images = self.get_images(stream, tdir, True)
  106.             finally:
  107.                 pass
  108.  
  109.         else:
  110.             toc = self.process_pml(stream, 'index.html')
  111.             pages.append('index.html')
  112.             if hasattr(stream, 'name'):
  113.                 images = self.get_images(stream, os.path.abspath(os.path.dirname(stream.name)))
  114.             
  115.         pages.sort()
  116.         manifest_items = []
  117.         for item in pages + images:
  118.             manifest_items.append((item, None))
  119.         
  120.         get_metadata = get_metadata
  121.         import calibre.ebooks.metadata.meta
  122.         log.debug('Reading metadata from input file...')
  123.         mi = get_metadata(stream, 'pml')
  124.         if 'images/cover.png' in images:
  125.             mi.cover = 'images/cover.png'
  126.         
  127.         opf = OPFCreator(os.getcwd(), mi)
  128.         log.debug('Generating manifest...')
  129.         opf.create_manifest(manifest_items)
  130.         opf.create_spine(pages)
  131.         opf.set_toc(toc)
  132.         
  133.         try:
  134.             opffile = _[2]
  135.             
  136.             try:
  137.                 tocfile = _[3]
  138.                 opf.render(opffile, tocfile, 'toc.ncx')
  139.             finally:
  140.                 pass
  141.  
  142.         finally:
  143.             pass
  144.  
  145.         return os.path.join(os.getcwd(), 'metadata.opf')
  146.  
  147.  
  148.