From 1da9d93ec6b819a2b90bfd7de9adbd3f6049d884 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 8 Sep 2019 21:25:35 -0700 Subject: [PATCH] Add command exec. --- epubfile.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/epubfile.py b/epubfile.py index de6a4ba..3910a9a 100644 --- a/epubfile.py +++ b/epubfile.py @@ -1152,12 +1152,20 @@ covercomesfirst: first, otherwise some /a/image.jpg will always be before /images/cover.jpg. '''.strip(), +'exec': +''' +exec: + Execute your own Python code against the book. + + > epubfile.py exec book.epub --command "book._____()" +'''.strip(), + 'generate_toc': ''' generate_toc: Regenerate the toc.ncx and nav.xhtml based on headers in the files. - > epubfile.py generate_toc book.epub epubfile.py generate_toc book.epub flags: --max_level X: @@ -1271,6 +1279,14 @@ def covercomesfirst_argparse(args): book = Epub.open(epub) covercomesfirst(book) +def exec_argparse(args): + epubs = [epub for pattern in args.epubs for epub in glob.glob(pattern)] + for epub in epubs: + print(epub) + book = Epub.open(epub) + exec(args.command) + book.save(epub) + def generate_toc_argparse(args): epubs = [epub for pattern in args.epubs for epub in glob.glob(pattern)] books = [] @@ -1318,7 +1334,7 @@ def merge(input_filepaths, output_filename, do_headerfile=False): new_basename = prefix.format(old_basename) basename_map[id] = new_basename - # Don't worry, we're not going to save this! + # Don't worry, we're not going to save over the input book! input_book.rename_file(basename_map) if do_headerfile: @@ -1381,6 +1397,11 @@ def main(argv): p_covercomesfirst.add_argument('epubs', nargs='+', default=[]) p_covercomesfirst.set_defaults(func=covercomesfirst_argparse) + p_exec = subparsers.add_parser('exec') + p_exec.add_argument('epubs', nargs='+', default=[]) + p_exec.add_argument('--command', dest='command', default=None, required=True) + p_exec.set_defaults(func=exec_argparse) + p_generate_toc = subparsers.add_parser('generate_toc') p_generate_toc.add_argument('epubs', nargs='+', default=[]) p_generate_toc.add_argument('--max_level', dest='max_level', default=None)