Epub: fixes #4164, #4166
[librarian.git] / src / librarian / book2anything.py
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 from __future__ import print_function, unicode_literals
8
9 import os.path
10 import optparse
11 import six
12 from librarian import DirDocProvider, ParseError
13 from librarian.parser import WLDocument
14 from librarian.cover import make_cover, COVER_CLASSES
15
16
17 class Option(object):
18     """Option for optparse. Use it like `optparse.OptionParser.add_option`."""
19     def __init__(self, *names, **options):
20         self.names = names
21         self.options = options
22
23     def add(self, parser):
24         parser.add_option(*self.names, **self.options)
25
26     def name(self):
27         return self.options['dest']
28
29     def value(self, options):
30         return getattr(options, self.name())
31
32
33 class Book2Anything(object):
34     """A class for creating book2... scripts.
35
36     Subclass it for any format you want to convert to.
37     """
38     format_name = None  # Set format name, like "PDF".
39     ext = None  # Set file extension, like "pdf".
40     uses_cover = False  # Can it add a cover?
41     cover_optional = True  # Only relevant if uses_cover
42     uses_provider = False  # Does it need a DocProvider?
43     transform = None  # Transform method. Uses WLDocument.as_{ext} by default.
44     parser_options = []  # List of Option objects for additional parser args.
45     # List of Option objects for additional transform args.
46     transform_options = []
47     # List of Option objects for supported transform flags.
48     transform_flags = []
49
50     @classmethod
51     def run(cls):
52         # Parse commandline arguments
53         usage = """Usage: %%prog [options] SOURCE [SOURCE...]
54         Convert SOURCE files to %s format.""" % cls.format_name
55
56         parser = optparse.OptionParser(usage=usage)
57
58         parser.add_option(
59             '-v', '--verbose', action='store_true', dest='verbose',
60             default=False, help='print status messages to stdout')
61         parser.add_option(
62             '-d', '--make-dir', action='store_true', dest='make_dir',
63             default=False,
64             help='create a directory for author and put the output file in it'
65         )
66         parser.add_option(
67             '-o', '--output-file', dest='output_file', metavar='FILE',
68             help='specifies the output file')
69         parser.add_option(
70             '-O', '--output-dir', dest='output_dir', metavar='DIR',
71             help='specifies the directory for output'
72         )
73         if cls.uses_cover:
74             if cls.cover_optional:
75                 parser.add_option(
76                     '-c', '--with-cover', action='store_true',
77                     dest='with_cover', default=False,
78                     help='create default cover'
79                 )
80             parser.add_option(
81                 '-C', '--image-cache', dest='image_cache', metavar='URL',
82                 help='prefix for image download cache'
83                 + (' (implies --with-cover)' if cls.cover_optional else '')
84             )
85             parser.add_option(
86                 '--cover-class', dest='cover_class',
87                 help='cover class name'
88             )
89         for option in (
90                 cls.parser_options
91                 + cls.transform_options
92                 + cls.transform_flags):
93             option.add(parser)
94
95         options, input_filenames = parser.parse_args()
96
97         if len(input_filenames) < 1:
98             parser.print_help()
99             return 1
100
101         # Prepare additional args for parser.
102         parser_args = {}
103         for option in cls.parser_options:
104             parser_args[option.name()] = option.value(options)
105         # Prepare additional args for transform method.
106         transform_args = {}
107         for option in cls.transform_options:
108             transform_args[option.name()] = option.value(options)
109         # Add flags to transform_args, if any.
110         transform_flags = [
111             flag.name()
112             for flag in cls.transform_flags
113             if flag.value(options)
114         ]
115         if transform_flags:
116             transform_args['flags'] = transform_flags
117         if options.verbose:
118             transform_args['verbose'] = True
119         # Add cover support, if any.
120         if cls.uses_cover:
121             if options.image_cache:
122                 def cover_class(book_info, *args, **kwargs):
123                     return make_cover(
124                         book_info, image_cache=options.image_cache,
125                         cover_class=options.cover_class,
126                         *args, **kwargs
127                     )
128                 transform_args['cover'] = cover_class
129             elif not cls.cover_optional or options.with_cover:
130                 cover_class = COVER_CLASSES.get(
131                     options.cover_class, make_cover)
132                 transform_args['cover'] = cover_class
133
134         # Do some real work
135         try:
136             for main_input in input_filenames:
137                 if options.verbose:
138                     print(main_input)
139
140             if isinstance(main_input, six.binary_type):
141                 main_input = main_input.decode('utf-8')
142
143             # Where to find input?
144             if cls.uses_provider:
145                 path, fname = os.path.realpath(main_input).rsplit('/', 1)
146                 provider = DirDocProvider(path)
147             else:
148                 provider = None
149
150             # Where to write output?
151             if not (options.output_file or options.output_dir):
152                 output_file = os.path.splitext(main_input)[0] + '.' + cls.ext
153             else:
154                 output_file = options.output_file
155
156             # Do the transformation.
157             doc = WLDocument.from_file(main_input, provider=provider,
158                                        **parser_args)
159             transform = cls.transform
160             if transform is None:
161                 transform = getattr(WLDocument, 'as_%s' % cls.ext)
162             output = transform(doc, **transform_args)
163
164             doc.save_output_file(output, output_file, options.output_dir,
165                                  options.make_dir, cls.ext)
166
167         except ParseError as e:
168             print('%(file)s:%(name)s:%(message)s' % {
169                 'file': main_input,
170                 'name': e.__class__.__name__,
171                 'message': e
172             })