Style changes.
[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
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         for option in (
86                 cls.parser_options
87                 + cls.transform_options
88                 + cls.transform_flags):
89             option.add(parser)
90
91         options, input_filenames = parser.parse_args()
92
93         if len(input_filenames) < 1:
94             parser.print_help()
95             return 1
96
97         # Prepare additional args for parser.
98         parser_args = {}
99         for option in cls.parser_options:
100             parser_args[option.name()] = option.value(options)
101         # Prepare additional args for transform method.
102         transform_args = {}
103         for option in cls.transform_options:
104             transform_args[option.name()] = option.value(options)
105         # Add flags to transform_args, if any.
106         transform_flags = [
107             flag.name()
108             for flag in cls.transform_flags
109             if flag.value(options)
110         ]
111         if transform_flags:
112             transform_args['flags'] = transform_flags
113         if options.verbose:
114             transform_args['verbose'] = True
115         # Add cover support, if any.
116         if cls.uses_cover:
117             if options.image_cache:
118                 def cover_class(book_info, *args, **kwargs):
119                     return make_cover(
120                         book_info, image_cache=options.image_cache,
121                         *args, **kwargs
122                     )
123                 transform_args['cover'] = cover_class
124             elif not cls.cover_optional or options.with_cover:
125                 transform_args['cover'] = make_cover
126
127         # Do some real work
128         try:
129             for main_input in input_filenames:
130                 if options.verbose:
131                     print(main_input)
132
133             if isinstance(main_input, six.binary_type):
134                 main_input = main_input.decode('utf-8')
135
136             # Where to find input?
137             if cls.uses_provider:
138                 path, fname = os.path.realpath(main_input).rsplit('/', 1)
139                 provider = DirDocProvider(path)
140             else:
141                 provider = None
142
143             # Where to write output?
144             if not (options.output_file or options.output_dir):
145                 output_file = os.path.splitext(main_input)[0] + '.' + cls.ext
146             else:
147                 output_file = options.output_file
148
149             # Do the transformation.
150             doc = WLDocument.from_file(main_input, provider=provider,
151                                        **parser_args)
152             transform = cls.transform
153             if transform is None:
154                 transform = getattr(WLDocument, 'as_%s' % cls.ext)
155             output = transform(doc, **transform_args)
156
157             doc.save_output_file(output, output_file, options.output_dir,
158                                  options.make_dir, cls.ext)
159
160         except ParseError as e:
161             print('%(file)s:%(name)s:%(message)s' % {
162                 'file': main_input,
163                 'name': e.__class__.__name__,
164                 'message': e
165             })