From 58bca5f56834e7fdffed7c1e9609ced58affa941 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Aug 2011 09:53:14 -0600 Subject: [PATCH] MOBI Output: Add a command line option --extract-to that uses the inspect MOBI tool to extract the created MOBI file to the specified directory --- src/calibre/ebooks/mobi/debug.py | 7 ++++--- src/calibre/ebooks/mobi/output.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/mobi/debug.py b/src/calibre/ebooks/mobi/debug.py index 8034117f9b..47450842d1 100644 --- a/src/calibre/ebooks/mobi/debug.py +++ b/src/calibre/ebooks/mobi/debug.py @@ -1430,16 +1430,17 @@ class MOBIFile(object): # {{{ print (str(self.mobi_header).encode('utf-8'), file=f) # }}} -def inspect_mobi(path_or_stream, prefix='decompiled'): # {{{ +def inspect_mobi(path_or_stream, ddir=None): # {{{ stream = (path_or_stream if hasattr(path_or_stream, 'read') else open(path_or_stream, 'rb')) f = MOBIFile(stream) - ddir = prefix + '_' + os.path.splitext(os.path.basename(stream.name))[0] + if ddir is None: + ddir = 'decompiled_' + os.path.splitext(os.path.basename(stream.name))[0] try: shutil.rmtree(ddir) except: pass - os.mkdir(ddir) + os.makedirs(ddir) with open(os.path.join(ddir, 'header.txt'), 'wb') as out: f.print_header(f=out) diff --git a/src/calibre/ebooks/mobi/output.py b/src/calibre/ebooks/mobi/output.py index aeab8518a9..4451bc66b8 100644 --- a/src/calibre/ebooks/mobi/output.py +++ b/src/calibre/ebooks/mobi/output.py @@ -50,6 +50,11 @@ 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='extract_to', recommended_value=None, + help=_('Extract the contents of the MOBI file to the' + ' specified directory. If the directory already ' + 'exists, it will be deleted.') + ), OptionRecommendation(name='mobi_navpoints_only_deepest', recommended_value=False, help=_('When adding navpoints for the chapter-to-chapter' @@ -185,3 +190,8 @@ class MOBIOutput(OutputFormatPlugin): write_page_breaks_after_item=write_page_breaks_after_item) writer(oeb, output_path) + if opts.extract_to is not None: + from calibre.ebooks.mobi.debug import inspect_mobi + ddir = opts.extract_to + inspect_mobi(output_path, ddir=ddir) +