2 # -*- coding: utf-8 -*-
4 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from __future__ import print_function, unicode_literals
12 from librarian import DirDocProvider, ParseError
13 from librarian.parser import WLDocument
14 from librarian.cover import make_cover
18 """Option for optparse. Use it like `optparse.OptionParser.add_option`."""
19 def __init__(self, *names, **options):
21 self.options = options
23 def add(self, parser):
24 parser.add_option(*self.names, **self.options)
27 return self.options['dest']
29 def value(self, options):
30 return getattr(options, self.name())
33 class Book2Anything(object):
34 """A class for creating book2... scripts.
36 Subclass it for any format you want to convert to.
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.
52 # Parse commandline arguments
53 usage = """Usage: %%prog [options] SOURCE [SOURCE...]
54 Convert SOURCE files to %s format.""" % cls.format_name
56 parser = optparse.OptionParser(usage=usage)
59 '-v', '--verbose', action='store_true', dest='verbose',
60 default=False, help='print status messages to stdout')
62 '-d', '--make-dir', action='store_true', dest='make_dir',
64 help='create a directory for author and put the output file in it'
67 '-o', '--output-file', dest='output_file', metavar='FILE',
68 help='specifies the output file')
70 '-O', '--output-dir', dest='output_dir', metavar='DIR',
71 help='specifies the directory for output'
74 if cls.cover_optional:
76 '-c', '--with-cover', action='store_true',
77 dest='with_cover', default=False,
78 help='create default cover'
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 '')
87 + cls.transform_options
88 + cls.transform_flags):
91 options, input_filenames = parser.parse_args()
93 if len(input_filenames) < 1:
97 # Prepare additional args for parser.
99 for option in cls.parser_options:
100 parser_args[option.name()] = option.value(options)
101 # Prepare additional args for transform method.
103 for option in cls.transform_options:
104 transform_args[option.name()] = option.value(options)
105 # Add flags to transform_args, if any.
108 for flag in cls.transform_flags
109 if flag.value(options)
112 transform_args['flags'] = transform_flags
114 transform_args['verbose'] = True
115 # Add cover support, if any.
117 if options.image_cache:
118 def cover_class(book_info, *args, **kwargs):
120 book_info, image_cache=options.image_cache,
123 transform_args['cover'] = cover_class
124 elif not cls.cover_optional or options.with_cover:
125 transform_args['cover'] = make_cover
129 for main_input in input_filenames:
133 if isinstance(main_input, six.binary_type):
134 main_input = main_input.decode('utf-8')
136 # Where to find input?
137 if cls.uses_provider:
138 path, fname = os.path.realpath(main_input).rsplit('/', 1)
139 provider = DirDocProvider(path)
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
147 output_file = options.output_file
149 # Do the transformation.
150 doc = WLDocument.from_file(main_input, provider=provider,
152 transform = cls.transform
153 if transform is None:
154 transform = getattr(WLDocument, 'as_%s' % cls.ext)
155 output = transform(doc, **transform_args)
157 doc.save_output_file(output, output_file, options.output_dir,
158 options.make_dir, cls.ext)
160 except ParseError as e:
161 print('%(file)s:%(name)s:%(message)s' % {
163 'name': e.__class__.__name__,