epub: fix --working-copy description
[librarian.git] / scripts / book2mobi
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 #
7 import os.path
8 import optparse
9
10 from librarian import DirDocProvider, ParseError
11 from librarian.parser import WLDocument
12
13
14 if __name__ == '__main__':
15     # Parse commandline arguments
16     usage = """Usage: %prog [options] SOURCE [SOURCE...]
17     Convert SOURCE files to MOBI format."""
18
19     parser = optparse.OptionParser(usage=usage)
20
21     parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
22         help='print status messages to stdout')
23     parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False,
24                       help='create a directory for author and put the PDF in it')
25     parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE',
26                       help='specifies the output file')
27     parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR',
28                       help='specifies the directory for output')
29
30     options, input_filenames = parser.parse_args()
31
32     if len(input_filenames) < 1:
33         parser.print_help()
34         exit(1)
35
36     # Do some real work
37     try:
38         for main_input in input_filenames:
39             path, fname = os.path.realpath(main_input).rsplit('/', 1)
40             provider = DirDocProvider(path)
41             if not (options.output_file or options.output_dir):
42                 output_file = os.path.splitext(main_input)[0] + '.mobi'
43             else:
44                 output_file = None
45
46             doc = WLDocument.from_file(main_input, provider=provider)
47             mobi = doc.as_mobi()
48
49             doc.save_output_file(mobi,
50                 output_file, options.output_dir, options.make_dir, 'mobi')
51     except ParseError, e:
52         print '%(file)s:%(name)s:%(message)s' % {
53             'file': main_input,
54             'name': e.__class__.__name__,
55             'message': e
56         }