tqdm/.meta/mkcompletion.py

65 lines
1.8 KiB
Python
Raw Normal View History

2020-04-26 21:31:53 +00:00
"""
Auto-generate tqdm/completion.sh from docstrings.
"""
2020-04-24 21:25:32 +00:00
from __future__ import print_function
2020-04-26 20:50:55 +00:00
from io import open as io_open
2020-04-24 21:25:32 +00:00
from os import path
2020-04-26 20:50:55 +00:00
import re
2020-04-24 21:25:32 +00:00
import sys
2020-04-26 20:50:55 +00:00
2020-05-22 12:54:09 +00:00
sys.path.insert(0, path.dirname(path.dirname(__file__)))
import tqdm # NOQA
import tqdm.cli # NOQA
2020-04-25 17:44:23 +00:00
RE_OPT = re.compile(r'(\w+) :', flags=re.M)
2020-12-18 22:55:41 +00:00
RE_OPT_INPUT = re.compile(r'(\w+) : (?:str|int|float|chr|dict|tuple)', flags=re.M)
2020-04-25 17:44:23 +00:00
2020-04-26 20:50:55 +00:00
def doc2opt(doc, user_input=True):
"""
doc : str, document to parse
2020-04-26 20:50:55 +00:00
user_input : bool, optional.
[default: True] for only options requiring user input
"""
2020-04-26 20:50:55 +00:00
RE = RE_OPT_INPUT if user_input else RE_OPT
return ('--' + i for i in RE.findall(doc))
2020-04-25 17:44:23 +00:00
# CLI options
2020-04-25 17:44:23 +00:00
options = {'-h', '--help', '-v', '--version'}
options_input = set()
2020-04-25 18:45:35 +00:00
for doc in (tqdm.tqdm.__init__.__doc__, tqdm.cli.CLI_EXTRA_DOC):
2020-04-26 20:50:55 +00:00
options.update(doc2opt(doc, user_input=False))
options_input.update(doc2opt(doc, user_input=True))
2020-12-18 22:55:41 +00:00
options.difference_update('--' + i for i in ('name',) + tqdm.cli.UNSUPPORTED_OPTS)
options_input &= options
options_input -= {"--log"} # manually dealt with
2020-04-24 21:25:32 +00:00
src_dir = path.abspath(path.dirname(__file__))
completion = u"""\
#!/usr/bin/env bash
_tqdm(){{
local cur prv
cur="${{COMP_WORDS[COMP_CWORD]}}"
prv="${{COMP_WORDS[COMP_CWORD - 1]}}"
case ${{prv}} in
{opts_manual})
# await user input
;;
"--log")
COMPREPLY=($(compgen -W \
'CRITICAL FATAL ERROR WARN WARNING INFO DEBUG NOTSET' -- ${{cur}}))
;;
*)
COMPREPLY=($(compgen -W '{opts}' -- ${{cur}}))
;;
esac
}}
complete -F _tqdm tqdm
2020-12-18 22:55:41 +00:00
""".format(opts=' '.join(sorted(options)), opts_manual='|'.join(sorted(options_input)))
2020-04-24 21:25:32 +00:00
if __name__ == "__main__":
fncompletion = path.join(path.dirname(src_dir), 'tqdm', 'completion.sh')
with io_open(fncompletion, mode='w', encoding='utf-8') as fd:
fd.write(completion)