Use betterhelp to manage docstrings.
This commit is contained in:
parent
711493a9a8
commit
7afb9fcb7a
1 changed files with 56 additions and 57 deletions
113
epubfile.py
113
epubfile.py
|
@ -1175,6 +1175,9 @@ from voussoirkit import betterhelp
|
||||||
from voussoirkit import winglob
|
from voussoirkit import winglob
|
||||||
|
|
||||||
DOCSTRING = '''
|
DOCSTRING = '''
|
||||||
|
Epubfile
|
||||||
|
The simple python .epub scripting tool.
|
||||||
|
|
||||||
{addfile}
|
{addfile}
|
||||||
|
|
||||||
{covercomesfirst}
|
{covercomesfirst}
|
||||||
|
@ -1186,19 +1189,20 @@ DOCSTRING = '''
|
||||||
{merge}
|
{merge}
|
||||||
|
|
||||||
{normalize}
|
{normalize}
|
||||||
|
|
||||||
|
TO SEE DETAILS ON EACH COMMAND, RUN
|
||||||
|
> epubfile.py <command>
|
||||||
'''.lstrip()
|
'''.lstrip()
|
||||||
|
|
||||||
SUB_DOCSTRINGS = {
|
SUB_DOCSTRINGS = dict(
|
||||||
'addfile':
|
addfile='''
|
||||||
'''
|
|
||||||
addfile:
|
addfile:
|
||||||
Add files into the book.
|
Add files into the book.
|
||||||
|
|
||||||
> epubfile.py addfile book.epub page1.html image.jpg
|
> epubfile.py addfile book.epub page1.html image.jpg
|
||||||
'''.strip(),
|
'''.strip(),
|
||||||
|
|
||||||
'covercomesfirst':
|
covercomesfirst='''
|
||||||
'''
|
|
||||||
covercomesfirst:
|
covercomesfirst:
|
||||||
Rename the cover image file so that it is the alphabetically-first image.
|
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.
|
first, otherwise some /a/image.jpg will always be before /images/cover.jpg.
|
||||||
'''.strip(),
|
'''.strip(),
|
||||||
|
|
||||||
'exec':
|
exec='''
|
||||||
'''
|
|
||||||
exec:
|
exec:
|
||||||
Execute your own Python code against the book.
|
Execute your own Python code against the book.
|
||||||
|
|
||||||
> epubfile.py exec book.epub --command "book._____()"
|
> epubfile.py exec book.epub --command "book._____()"
|
||||||
'''.strip(),
|
'''.strip(),
|
||||||
|
|
||||||
'generate_toc':
|
generate_toc='''
|
||||||
'''
|
|
||||||
generate_toc:
|
generate_toc:
|
||||||
Regenerate the toc.ncx and nav.xhtml based on headers in the files.
|
Regenerate the toc.ncx and nav.xhtml based on headers in the files.
|
||||||
|
|
||||||
|
@ -1234,16 +1236,14 @@ generate_toc:
|
||||||
That is, h1, h2, ... hX.
|
That is, h1, h2, ... hX.
|
||||||
'''.strip(),
|
'''.strip(),
|
||||||
|
|
||||||
'holdit':
|
holdit='''
|
||||||
'''
|
|
||||||
holdit:
|
holdit:
|
||||||
Extract the book and leave it open for manual editing, then save.
|
Extract the book and leave it open for manual editing, then save.
|
||||||
|
|
||||||
> epubfile.py holdit book.epub
|
> epubfile.py holdit book.epub
|
||||||
'''.strip(),
|
'''.strip(),
|
||||||
|
|
||||||
'merge':
|
merge='''
|
||||||
'''
|
|
||||||
merge:
|
merge:
|
||||||
Merge multiple books into one.
|
Merge multiple books into one.
|
||||||
|
|
||||||
|
@ -1261,8 +1261,7 @@ merge:
|
||||||
Overwrite the output file without prompting.
|
Overwrite the output file without prompting.
|
||||||
'''.strip(),
|
'''.strip(),
|
||||||
|
|
||||||
'normalize':
|
normalize='''
|
||||||
'''
|
|
||||||
normalize:
|
normalize:
|
||||||
Rename files and directories in the book to match a common structure.
|
Rename files and directories in the book to match a common structure.
|
||||||
|
|
||||||
|
@ -1270,8 +1269,8 @@ normalize:
|
||||||
subdirectories by type: Text, Images, Styles, etc.
|
subdirectories by type: Text, Images, Styles, etc.
|
||||||
|
|
||||||
> epubfile.py normalize book.epub
|
> epubfile.py normalize book.epub
|
||||||
'''.strip()
|
'''.strip(),
|
||||||
}
|
)
|
||||||
|
|
||||||
DOCSTRING = betterhelp.add_previews(DOCSTRING, SUB_DOCSTRINGS)
|
DOCSTRING = betterhelp.add_previews(DOCSTRING, SUB_DOCSTRINGS)
|
||||||
|
|
||||||
|
@ -1470,47 +1469,47 @@ def normalize_argparse(args):
|
||||||
book.move_nav_to_end()
|
book.move_nav_to_end()
|
||||||
book.save(epub)
|
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):
|
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 = parser.parse_args(argv)
|
||||||
args.func(args)
|
args.func(args)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue