4 from tempfile import NamedTemporaryFile
5 from librarian import functions, get_resource, OutputFile
6 from librarian.hyphenator import Hyphenator
7 from .epub import EpubBuilder
10 class MobiBuilder(EpubBuilder):
11 file_extension = 'mobi'
12 isbn_field = 'isbn_mobi'
14 def build(self, document, use_kindlegen=False, converter_path=None, **kwargs):
15 bibl_lng = document.meta.language
16 short_lng = functions.lang_code_3to2(bibl_lng)
18 self.hyphenator = Hyphenator(get_resource('res/hyph-dictionaries/hyph_' +
23 epub = super().build(document, **kwargs)
25 devnull = open("/dev/null", 'w')
26 gen_kwargs = {"stdout": devnull, "stderr": devnull}
28 output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi',
33 output_file_basename = os.path.basename(output_file.name)
34 subprocess.check_call([converter_path or 'kindlegen',
35 '-c2', epub.get_filename(),
36 '-o', output_file_basename], **gen_kwargs)
38 subprocess.check_call([converter_path or 'ebook-convert',
40 output_file.name, '--no-inline-toc',
41 '--mobi-file-type=both',
42 '--mobi-ignore-margins',
44 return OutputFile.from_filename(output_file.name)