module2rst.py 2.21 KB
Newer Older
sunzhq2's avatar
sunzhq2 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
from glob import glob
import importlib
import os

import configargparse


# parser
parser = configargparse.ArgumentParser(
    description="generate RST files from <root> module recursively into <dst>/_gen",
    config_file_parser_class=configargparse.YAMLConfigFileParser,
    formatter_class=configargparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
    "--root", nargs="+", help="root module to generate docs recursively"
)
parser.add_argument("--dst", type=str, help="destination path to generate RSTs")
parser.add_argument("--exclude", nargs="*", default=[], help="exclude module name")
args = parser.parse_args()
print(args)


def to_module(path_name):
    ret = path_name.replace(".py", "").replace("/", ".")
    if ret.endswith("."):
        return ret[:-1]
    return ret


def gen_rst(module_path, f):
    name = to_module(module_path)
    module = importlib.import_module(name)
    title = name + " package"
    sep = "=" * len(title)
    doc = module.__doc__
    if doc is None:
        doc = ""
    f.write(
        f"""
{title}
{sep}
{doc}

"""
    )

    for cpath in glob(module_path + "/**/*.py", recursive=True):
        print(cpath)
        if not os.path.exists(cpath):
            continue
        if "__pycache__" in cpath:
            continue
        cname = to_module(cpath)
        csep = "-" * len(cname)
        f.write(
            f"""
.. _{cname}:

{cname}
{csep}

.. automodule:: {cname}
    :members:
    :undoc-members:
    :show-inheritance:

"""
        )
    f.flush()


modules_rst = """
.. toctree::
   :maxdepth: 1
   :caption: Package Reference:

"""
gendir = args.dst + "/_gen"
os.makedirs(gendir, exist_ok=True)
for root in args.root:
    for p in glob(root + "/**", recursive=False):
        if p in args.exclude:
            continue
        if "__pycache__" in p:
            continue
        if "__init__" in p:
            continue
        if "version.txt" in p:
            continue
        fname = to_module(p) + ".rst"
        dst = f"{gendir}/{fname}"
        modules_rst += f"   ./_gen/{fname}\n"
        print(f"[INFO] generating {dst}")
        with open(dst, "w") as f:
            gen_rst(p, f)


with open(gendir + "/modules.rst", "w") as f:
    f.write(modules_rst)