From c1967a935d9cd2654c83d1e455a5fd1525ae91ae Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Jul 2011 23:21:10 -0600 Subject: [PATCH] ... --- src/calibre/ebooks/mobi/kindlegen.py | 19 ++++++++++--------- src/calibre/ebooks/mobi/output.py | 4 +++- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/calibre/ebooks/mobi/kindlegen.py b/src/calibre/ebooks/mobi/kindlegen.py index b1a64f86af..7c626a2978 100644 --- a/src/calibre/ebooks/mobi/kindlegen.py +++ b/src/calibre/ebooks/mobi/kindlegen.py @@ -19,7 +19,7 @@ from calibre import CurrentDir exe = 'kindlegen.exe' if iswindows else 'kindlegen' -def refactor_opf(opf, is_periodical): +def refactor_opf(opf, is_periodical, toc): with open(opf, 'rb') as f: root = etree.fromstring(f.read()) if is_periodical: @@ -27,11 +27,11 @@ def refactor_opf(opf, is_periodical): xm = etree.SubElement(metadata, 'x-metadata') xm.tail = '\n' xm.text = '\n\t' - mobip = etree.SubElement(xm, attrib={'encoding':"utf-8", + mobip = etree.SubElement(xm, 'output', attrib={'encoding':"utf-8", 'content-type':"application/x-mobipocket-subscription-magazine"}) mobip.tail = '\n' with open(opf, 'wb') as f: - f.write(etree.tostring(root, method='xml', encodin='utf-8', + f.write(etree.tostring(root, method='xml', encoding='utf-8', xml_declaration=True)) @@ -43,7 +43,7 @@ def refactor_guide(oeb): def run_kindlegen(opf, log): log.info('Running kindlegen on MOBIML created by calibre') oname = os.path.splitext(opf)[0] + '.mobi' - with tempfile.NamedTemporaryFile('_kindlegen_output.txt') as tfile: + with tempfile.NamedTemporaryFile(suffix='_kindlegen_output.txt') as tfile: p = subprocess.Popen([exe, opf, '-c1', '-verbose', '-o', oname], stderr=subprocess.STDOUT, stdout=tfile) returncode = p.wait() @@ -63,11 +63,7 @@ def kindlegen(oeb, opts, input_plugin, output_path): oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb, tdir, input_plugin, opts, oeb.log) opf = [x for x in os.listdir(tdir) if x.endswith('.opf')][0] - refactor_opf(opf, is_periodical) - with CurrentDir(tdir): - oname = run_kindlegen(opf, oeb.log) - shutil.copyfile(oname, output_path) - + refactor_opf(os.path.join(tdir, opf), is_periodical, oeb.toc) try: if os.path.exists('/tmp/kindlegen'): shutil.rmtree('/tmp/kindlegen') @@ -76,3 +72,8 @@ def kindlegen(oeb, opts, input_plugin, output_path): except: pass + with CurrentDir(tdir): + oname = run_kindlegen(opf, oeb.log) + shutil.copyfile(oname, output_path) + + diff --git a/src/calibre/ebooks/mobi/output.py b/src/calibre/ebooks/mobi/output.py index cc526e0976..35da368155 100644 --- a/src/calibre/ebooks/mobi/output.py +++ b/src/calibre/ebooks/mobi/output.py @@ -50,12 +50,14 @@ class MOBIOutput(OutputFormatPlugin): help=_('When adding the Table of Contents to the book, add it at the start of the ' 'book instead of the end. Not recommended.') ), + ''' OptionRecommendation(name='kindlegen', recommended_value=False, - help=_('Use kindlegen (must be in your PATH) to generate the' + help=('Use kindlegen (must be in your PATH) to generate the' ' binary wrapper for the MOBI format. Useful to debug ' ' the calibre MOBI output.') ), + ''' ])