From 7afb9fcb7a864c1c0e4882d5cfd27deec5db3d96 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 30 Jan 2020 23:41:42 -0800 Subject: [PATCH] Use betterhelp to manage docstrings. --- epubfile.py | 113 ++++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 57 deletions(-) diff --git a/epubfile.py b/epubfile.py index 4fe8c24..cf3c119 100644 --- a/epubfile.py +++ b/epubfile.py @@ -1175,6 +1175,9 @@ from voussoirkit import betterhelp from voussoirkit import winglob DOCSTRING = ''' +Epubfile +The simple python .epub scripting tool. + {addfile} {covercomesfirst} @@ -1186,19 +1189,20 @@ DOCSTRING = ''' {merge} {normalize} + +TO SEE DETAILS ON EACH COMMAND, RUN +> epubfile.py '''.lstrip() -SUB_DOCSTRINGS = { -'addfile': -''' +SUB_DOCSTRINGS = dict( +addfile=''' addfile: Add files into the book. > epubfile.py addfile book.epub page1.html image.jpg '''.strip(), -'covercomesfirst': -''' +covercomesfirst=''' covercomesfirst: Rename the cover image file so that it is the alphabetically-first image. @@ -1213,16 +1217,14 @@ covercomesfirst: first, otherwise some /a/image.jpg will always be before /images/cover.jpg. '''.strip(), -'exec': -''' +exec=''' exec: Execute your own Python code against the book. > epubfile.py exec book.epub --command "book._____()" '''.strip(), -'generate_toc': -''' +generate_toc=''' generate_toc: Regenerate the toc.ncx and nav.xhtml based on headers in the files. @@ -1234,16 +1236,14 @@ generate_toc: That is, h1, h2, ... hX. '''.strip(), -'holdit': -''' +holdit=''' holdit: Extract the book and leave it open for manual editing, then save. > epubfile.py holdit book.epub '''.strip(), -'merge': -''' +merge=''' merge: Merge multiple books into one. @@ -1261,8 +1261,7 @@ merge: Overwrite the output file without prompting. '''.strip(), -'normalize': -''' +normalize=''' normalize: Rename files and directories in the book to match a common structure. @@ -1270,8 +1269,8 @@ normalize: subdirectories by type: Text, Images, Styles, etc. > epubfile.py normalize book.epub -'''.strip() -} +'''.strip(), +) DOCSTRING = betterhelp.add_previews(DOCSTRING, SUB_DOCSTRINGS) @@ -1470,47 +1469,47 @@ def normalize_argparse(args): book.move_nav_to_end() book.save(epub) -@betterhelp.subparser_betterhelp(main_docstring=DOCSTRING, sub_docstrings=SUB_DOCSTRINGS) +parser = argparse.ArgumentParser(description=__doc__) +subparsers = parser.add_subparsers() + +p_addfile = subparsers.add_parser('addfile') +p_addfile.add_argument('epub') +p_addfile.add_argument('files', nargs='+', default=[]) +p_addfile.set_defaults(func=addfile_argparse) + +p_covercomesfirst = subparsers.add_parser('covercomesfirst') +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) +p_generate_toc.set_defaults(func=generate_toc_argparse) + +p_holdit = subparsers.add_parser('holdit') +p_holdit.add_argument('epubs', nargs='+', default=[]) +p_holdit.set_defaults(func=holdit_argparse) + +p_merge = subparsers.add_parser('merge') +p_merge.add_argument('epubs', nargs='+', default=[]) +p_merge.add_argument('--output', dest='output', default=None, required=True) +p_merge.add_argument('--headerfile', dest='headerfile', action='store_true') +p_merge.add_argument('--demote_headers', dest='demote_headers', action='store_true') +p_merge.add_argument('--number_headerfile', dest='number_headerfile', action='store_true') +p_merge.add_argument('-y', '--autoyes', dest='autoyes', action='store_true') +p_merge.set_defaults(func=merge_argparse) + +p_normalize = subparsers.add_parser('normalize') +p_normalize.add_argument('epubs', nargs='+', default=[]) +p_normalize.set_defaults(func=normalize_argparse) + +@betterhelp.subparser_betterhelp(parser, main_docstring=DOCSTRING, sub_docstrings=SUB_DOCSTRINGS) def main(argv): - parser = argparse.ArgumentParser(description=__doc__) - subparsers = parser.add_subparsers() - - p_addfile = subparsers.add_parser('addfile') - p_addfile.add_argument('epub') - p_addfile.add_argument('files', nargs='+', default=[]) - p_addfile.set_defaults(func=addfile_argparse) - - p_covercomesfirst = subparsers.add_parser('covercomesfirst') - 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) - p_generate_toc.set_defaults(func=generate_toc_argparse) - - p_holdit = subparsers.add_parser('holdit') - p_holdit.add_argument('epubs', nargs='+', default=[]) - p_holdit.set_defaults(func=holdit_argparse) - - p_merge = subparsers.add_parser('merge') - p_merge.add_argument('epubs', nargs='+', default=[]) - p_merge.add_argument('--output', dest='output', default=None, required=True) - p_merge.add_argument('--headerfile', dest='headerfile', action='store_true') - p_merge.add_argument('--demote_headers', dest='demote_headers', action='store_true') - p_merge.add_argument('--number_headerfile', dest='number_headerfile', action='store_true') - p_merge.add_argument('-y', '--autoyes', dest='autoyes', action='store_true') - p_merge.set_defaults(func=merge_argparse) - - p_normalize = subparsers.add_parser('normalize') - p_normalize.add_argument('epubs', nargs='+', default=[]) - p_normalize.set_defaults(func=normalize_argparse) - args = parser.parse_args(argv) args.func(args)