~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/oeb/polish/import_book.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-05-14 18:17:50 UTC
  • mto: This revision was merged to the branch mainline in revision 75.
  • Revision ID: package-import@ubuntu.com-20140514181750-efj1wymey2vb4cao
Tags: upstream-1.36.0+dfsg
ImportĀ upstreamĀ versionĀ 1.36.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim:fileencoding=utf-8
 
3
from __future__ import (unicode_literals, division, absolute_import,
 
4
                        print_function)
 
5
 
 
6
__license__ = 'GPL v3'
 
7
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
 
8
 
 
9
import os, sys
 
10
 
 
11
from calibre.ptempfile import TemporaryDirectory
 
12
from calibre.ebooks.conversion.plumber import Plumber
 
13
from calibre.ebooks.oeb.polish.container import Container, OEB_DOCS, OEB_STYLES
 
14
from calibre.ebooks.epub import initialize_container
 
15
 
 
16
from calibre.utils.logging import default_log
 
17
 
 
18
IMPORTABLE = {'htm', 'xhtml', 'html', 'xhtm', 'docx'}
 
19
 
 
20
def auto_fill_manifest(container):
 
21
    manifest_id_map = container.manifest_id_map
 
22
    manifest_name_map = {v:k for k, v in manifest_id_map.iteritems()}
 
23
 
 
24
    for name, mt in container.mime_map.iteritems():
 
25
        if name not in manifest_name_map and not container.ok_to_be_unmanifested(name):
 
26
            mitem = container.generate_item(name)
 
27
            gname = container.href_to_name(mitem.get('href'), container.opf_name)
 
28
            if gname != name:
 
29
                raise ValueError('This should never happen (gname=%r, name=%r)' % (gname, name))
 
30
            manifest_name_map[name] = mitem.get('id')
 
31
            manifest_id_map[mitem.get('id')] = name
 
32
 
 
33
def import_book_as_epub(srcpath, destpath, log=default_log):
 
34
    if not destpath.lower().endswith('.epub'):
 
35
        raise ValueError('Can only import books into the EPUB format, not %s' % (os.path.basename(destpath)))
 
36
    with TemporaryDirectory('eei') as tdir:
 
37
        tdir = os.path.abspath(os.path.realpath(tdir))  # Needed to handle the multiple levels of symlinks for /tmp on OS X
 
38
        plumber = Plumber(srcpath, tdir, log)
 
39
        plumber.setup_options()
 
40
        if srcpath.lower().endswith('.opf'):
 
41
            plumber.opts.dont_package = True
 
42
        if hasattr(plumber.opts, 'no_process'):
 
43
            plumber.opts.no_process = True
 
44
        plumber.input_plugin.for_viewer = True
 
45
        with plumber.input_plugin, open(plumber.input, 'rb') as inf:
 
46
            pathtoopf = plumber.input_plugin(inf, plumber.opts, plumber.input_fmt, log, {}, tdir)
 
47
        if hasattr(pathtoopf, 'manifest'):
 
48
            from calibre.ebooks.oeb.iterator.book import write_oebbook
 
49
            pathtoopf = write_oebbook(pathtoopf, tdir)
 
50
 
 
51
        c = Container(tdir, pathtoopf, log)
 
52
        auto_fill_manifest(c)
 
53
        # Auto fix all HTML/CSS
 
54
        for name, mt in c.mime_map.iteritems():
 
55
            if mt in set(OEB_DOCS) | set(OEB_STYLES):
 
56
                c.parsed(name)
 
57
                c.dirty(name)
 
58
        c.commit()
 
59
 
 
60
        zf = initialize_container(destpath, opf_name=c.opf_name)
 
61
        with zf:
 
62
            for name in c.name_path_map:
 
63
                zf.writestr(name, c.raw_data(name, decode=False))
 
64
 
 
65
if __name__ == '__main__':
 
66
    import_book_as_epub(sys.argv[-2], sys.argv[-1])
 
67