From fc4f0f43794408ae027010262805733dd5b439bd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 10 Jun 2013 06:13:05 +0530 Subject: [PATCH] Dump mobi and docx easily with calibre-debug You can now directly run calibre-debug file.mobi or calibre-debug file.docx to dump the contents of the file for debugging/inspection. --- src/calibre/debug.py | 27 +++++++++++++++++++++------ src/calibre/ebooks/docx/dump.py | 2 +- src/calibre/linux.py | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/calibre/debug.py b/src/calibre/debug.py index 6ccaf750e2..7a1fe754fa 100644 --- a/src/calibre/debug.py +++ b/src/calibre/debug.py @@ -152,7 +152,8 @@ def add_simple_plugin(path_to_plugin): shutil.rmtree(tdir) def print_basic_debug_info(out=None): - if out is None: out = sys.stdout + if out is None: + out = sys.stdout out = functools.partial(prints, file=out) import platform from calibre.constants import (__appname__, get_version, isportable, isosx, @@ -175,7 +176,7 @@ def print_basic_debug_info(out=None): def run_debug_gui(logpath): import time - time.sleep(3) # Give previous GUI time to shutdown fully and release locks + time.sleep(3) # Give previous GUI time to shutdown fully and release locks from calibre.constants import __appname__ prints(__appname__, _('Debug log')) print_basic_debug_info() @@ -197,6 +198,12 @@ def run_script(path, args): g['__file__'] = ef execfile(ef, g) +def inspect_mobi(path): + from calibre.ebooks.mobi.debug.main import inspect_mobi + prints('Inspecting:', path) + inspect_mobi(path) + print + def main(args=sys.argv): from calibre.constants import debug debug() @@ -231,7 +238,7 @@ def main(args=sys.argv): main() elif opts.command: sys.argv = args - exec opts.command + exec(opts.command) elif opts.debug_device_driver: debug_device_driver() elif opts.add_simple_plugin is not None: @@ -246,11 +253,8 @@ def main(args=sys.argv): sql_dump = args[-1] reinit_db(opts.reinitialize_db, sql_dump=sql_dump) elif opts.inspect_mobi: - from calibre.ebooks.mobi.debug.main import inspect_mobi for path in args[1:]: - prints('Inspecting:', path) inspect_mobi(path) - print elif opts.tweak_book: from calibre.ebooks.tweak import tweak tweak(opts.tweak_book) @@ -274,6 +278,16 @@ def main(args=sys.argv): plugin.cli_main([plugin.name] + args[1:]) elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}: run_script(args[1], args[2:]) + elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'mobi', 'azw', 'azw3', 'docx'}: + for path in args[1:]: + ext = path.rpartition('.')[-1] + if ext == 'docx': + from calibre.ebooks.docx.dump import dump + dump(path) + elif ext in {'mobi', 'azw', 'azw3'}: + inspect_mobi(path) + else: + print ('Cannot dump unknown filetype: %s' % path) else: from calibre import ipython ipython() @@ -282,3 +296,4 @@ def main(args=sys.argv): if __name__ == '__main__': sys.exit(main()) + diff --git a/src/calibre/ebooks/docx/dump.py b/src/calibre/ebooks/docx/dump.py index 6ebc2e8871..103236f05f 100644 --- a/src/calibre/ebooks/docx/dump.py +++ b/src/calibre/ebooks/docx/dump.py @@ -15,7 +15,7 @@ from calibre.utils.zipfile import ZipFile def dump(path): dest = os.path.splitext(os.path.basename(path))[0] - dest += '_extracted' + dest += '-dumped' if os.path.exists(dest): shutil.rmtree(dest) with ZipFile(path) as zf: diff --git a/src/calibre/linux.py b/src/calibre/linux.py index d2b0c941a6..04bb471feb 100644 --- a/src/calibre/linux.py +++ b/src/calibre/linux.py @@ -542,7 +542,7 @@ class PostInstall: o_and_w('fetch-ebook-metadata', fem_op, []) o_and_w('calibre-smtp', smtp_op, []) o_and_w('calibre-server', serv_op, []) - o_and_e('calibre-debug', debug_op, ['py', 'recipe'], file_map={ + o_and_e('calibre-debug', debug_op, ['py', 'recipe', 'mobi', 'azw', 'azw3', 'docx'], file_map={ '--tweak-book':['epub', 'azw3', 'mobi'], '--subset-font':['ttf', 'otf'], '--exec-file':['py', 'recipe'],