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.
9 from tempfile import NamedTemporaryFile
10 from lxml import etree
12 from librarian.cover import WLCover
13 from librarian import epub, get_resource, NoDublinCore, RDFNS
14 from librarian.dcparser import BookInfo
17 def transform(provider, slug=None, file_path=None, output_file=None, output_dir=None, make_dir=False, verbose=False,
18 sample=None, cover=None, flags=None):
19 """ produces a MOBI file
21 provider: a DocProvider
22 slug: slug of file to process, available by provider
23 output_file: path to output file
24 output_dir: path to directory to save output file to; either this or output_file must be present
25 make_dir: writes output to <output_dir>/<author>/<slug>.mobi instead of <output_dir>/<slug>.mobi
26 sample=n: generate sample e-book (with at least n paragraphs)
27 cover: a cover.Cover object
28 flags: less-advertising,
31 # read metadata from the first file
34 raise ValueError('slug or file_path should be specified, not both')
35 f = open(file_path, 'r')
36 input_xml = etree.parse(f)
40 raise ValueError('either slug or file_path should be specified')
41 input_xml = etree.parse(provider[slug])
43 metadata = input_xml.find('.//'+RDFNS('Description'))
45 raise NoDublinCore('Document has no DublinCore - which is required.')
46 book_info = BookInfo.from_element(input_xml)
48 # if output to dir, create the file
49 if output_dir is not None:
51 author = unicode(book_info.author)
52 output_dir = os.path.join(output_dir, author)
54 os.makedirs(output_dir)
58 output_file = os.path.join(output_dir, '%s.mobi' % slug)
60 output_file = os.path.join(output_dir, os.path.splitext(os.path.basename(file_path))[0] + '.mobi')
62 # provide a cover by default
65 cover_file = NamedTemporaryFile(suffix='.png', delete=False)
66 c = cover(book_info.author.readable(), book_info.title)
69 epub_file = NamedTemporaryFile(suffix='.epub', delete=False)
72 flags = list(flags) + ['without-fonts']
73 epub.transform(provider, file_path=file_path, output_file=epub_file, verbose=verbose,
74 sample=sample, html_toc=True, flags=flags, style=get_resource('mobi/style.css'))
79 devnull = open("/dev/null", 'w')
80 kwargs = {"stdout": devnull, "stderr": devnull}
81 subprocess.check_call(['ebook-convert', epub_file.name, output_file,
82 '--no-inline-toc', '--cover=%s' % cover_file.name], **kwargs)
83 os.unlink(epub_file.name)
84 os.unlink(cover_file.name)