move stats to L2
[librarian.git] / src / librarian / book2anything.py
old mode 100755 (executable)
new mode 100644 (file)
index 948d9fd..b9e2a33
@@ -1,20 +1,14 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-#
 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
-# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 #
-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 make_cover
+from librarian.cover import make_cover, COVER_CLASSES
 
 
-class Option(object):
+class Option:
     """Option for optparse. Use it like `optparse.OptionParser.add_option`."""
     def __init__(self, *names, **options):
         self.names = names
@@ -30,9 +24,9 @@ class Option(object):
         return getattr(options, self.name())
 
 
-class Book2Anything(object):
+class Book2Anything:
     """A class for creating book2... scripts.
-    
+
     Subclass it for any format you want to convert to.
     """
     format_name = None  # Set format name, like "PDF".
@@ -42,8 +36,10 @@ class Book2Anything(object):
     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.
+    # List of Option objects for additional transform args.
+    transform_options = []
+    # List of Option objects for supported transform flags.
+    transform_flags = []
 
     @classmethod
     def run(cls):
@@ -53,22 +49,41 @@ 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('-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 ''))
-        for option in cls.parser_options + cls.transform_options + cls.transform_flags:
+                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(
+                '--cover-class', dest='cover_class',
+                help='cover class name'
+            )
+        for option in (
+                cls.parser_options
+                + cls.transform_options
+                + cls.transform_flags):
             option.add(parser)
 
         options, input_filenames = parser.parse_args()
@@ -86,7 +101,11 @@ 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:
@@ -95,10 +114,16 @@ class Book2Anything(object):
         if cls.uses_cover:
             if options.image_cache:
                 def cover_class(book_info, *args, **kwargs):
-                    return make_cover(book_info, image_cache=options.image_cache, *args, **kwargs)
+                    return make_cover(
+                        book_info, image_cache=options.image_cache,
+                        cover_class=options.cover_class,
+                        *args, **kwargs
+                    )
                 transform_args['cover'] = cover_class
             elif not cls.cover_optional or options.with_cover:
-                transform_args['cover'] = make_cover
+                cover_class = COVER_CLASSES.get(
+                    options.cover_class, make_cover)
+                transform_args['cover'] = cover_class
 
         # Do some real work
         try:
@@ -106,7 +131,7 @@ class Book2Anything(object):
                 if options.verbose:
                     print(main_input)
 
-            if isinstance(main_input, six.binary_type):
+            if isinstance(main_input, bytes):
                 main_input = main_input.decode('utf-8')
 
             # Where to find input?
@@ -123,13 +148,15 @@ class Book2Anything(object):
                 output_file = options.output_file
 
             # Do the transformation.
-            doc = WLDocument.from_file(main_input, provider=provider, **parser_args)
+            doc = WLDocument.from_file(main_input, provider=provider,
+                                       **parser_args)
             transform = cls.transform
             if transform is None:
                 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 as e:
             print('%(file)s:%(name)s:%(message)s' % {