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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/oeb/output.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 3'
 
3
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
 
4
__docformat__ = 'restructuredtext en'
 
5
 
 
6
import os, re
 
7
 
 
8
from lxml import etree
 
9
 
 
10
from calibre.customize.conversion import OutputFormatPlugin
 
11
from calibre import CurrentDir
 
12
from calibre.customize.conversion import OptionRecommendation
 
13
 
 
14
from urllib import unquote
 
15
 
 
16
class OEBOutput(OutputFormatPlugin):
 
17
 
 
18
    name = 'OEB Output'
 
19
    author = 'Kovid Goyal'
 
20
    file_type = 'oeb'
 
21
 
 
22
    recommendations = set([('pretty_print', True, OptionRecommendation.HIGH)])
 
23
 
 
24
 
 
25
    def convert(self, oeb_book, output_path, input_plugin, opts, log):
 
26
        self.log, self.opts = log, opts
 
27
        if not os.path.exists(output_path):
 
28
            os.makedirs(output_path)
 
29
        from calibre.ebooks.oeb.base import OPF_MIME, NCX_MIME, PAGE_MAP_MIME
 
30
        with CurrentDir(output_path):
 
31
            results = oeb_book.to_opf2(page_map=True)
 
32
            for key in (OPF_MIME, NCX_MIME, PAGE_MAP_MIME):
 
33
                href, root = results.pop(key, [None, None])
 
34
                if root is not None:
 
35
                    raw = etree.tostring(root, pretty_print=True,
 
36
                            encoding='utf-8', xml_declaration=True)
 
37
                    if key == OPF_MIME:
 
38
                        # Needed as I can't get lxml to output opf:role and
 
39
                        # not output <opf:metadata> as well
 
40
                        raw = re.sub(r'(<[/]{0,1})opf:', r'\1', raw)
 
41
                    with open(href, 'wb') as f:
 
42
                        f.write(raw)
 
43
 
 
44
            for item in oeb_book.manifest:
 
45
                path = os.path.abspath(unquote(item.href))
 
46
                dir = os.path.dirname(path)
 
47
                if not os.path.exists(dir):
 
48
                    os.makedirs(dir)
 
49
                with open(path, 'wb') as f:
 
50
                    f.write(str(item))
 
51
 
 
52