Refactor polishing code so it can be used from within Tweak Book

This commit is contained in:
Kovid Goyal 2013-11-08 13:52:32 +05:30
parent e00615d89d
commit 45edfa442c

View File

@ -119,12 +119,8 @@ def update_metadata(ebook, new_opf):
stream.truncate()
stream.write(opf.render())
def polish(file_map, opts, log, report):
def polish_one(ebook, opts, report):
rt = lambda x: report('\n### ' + x)
st = time.time()
for inbook, outbook in file_map.iteritems():
report(_('## Polishing: %s')%(inbook.rpartition('.')[-1].upper()))
ebook = get_container(inbook, log)
jacket = None
if opts.subset or opts.embed:
@ -178,6 +174,13 @@ def polish(file_map, opts, log, report):
subset_all_fonts(ebook, stats.font_stats, report)
report('')
def polish(file_map, opts, log, report):
st = time.time()
for inbook, outbook in file_map.iteritems():
report(_('## Polishing: %s')%(inbook.rpartition('.')[-1].upper()))
ebook = get_container(inbook, log)
polish_one(ebook, opts, report)
ebook.commit(outbook)
report('-'*70)
report(_('Polishing took: %.1f seconds')%(time.time()-st))
@ -204,6 +207,15 @@ def gui_polish(data):
log(msg)
return '\n\n'.join(report)
def tweak_polish(container, actions):
opts = ALL_OPTS.copy()
opts.update(actions)
O = namedtuple('Options', ' '.join(ALL_OPTS.iterkeys()))
opts = O(**opts)
report = []
polish_one(container, opts, report.append)
return report
def option_parser():
from calibre.utils.config import OptionParser
USAGE = '%prog [options] input_file [output_file]\n\n' + re.sub(