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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/odt/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
from __future__ import with_statement
 
2
__license__   = 'GPL v3'
 
3
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
 
4
__docformat__ = 'restructuredtext en'
 
5
 
 
6
'''
 
7
Convert an ODT file into a Open Ebook
 
8
'''
 
9
import os
 
10
from odf.odf2xhtml import ODF2XHTML
 
11
 
 
12
from calibre import CurrentDir, walk
 
13
from calibre.customize.conversion import InputFormatPlugin
 
14
 
 
15
class Extract(ODF2XHTML):
 
16
 
 
17
    def extract_pictures(self, zf):
 
18
        if not os.path.exists('Pictures'):
 
19
            os.makedirs('Pictures')
 
20
        for name in zf.namelist():
 
21
            if name.startswith('Pictures'):
 
22
                data = zf.read(name)
 
23
                with open(name, 'wb') as f:
 
24
                    f.write(data)
 
25
 
 
26
    def __call__(self, stream, odir):
 
27
        from calibre.utils.zipfile import ZipFile
 
28
        from calibre.ebooks.metadata.meta import get_metadata
 
29
        from calibre.ebooks.metadata.opf2 import OPFCreator
 
30
 
 
31
 
 
32
        if not os.path.exists(odir):
 
33
            os.makedirs(odir)
 
34
        with CurrentDir(odir):
 
35
            print 'Extracting ODT file...'
 
36
            html = self.odf2xhtml(stream)
 
37
            with open('index.xhtml', 'wb') as f:
 
38
                f.write(html.encode('utf-8'))
 
39
            zf = ZipFile(stream, 'r')
 
40
            self.extract_pictures(zf)
 
41
            stream.seek(0)
 
42
            mi = get_metadata(stream, 'odt')
 
43
            if not mi.title:
 
44
                mi.title = _('Unknown')
 
45
            if not mi.authors:
 
46
                mi.authors = [_('Unknown')]
 
47
            opf = OPFCreator(os.path.abspath(os.getcwdu()), mi)
 
48
            opf.create_manifest([(os.path.abspath(f), None) for f in walk(os.getcwd())])
 
49
            opf.create_spine([os.path.abspath('index.xhtml')])
 
50
            with open('metadata.opf', 'wb') as f:
 
51
                opf.render(f)
 
52
            return os.path.abspath('metadata.opf')
 
53
 
 
54
 
 
55
class ODTInput(InputFormatPlugin):
 
56
 
 
57
    name        = 'ODT Input'
 
58
    author      = 'Kovid Goyal'
 
59
    description = 'Convert ODT (OpenOffice) files to HTML'
 
60
    file_types  = set(['odt'])
 
61
 
 
62
 
 
63
    def convert(self, stream, options, file_ext, log,
 
64
                accelerators):
 
65
        return Extract()(stream, '.')
 
66
 
 
67
    def postprocess_book(self, oeb, opts, log):
 
68
        # Fix <p><div> constructs as the asinine epubchecker complains
 
69
        # about them
 
70
        from calibre.ebooks.oeb.base import XPath, XHTML
 
71
        path = XPath('//h:p/h:div')
 
72
        for item in oeb.spine:
 
73
            root = item.data
 
74
            if not hasattr(root, 'xpath'): continue
 
75
            for div in path(root):
 
76
                div.getparent().tag = XHTML('div')
 
77
 
 
78