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 collections import namedtuple
11 from librarian import ParseError
12 from librarian.document import Document
16 """Option for optparse. Use it like `optparse.OptionParser.add_option`."""
17 def __init__(self, *names, **options):
19 self.options = options
21 def add(self, parser):
22 parser.add_option(*self.names, **self.options)
25 return self.options['dest']
27 def value(self, options):
28 return getattr(options, self.name())
31 class Book2Anything(object):
32 """A class for creating book2... scripts.
34 Subclass it for any format you want to convert to.
36 format_cls = None # A formats.Format subclass
37 document_options = [] # List of Option objects for document options.
38 format_options = [] # List of Option objects for format customization.
39 build_options = [] # List of Option objects for build options.
43 # Parse commandline arguments
44 usage = """Usage: %%prog [options] SOURCE [SOURCE...]
45 Convert SOURCE files to %s.""" % cls.format_cls.format_name
47 parser = optparse.OptionParser(usage=usage)
49 parser.add_option('-v', '--verbose',
50 action='store_true', dest='verbose', default=False,
51 help='print status messages to stdout')
52 parser.add_option('-o', '--output-file',
53 dest='output_file', metavar='FILE',
54 help='specifies the output file')
55 for option in cls.document_options + cls.format_options + cls.build_options:
58 options, input_filenames = parser.parse_args()
60 if len(input_filenames) < 1:
64 # Prepare additional args for document.
66 for option in cls.document_options:
67 document_args[option.name()] = option.value(options)
68 # Prepare additional args for format.
70 for option in cls.format_options:
71 format_args[option.name()] = option.value(options)
72 # Prepare additional args for build.
74 for option in cls.build_options:
75 build_args[option.name()] = option.value(options)
79 for main_input in input_filenames:
83 # Do the transformation.
84 doc = Document.from_file(main_input, **document_args)
85 format_ = cls.format_cls(doc, **format_args)
87 # Where to write output?
88 if not options.output_file:
89 output_file = os.path.splitext(main_input)[0] + '.' + format_.format_ext
93 output = format_.build(**build_args)
94 output.save_as(output_file)
97 print '%(file)s:%(name)s:%(message)s' % {
99 'name': e.__class__.__name__,