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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/lrf/epub/convert_from.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
 
__license__   = 'GPL v3'
2
 
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
3
 
 
4
 
import os, sys, shutil, logging
5
 
from calibre.ebooks.lrf import option_parser as lrf_option_parser
6
 
from calibre.ebooks import ConversionError, DRMError
7
 
from calibre.ebooks.lrf.html.convert_from import process_file as html_process_file
8
 
from calibre.ebooks.metadata.opf import OPF
9
 
from calibre.ebooks.metadata.epub import OCFDirReader
10
 
from calibre.utils.zipfile import ZipFile
11
 
from calibre import setup_cli_handlers
12
 
from calibre.ptempfile import PersistentTemporaryDirectory
13
 
 
14
 
 
15
 
def option_parser():
16
 
    return lrf_option_parser(
17
 
_('''Usage: %prog [options] mybook.epub
18
 
        
19
 
        
20
 
%prog converts mybook.epub to mybook.lrf''')
21
 
        )
22
 
 
23
 
def generate_html(pathtoepub, logger):
24
 
    if not os.access(pathtoepub, os.R_OK):
25
 
        raise ConversionError('Cannot read from ' + pathtoepub)
26
 
    tdir = PersistentTemporaryDirectory('_epub2lrf')
27
 
    #os.rmdir(tdir)
28
 
    try:
29
 
        ZipFile(pathtoepub).extractall(tdir)
30
 
    except:
31
 
        raise ConversionError, '.epub extraction failed'
32
 
    if os.path.exists(os.path.join(tdir, 'META-INF', 'encryption.xml')):
33
 
            raise DRMError(os.path.basename(pathtoepub))
34
 
    
35
 
    return tdir
36
 
 
37
 
def process_file(path, options, logger=None):
38
 
    if logger is None:
39
 
        level = logging.DEBUG if options.verbose else logging.INFO
40
 
        logger = logging.getLogger('epub2lrf')
41
 
        setup_cli_handlers(logger, level)
42
 
    epub = os.path.abspath(os.path.expanduser(path))
43
 
    tdir = generate_html(epub, logger)
44
 
    try:
45
 
        ocf = OCFDirReader(tdir)
46
 
        htmlfile = ocf.opf.spine[0].path
47
 
        options.opf = os.path.join(tdir, ocf.container[OPF.MIMETYPE])
48
 
        if not options.output:
49
 
            ext = '.lrs' if options.lrs else '.lrf'
50
 
            options.output = os.path.abspath(os.path.basename(os.path.splitext(path)[0]) + ext)
51
 
        options.output = os.path.abspath(os.path.expanduser(options.output))
52
 
        options.use_spine = True
53
 
        
54
 
        html_process_file(htmlfile, options, logger=logger)
55
 
    finally:
56
 
        try:
57
 
            shutil.rmtree(tdir)
58
 
        except:
59
 
            logger.warning('Failed to delete temporary directory '+tdir)
60
 
 
61
 
 
62
 
def main(args=sys.argv, logger=None):
63
 
    parser = option_parser()
64
 
    options, args = parser.parse_args(args)
65
 
    if len(args) != 2:
66
 
        parser.print_help()
67
 
        print
68
 
        print 'No epub file specified'
69
 
        return 1
70
 
    process_file(args[1], options, logger)
71
 
    return 0        
72
 
        
73
 
            
74
 
if __name__ == '__main__':
75
 
    sys.exit(main())