home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os.path as os
- __license__ = 'GPL v3'
- __copyright__ = '2009, John Schember <john@nachtimwald.com>'
- __docformat__ = 'restructuredtext en'
- import glob
- import os
- import shutil
- from calibre.customize.conversion import InputFormatPlugin
- from calibre.ptempfile import TemporaryDirectory
- from calibre.utils.zipfile import ZipFile
- from calibre.ebooks.pml.pmlconverter import PML_HTMLizer
- from calibre.ebooks.metadata.toc import TOC
- from calibre.ebooks.metadata.opf2 import OPFCreator
-
- class PMLInput(InputFormatPlugin):
- name = 'PML Input'
- author = 'John Schember'
- description = 'Convert PML to OEB'
- file_types = set([
- 'pml',
- 'pmlz'])
-
- def process_pml(self, pml_path, html_path, close_all = False):
- pclose = False
- hclose = False
- if not hasattr(pml_path, 'read'):
- pml_stream = open(pml_path, 'rb')
- pclose = True
- else:
- pml_stream = pml_path
- pml_stream.seek(0)
- if not hasattr(html_path, 'write'):
- html_stream = open(html_path, 'wb')
- hclose = True
- else:
- html_stream = html_path
- ienc = None if pml_stream.encoding else 'cp1252'
- if self.options.input_encoding:
- ienc = self.options.input_encoding
-
- self.log.debug('Converting PML to HTML...')
- hizer = PML_HTMLizer()
- html = hizer.parse_pml(pml_stream.read().decode(ienc), html_path)
- html_stream.write('<html><head><title /></head><body>%s</body></html>' % html.encode('utf-8', 'replace'))
- if pclose:
- pml_stream.close()
-
- if hclose:
- html_stream.close()
-
- return hizer.get_toc()
-
-
- def get_images(self, stream, tdir, top_level = False):
- images = []
- imgs = []
- if top_level:
- imgs = glob.glob(os.path.join(tdir, '*.png'))
-
- if not imgs:
- if hasattr(stream, 'name'):
- imgs = glob.glob(os.path.join(tdir, os.path.splitext(os.path.basename(stream.name))[0] + '_img', '*.png'))
-
-
- if not imgs:
- imgs = glob.glob(os.path.join(os.path.join(tdir, 'images'), '*.png'))
-
- if imgs:
- os.makedirs(os.path.join(os.getcwd(), 'images'))
-
- for img in imgs:
- pimg_name = os.path.basename(img)
- pimg_path = os.path.join(os.getcwd(), 'images', pimg_name)
- images.append('images/' + pimg_name)
- shutil.copy(img, pimg_path)
-
- return images
-
-
- def convert(self, stream, options, file_ext, log, accelerators):
- self.options = options
- self.log = log
- pages = []
- images = []
- toc = TOC()
- if file_ext == 'pmlz':
- log.debug('De-compressing content to temporary directory...')
-
- try:
- tdir = _[1]
- zf = ZipFile(stream)
- zf.extractall(tdir)
- pmls = glob.glob(os.path.join(tdir, '*.pml'))
- for pml in pmls:
- html_name = os.path.splitext(os.path.basename(pml))[0] + '.html'
- html_path = os.path.join(os.getcwd(), html_name)
- pages.append(html_name)
- log.debug('Processing PML item %s...' % pml)
- ttoc = self.process_pml(pml, html_path)
- toc += ttoc
-
- images = self.get_images(stream, tdir, True)
- finally:
- pass
-
- else:
- toc = self.process_pml(stream, 'index.html')
- pages.append('index.html')
- if hasattr(stream, 'name'):
- images = self.get_images(stream, os.path.abspath(os.path.dirname(stream.name)))
-
- pages.sort()
- manifest_items = []
- for item in pages + images:
- manifest_items.append((item, None))
-
- get_metadata = get_metadata
- import calibre.ebooks.metadata.meta
- log.debug('Reading metadata from input file...')
- mi = get_metadata(stream, 'pml')
- if 'images/cover.png' in images:
- mi.cover = 'images/cover.png'
-
- opf = OPFCreator(os.getcwd(), mi)
- log.debug('Generating manifest...')
- opf.create_manifest(manifest_items)
- opf.create_spine(pages)
- opf.set_toc(toc)
-
- try:
- opffile = _[2]
-
- try:
- tocfile = _[3]
- opf.render(opffile, tocfile, 'toc.ncx')
- finally:
- pass
-
- finally:
- pass
-
- return os.path.join(os.getcwd(), 'metadata.opf')
-
-
-