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