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.
This commit is contained in:
Kovid Goyal 2013-06-10 06:13:05 +05:30
parent 773b7d4127
commit fc4f0f4379
3 changed files with 23 additions and 8 deletions

View File

@ -152,7 +152,8 @@ def add_simple_plugin(path_to_plugin):
shutil.rmtree(tdir) shutil.rmtree(tdir)
def print_basic_debug_info(out=None): 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) out = functools.partial(prints, file=out)
import platform import platform
from calibre.constants import (__appname__, get_version, isportable, isosx, from calibre.constants import (__appname__, get_version, isportable, isosx,
@ -197,6 +198,12 @@ def run_script(path, args):
g['__file__'] = ef g['__file__'] = ef
execfile(ef, g) 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): def main(args=sys.argv):
from calibre.constants import debug from calibre.constants import debug
debug() debug()
@ -231,7 +238,7 @@ def main(args=sys.argv):
main() main()
elif opts.command: elif opts.command:
sys.argv = args sys.argv = args
exec opts.command exec(opts.command)
elif opts.debug_device_driver: elif opts.debug_device_driver:
debug_device_driver() debug_device_driver()
elif opts.add_simple_plugin is not None: elif opts.add_simple_plugin is not None:
@ -246,11 +253,8 @@ def main(args=sys.argv):
sql_dump = args[-1] sql_dump = args[-1]
reinit_db(opts.reinitialize_db, sql_dump=sql_dump) reinit_db(opts.reinitialize_db, sql_dump=sql_dump)
elif opts.inspect_mobi: elif opts.inspect_mobi:
from calibre.ebooks.mobi.debug.main import inspect_mobi
for path in args[1:]: for path in args[1:]:
prints('Inspecting:', path)
inspect_mobi(path) inspect_mobi(path)
print
elif opts.tweak_book: elif opts.tweak_book:
from calibre.ebooks.tweak import tweak from calibre.ebooks.tweak import tweak
tweak(opts.tweak_book) tweak(opts.tweak_book)
@ -274,6 +278,16 @@ def main(args=sys.argv):
plugin.cli_main([plugin.name] + args[1:]) plugin.cli_main([plugin.name] + args[1:])
elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}: elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}:
run_script(args[1], args[2:]) 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: else:
from calibre import ipython from calibre import ipython
ipython() ipython()
@ -282,3 +296,4 @@ def main(args=sys.argv):
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())

View File

@ -15,7 +15,7 @@ from calibre.utils.zipfile import ZipFile
def dump(path): def dump(path):
dest = os.path.splitext(os.path.basename(path))[0] dest = os.path.splitext(os.path.basename(path))[0]
dest += '_extracted' dest += '-dumped'
if os.path.exists(dest): if os.path.exists(dest):
shutil.rmtree(dest) shutil.rmtree(dest)
with ZipFile(path) as zf: with ZipFile(path) as zf:

View File

@ -542,7 +542,7 @@ class PostInstall:
o_and_w('fetch-ebook-metadata', fem_op, []) o_and_w('fetch-ebook-metadata', fem_op, [])
o_and_w('calibre-smtp', smtp_op, []) o_and_w('calibre-smtp', smtp_op, [])
o_and_w('calibre-server', serv_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'], '--tweak-book':['epub', 'azw3', 'mobi'],
'--subset-font':['ttf', 'otf'], '--subset-font':['ttf', 'otf'],
'--exec-file':['py', 'recipe'], '--exec-file':['py', 'recipe'],