-
Notifications
You must be signed in to change notification settings - Fork 33
/
gen_ref_pages.py
69 lines (54 loc) · 2.47 KB
/
gen_ref_pages.py
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
from pathlib import Path
import griffe
import mkdocs_gen_files
from quantfreedom.helpers.utils import delete_dir
nav = mkdocs_gen_files.Nav()
try:
Path(Path(__file__).parent / "docs/summary.md").unlink()
delete_dir(Path(__file__).parent / "docs" / "quantfreedom")
except:
pass
src = Path(__file__).parent / "quantfreedom"
data = griffe.load("quantfreedom")
for path in sorted(src.rglob("*.py")):
if "_github" in str(path):
continue
module_path = path.relative_to(src.parent).with_suffix("")
parts = tuple(module_path.parts)
if parts[-1] == "__init__":
# We're currently handling an __init__.py file.
# Remove "__init__" suffix for accessing Griffe object in data.
parts = parts[:-1]
# Get Griffe object. If there's only one item in `parts`,
# it's the package name, so it's simply `data` (which is the package collected by Griffe).
# If there are more parts, we access the object by removing the first part, which is the package name,
# because `data` is already the package.
init_module = data[parts[1:]] if len(parts) > 1 else data
# We consider the __init__ module "has docstrings" if:
# - it has a module docstring
# - or any of its members has a docstring
# (attributes, functions, classes, but not modules or objects imported from other modules)
has_docstrings = init_module.has_docstring or any(
member.has_docstrings
for member in init_module.members.values()
if not (member.is_alias or member.is_module)
)
# has_docstrings = init_module.has_docstring or any(
# member.has_docstrings
# for member in init_module.members.values()
# if not (member.is_alias or member.is_module)
# )
else:
# If it's another module, simply use `has_docstrings` as a non-init module cannot have submodules anyway.
has_docstrings = data[parts[1:]].has_docstrings
# If we didn't find docstrings, continue to the next module file.
if not has_docstrings:
continue
doc_path = path.relative_to(src.parent).with_suffix(".md")
nav[parts] = doc_path.as_posix()
with mkdocs_gen_files.open(doc_path, "w") as fd:
ident = ".".join(parts)
fd.write(f"::: {ident}")
mkdocs_gen_files.set_edit_path(doc_path, Path("../") / path)
with mkdocs_gen_files.open("summary.md", "w") as nav_file:
nav_file.writelines(nav.build_literate_nav())