1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 
   6 from tempfile import NamedTemporaryFile
 
   7 from librarian import functions, get_resource, OutputFile
 
   8 from librarian.hyphenator import Hyphenator
 
   9 from .epub import EpubBuilder
 
  12 class MobiBuilder(EpubBuilder):
 
  13     file_extension = 'mobi'
 
  14     isbn_field = 'isbn_mobi'
 
  16     def build(self, document, use_kindlegen=False, converter_path=None, **kwargs):
 
  17         bibl_lng = document.meta.language
 
  18         short_lng = functions.lang_code_3to2(bibl_lng)
 
  20             self.hyphenator = Hyphenator(get_resource('res/hyph-dictionaries/hyph_' +
 
  25         epub = super().build(document, **kwargs)
 
  27         devnull = open("/dev/null", 'w')
 
  28         gen_kwargs = {"stdout": devnull, "stderr": devnull}
 
  30         output_file = NamedTemporaryFile(prefix='librarian', suffix='.mobi',
 
  35             output_file_basename = os.path.basename(output_file.name)
 
  36             subprocess.check_call([converter_path or 'kindlegen',
 
  37                                '-c2', epub.get_filename(),
 
  38                                '-o', output_file_basename], **gen_kwargs)
 
  40             subprocess.check_call([converter_path or 'ebook-convert',
 
  42                                output_file.name, '--no-inline-toc',
 
  43                                '--mobi-file-type=both',
 
  44                                '--mobi-ignore-margins',
 
  47         return OutputFile.from_filename(output_file.name)