tqdm/.meta/mkdocs.py

73 lines
2.2 KiB
Python
Raw Normal View History

2020-04-26 21:31:53 +00:00
"""
Auto-generate README.rst from .meta/.readme.rst and docstrings.
"""
2021-01-09 17:00:18 +00:00
import sys
2023-08-05 21:56:07 +00:00
from pathlib import Path
2020-04-26 20:50:55 +00:00
from textwrap import dedent
2023-08-05 21:56:07 +00:00
sys.path.insert(0, str(Path(__file__).parent.parent))
2020-05-22 12:54:09 +00:00
import tqdm # NOQA
import tqdm.cli # NOQA
2020-04-26 20:50:55 +00:00
2019-01-27 00:13:42 +00:00
HEAD_ARGS = """
Parameters
----------
"""
HEAD_RETS = """
Returns
-------
"""
HEAD_CLI = """
Extra CLI Options
-----------------
name : type, optional
TODO: find out why this is needed.
"""
2019-01-27 00:29:13 +00:00
2019-05-13 16:33:42 +00:00
def doc2rst(doc, arglist=True, raw=False):
2019-01-27 00:13:42 +00:00
"""
arglist : bool, whether to create argument lists
2019-05-13 16:33:42 +00:00
raw : bool, ignores arglist and indents by 2 spaces
2019-01-27 00:13:42 +00:00
"""
2019-05-13 16:33:42 +00:00
doc = doc.replace('`', '``')
if raw:
doc = doc.replace('\n ', '\n ')
else:
doc = dedent(doc)
if arglist:
2021-09-18 21:10:50 +00:00
doc = '\n'.join(i if not i or i[0] == ' ' else '* ' + i + ' '
for i in doc.split('\n'))
2019-01-27 00:13:42 +00:00
return doc
2023-08-05 21:56:07 +00:00
src_dir = Path(__file__).parent.resolve()
README_rst = (src_dir / '.readme.rst').read_text("utf-8")
class_doc, init_doc = tqdm.tqdm.__doc__.split('\n\n', 1)
DOC_tqdm = doc2rst(class_doc + '\n', False).replace('\n', '\n ')
DOC_tqdm_init = doc2rst('\n' + init_doc)
DOC_tqdm_init_args = DOC_tqdm_init.partition(doc2rst(HEAD_ARGS))[-1].replace(
'\n ', '\n ').replace('\n ', '\n ')
2021-09-18 16:24:50 +00:00
DOC_tqdm_init_args, _, DOC_tqdm_init_rets = DOC_tqdm_init_args.partition(doc2rst(HEAD_RETS))
2019-08-26 15:40:03 +00:00
DOC_cli = doc2rst(tqdm.cli.CLI_EXTRA_DOC).partition(doc2rst(HEAD_CLI))[-1]
2019-05-13 16:33:42 +00:00
DOC_tqdm_tqdm = {}
for i in dir(tqdm.tqdm):
doc = getattr(tqdm.tqdm, i).__doc__
if doc:
DOC_tqdm_tqdm[i] = doc2rst(doc, raw=True)
2019-01-27 00:13:42 +00:00
# special cases
DOC_tqdm_init_args = DOC_tqdm_init_args.replace(' *,', ' ``*``,')
DOC_tqdm_init_args = DOC_tqdm_init_args.partition('* gui : bool, optional')[0]
2021-09-18 16:24:50 +00:00
README_rst = (
README_rst.replace('{DOC_tqdm}', DOC_tqdm)
.replace('{DOC_tqdm.tqdm.__init__.Parameters}', DOC_tqdm_init_args)
.replace('{DOC_tqdm.cli.CLI_EXTRA_DOC}', DOC_cli)
.replace('{DOC_tqdm.tqdm.__init__.Returns}', DOC_tqdm_init_rets))
2019-05-13 16:33:42 +00:00
for k, v in DOC_tqdm_tqdm.items():
README_rst = README_rst.replace('{DOC_tqdm.tqdm.%s}' % k, v)
2019-01-27 00:13:42 +00:00
if __name__ == "__main__":
2023-08-05 21:56:07 +00:00
(src_dir.parent / 'README.rst').write_text(README_rst, encoding='utf-8')