X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/9868276112ecaa2081f36b9304f6d10492a2baed..318d79c6ec02163ec898a66723055da25837346a:/librarian/book2anything.py?ds=sidebyside diff --git a/librarian/book2anything.py b/librarian/book2anything.py index 7ae6178..948d9fd 100755 --- a/librarian/book2anything.py +++ b/librarian/book2anything.py @@ -4,13 +4,14 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from collections import namedtuple +from __future__ import print_function, unicode_literals + import os.path import optparse - +import six from librarian import DirDocProvider, ParseError from librarian.parser import WLDocument -from librarian.cover import DefaultEbookCover +from librarian.cover import make_cover class Option(object): @@ -34,16 +35,15 @@ class Book2Anything(object): Subclass it for any format you want to convert to. """ - format_name = None # Set format name, like "PDF". - ext = None # Set file extension, like "pdf". - uses_cover = False # Can it add a cover? - cover_optional = True # Only relevant if uses_cover - uses_provider = False # Does it need a DocProvider? - transform = None # Transform method. Uses WLDocument.as_{ext} by default. - parser_options = [] # List of Option objects for additional parser args. - transform_options = [] # List of Option objects for additional transform args. - transform_flags = [] # List of Option objects for supported transform flags. - + format_name = None # Set format name, like "PDF". + ext = None # Set file extension, like "pdf". + uses_cover = False # Can it add a cover? + cover_optional = True # Only relevant if uses_cover + uses_provider = False # Does it need a DocProvider? + transform = None # Transform method. Uses WLDocument.as_{ext} by default. + parser_options = [] # List of Option objects for additional parser args. + transform_options = [] # List of Option objects for additional transform args. + transform_flags = [] # List of Option objects for supported transform flags. @classmethod def run(cls): @@ -53,33 +53,21 @@ class Book2Anything(object): parser = optparse.OptionParser(usage=usage) - parser.add_option('-v', '--verbose', - action='store_true', dest='verbose', default=False, - help='print status messages to stdout') - parser.add_option('-t', '--html-toc', - action='store_true', dest='html_toc', default=False, - help='with inline html toc [book2epub only]') - parser.add_option('-k', '--use-kindlegen', - action='store_true', dest='use_kindlegen', default=False, - help='use kindlegen tool [book2mobi only]') - parser.add_option('-d', '--make-dir', - action='store_true', dest='make_dir', default=False, - help='create a directory for author and put the output file in it') - parser.add_option('-o', '--output-file', - dest='output_file', metavar='FILE', - help='specifies the output file') - parser.add_option('-O', '--output-dir', - dest='output_dir', metavar='DIR', - help='specifies the directory for output') + parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, + help='print status messages to stdout') + parser.add_option('-d', '--make-dir', action='store_true', dest='make_dir', default=False, + help='create a directory for author and put the output file in it') + parser.add_option('-o', '--output-file', dest='output_file', metavar='FILE', + help='specifies the output file') + parser.add_option('-O', '--output-dir', dest='output_dir', metavar='DIR', + help='specifies the directory for output') if cls.uses_cover: if cls.cover_optional: - parser.add_option('-c', '--with-cover', - action='store_true', dest='with_cover', default=False, - help='create default cover') - parser.add_option('-C', '--image-cache', - dest='image_cache', metavar='URL', - help='prefix for image download cache' + - (' (implies --with-cover)' if cls.cover_optional else '')) + parser.add_option('-c', '--with-cover', action='store_true', dest='with_cover', default=False, + help='create default cover') + parser.add_option('-C', '--image-cache', dest='image_cache', metavar='URL', + help='prefix for image download cache' + + (' (implies --with-cover)' if cls.cover_optional else '')) for option in cls.parser_options + cls.transform_options + cls.transform_flags: option.add(parser) @@ -87,7 +75,7 @@ class Book2Anything(object): if len(input_filenames) < 1: parser.print_help() - return(1) + return 1 # Prepare additional args for parser. parser_args = {} @@ -98,31 +86,28 @@ class Book2Anything(object): for option in cls.transform_options: transform_args[option.name()] = option.value(options) # Add flags to transform_args, if any. - transform_flags = [flag.name() for flag in cls.transform_flags - if flag.value(options)] + transform_flags = [flag.name() for flag in cls.transform_flags if flag.value(options)] if transform_flags: transform_args['flags'] = transform_flags if options.verbose: - transform_args['verbose'] = True - if options.html_toc and cls.ext == 'epub': - transform_args['html_toc'] = True - if options.use_kindlegen and cls.ext == 'mobi': - transform_args['use_kindlegen'] = True + transform_args['verbose'] = True # Add cover support, if any. if cls.uses_cover: if options.image_cache: - def cover_class(*args, **kwargs): - return DefaultEbookCover(image_cache=options.image_cache, *args, **kwargs) + def cover_class(book_info, *args, **kwargs): + return make_cover(book_info, image_cache=options.image_cache, *args, **kwargs) transform_args['cover'] = cover_class elif not cls.cover_optional or options.with_cover: - transform_args['cover'] = DefaultEbookCover - + transform_args['cover'] = make_cover # Do some real work try: for main_input in input_filenames: if options.verbose: - print main_input + print(main_input) + + if isinstance(main_input, six.binary_type): + main_input = main_input.decode('utf-8') # Where to find input? if cls.uses_provider: @@ -135,7 +120,7 @@ class Book2Anything(object): if not (options.output_file or options.output_dir): output_file = os.path.splitext(main_input)[0] + '.' + cls.ext else: - output_file = None + output_file = options.output_file # Do the transformation. doc = WLDocument.from_file(main_input, provider=provider, **parser_args) @@ -144,12 +129,11 @@ class Book2Anything(object): transform = getattr(WLDocument, 'as_%s' % cls.ext) output = transform(doc, **transform_args) - doc.save_output_file(output, - output_file, options.output_dir, options.make_dir, cls.ext) + doc.save_output_file(output, output_file, options.output_dir, options.make_dir, cls.ext) - except ParseError, e: - print '%(file)s:%(name)s:%(message)s' % { + except ParseError as e: + print('%(file)s:%(name)s:%(message)s' % { 'file': main_input, 'name': e.__class__.__name__, 'message': e - } + })