1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from __future__ import print_function, unicode_literals
9 from librarian import pdf, epub, mobi, DirDocProvider, ParseError
10 from librarian.parser import WLDocument
12 from .util import makedirs
15 class Packager(object):
20 def transform(cls, *args, **kwargs):
21 return cls.converter.transform(*args, **kwargs)
24 def prepare_file(cls, main_input, output_dir, verbose=False, overwrite=False):
25 path, fname = os.path.realpath(main_input).rsplit('/', 1)
26 provider = DirDocProvider(path)
27 slug, ext = os.path.splitext(fname)
31 outfile = os.path.join(output_dir, slug + '.' + cls.ext)
32 if os.path.exists(outfile) and not overwrite:
35 doc = WLDocument.from_file(main_input, provider=provider)
36 output_file = cls.transform(doc, cover=cls.cover, flags=cls.flags)
37 doc.save_output_file(output_file, output_path=outfile)
40 def prepare(cls, input_filenames, output_dir='', verbose=False, overwrite=False):
42 for main_input in input_filenames:
45 cls.prepare_file(main_input, output_dir, verbose, overwrite)
46 except ParseError as e:
47 print('%(file)s:%(name)s:%(message)s' % {
49 'name': e.__class__.__name__,
54 class EpubPackager(Packager):
59 class MobiPackager(Packager):
64 class PdfPackager(Packager):
69 def transform(cls, *args, **kwargs):
70 return cls.converter.transform(*args, morefloats='new', **kwargs)