~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/pml/input.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
__license__   = 'GPL v3'
 
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
5
__docformat__ = 'restructuredtext en'
 
6
 
 
7
import glob
 
8
import os
 
9
import shutil
 
10
 
 
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_to_html
 
15
from calibre.ebooks.metadata.opf2 import OPFCreator
 
16
 
 
17
class PMLInput(InputFormatPlugin):
 
18
 
 
19
    name        = 'PML Input'
 
20
    author      = 'John Schember'
 
21
    description = 'Convert PML to OEB'
 
22
    # pmlz is a zip file containing pml files and png images.
 
23
    file_types  = set(['pml', 'pmlz'])
 
24
 
 
25
    def process_pml(self, pml_path, html_path):
 
26
        pclose = False
 
27
        hclose = False
 
28
    
 
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
            
 
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
        
 
41
        ienc = pml_stream.encoding if pml_stream.encoding else 'utf-8'
 
42
        if self.options.input_encoding:
 
43
            ienc = self.options.input_encoding
 
44
 
 
45
        self.log.debug('Converting PML to HTML...')
 
46
        html = pml_to_html(pml_stream.read().decode(ienc)) 
 
47
        html_stream.write('<html><head><title /></head><body>' + html.encode('utf-8', 'replace') + '</body></html>')
 
48
 
 
49
        if pclose:
 
50
            pml_stream.close()
 
51
        if hclose:
 
52
            html_stream.close()
 
53
 
 
54
    def convert(self, stream, options, file_ext, log,
 
55
                accelerators):
 
56
        self.options = options
 
57
        self.log = log
 
58
        pages, images = [], []
 
59
 
 
60
        if file_ext == 'pmlz':
 
61
            log.debug('De-compressing content to temporary directory...')
 
62
            with TemporaryDirectory('_unpmlz') as tdir:
 
63
                zf = ZipFile(stream)
 
64
                zf.extractall(tdir)
 
65
            
 
66
                pmls = glob.glob(os.path.join(tdir, '*.pml'))
 
67
                for pml in pmls:
 
68
                    html_name = os.path.splitext(os.path.basename(pml))[0]+'.html'
 
69
                    html_path = os.path.join(os.getcwd(), html_name)
 
70
                    
 
71
                    pages.append(html_name)
 
72
                    log.debug('Processing PML item %s...' % pml)
 
73
                    self.process_pml(pml, html_path)
 
74
                    
 
75
                imgs = glob.glob(os.path.join(tdir, '*.png'))
 
76
                if len(imgs) > 0:
 
77
                    os.makedirs(os.path.join(os.getcwd(), 'images'))
 
78
                for img in imgs:
 
79
                    pimg_name = os.path.basename(img)
 
80
                    pimg_path = os.path.join(os.getcwd(), 'images', pimg_name)
 
81
                    
 
82
                    images.append(pimg_name)
 
83
                    
 
84
                    shutil.move(img, pimg_path)
 
85
        else:
 
86
            self.process_pml(stream, 'index.html')
 
87
 
 
88
            pages.append('index.html')
 
89
            images = []
 
90
 
 
91
        # We want pages to be orded alphabetically.
 
92
        pages.sort()
 
93
 
 
94
        manifest_items = []
 
95
        for item in pages+images:
 
96
            manifest_items.append((item, None))
 
97
        
 
98
        from calibre.ebooks.metadata.meta import get_metadata
 
99
        log.debug('Reading metadata from input file...')
 
100
        mi = get_metadata(stream, 'pml')
 
101
        opf = OPFCreator(os.getcwd(), mi)
 
102
        log.debug('Generating manifest...')
 
103
        opf.create_manifest(manifest_items)
 
104
        opf.create_spine(pages)
 
105
        with open('metadata.opf', 'wb') as opffile:
 
106
            opf.render(opffile)
 
107
        
 
108
        return os.path.join(os.getcwd(), 'metadata.opf')