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, COVER_CLASSES
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 '')
86 '--cover-class', dest='cover_class',
87 help='cover class name'
91 + cls.transform_options
92 + cls.transform_flags):
95 options, input_filenames = parser.parse_args()
97 if len(input_filenames) < 1:
101 # Prepare additional args for parser.
103 for option in cls.parser_options:
104 parser_args[option.name()] = option.value(options)
105 # Prepare additional args for transform method.
107 for option in cls.transform_options:
108 transform_args[option.name()] = option.value(options)
109 # Add flags to transform_args, if any.
112 for flag in cls.transform_flags
113 if flag.value(options)
116 transform_args['flags'] = transform_flags
118 transform_args['verbose'] = True
119 # Add cover support, if any.
121 if options.image_cache:
122 def cover_class(book_info, *args, **kwargs):
124 book_info, image_cache=options.image_cache,
125 cover_class=options.cover_class,
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
136 for main_input in input_filenames:
140 if isinstance(main_input, six.binary_type):
141 main_input = main_input.decode('utf-8')
143 # Where to find input?
144 if cls.uses_provider:
145 path, fname = os.path.realpath(main_input).rsplit('/', 1)
146 provider = DirDocProvider(path)
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
154 output_file = options.output_file
156 # Do the transformation.
157 doc = WLDocument.from_file(main_input, provider=provider,
159 transform = cls.transform
160 if transform is None:
161 transform = getattr(WLDocument, 'as_%s' % cls.ext)
162 output = transform(doc, **transform_args)
164 doc.save_output_file(output, output_file, options.output_dir,
165 options.make_dir, cls.ext)
167 except ParseError as e:
168 print('%(file)s:%(name)s:%(message)s' % {
170 'name': e.__class__.__name__,