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

This commit is contained in:
Kovid Goyal 2011-08-01 09:53:14 -06:00
parent b615dffb53
commit 58bca5f568
2 changed files with 14 additions and 3 deletions

View File

@ -1430,16 +1430,17 @@ class MOBIFile(object): # {{{
print (str(self.mobi_header).encode('utf-8'), file=f) 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 stream = (path_or_stream if hasattr(path_or_stream, 'read') else
open(path_or_stream, 'rb')) open(path_or_stream, 'rb'))
f = MOBIFile(stream) 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: try:
shutil.rmtree(ddir) shutil.rmtree(ddir)
except: except:
pass pass
os.mkdir(ddir) os.makedirs(ddir)
with open(os.path.join(ddir, 'header.txt'), 'wb') as out: with open(os.path.join(ddir, 'header.txt'), 'wb') as out:
f.print_header(f=out) f.print_header(f=out)

View File

@ -50,6 +50,11 @@ class MOBIOutput(OutputFormatPlugin):
help=_('When adding the Table of Contents to the book, add it at the start of the ' help=_('When adding the Table of Contents to the book, add it at the start of the '
'book instead of the end. Not recommended.') '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', OptionRecommendation(name='mobi_navpoints_only_deepest',
recommended_value=False, recommended_value=False,
help=_('When adding navpoints for the chapter-to-chapter' 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) write_page_breaks_after_item=write_page_breaks_after_item)
writer(oeb, output_path) 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)