Skip to content

Commit ea24ba5

Browse files
committed
Add support for building documentation for other versions of module.
Specify the path to the source code of the version (via the '--path' flag) you wish to generate documentation for and the script should automatically discover the version of that code and generate the documentation. Pass the '--clean' flag to remove all existing documentation from the directory. Example to generate docs for v0.5.1 whilst keeping any existing documentation: python docs/make.py --path archives/v0.5.1/screen_brightness_control Note that you must store the code in a path that ends with the folder "screen_brightness_control" so that pdoc picks up the correct module name
1 parent c3415bc commit ea24ba5

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

docs/make.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/bin/python
2+
# flake8: noqa: E501
23
'''Script to generate documentation for this project.'''
4+
import argparse
5+
import glob
36
import os
47
import sys
8+
import shutil
59
from pathlib import Path
610

711
import pdoc
@@ -11,33 +15,64 @@
1115
TEMPLATES = HERE / 'templates'
1216
OUTPUT_DIR = HERE / 'docs'
1317

14-
if os.path.isfile(HERE / '..' / 'screen_brightness_control' / '_version.py'):
15-
sys.path.insert(0, str(HERE / '..' / 'screen_brightness_control'))
16-
from _version import __version__ # noqa: E402
17-
else:
18-
sys.path.insert(0, str(HERE / '..'))
19-
from screen_brightness_control import __version__
2018

19+
def makedir(path: Path, destroy=False):
20+
if destroy:
21+
if os.path.isdir(path):
22+
shutil.rmtree(path)
2123

22-
pdoc.docstrings.GOOGLE_LIST_SECTIONS.extend(["Returns", "Yields"])
23-
pdoc.render.configure(docformat='google', template_directory=TEMPLATES, footer_text=f'screen_brightness_control v{__version__}') # noqa: E501
24+
os.makedirs(path, exist_ok=True)
25+
26+
27+
def get_directory_version(path: Path):
28+
if os.path.isfile(path / '_version.py'):
29+
with open(path / '_version.py', 'r') as f:
30+
contents = f.read()
31+
else:
32+
with open(path / '__init__.py', 'r') as f:
33+
contents = f.read()
34+
35+
line = [i for i in contents.split('\n') if '__version__=' in i.replace(' ', '')][0]
36+
v = line.replace(' ', '').replace('__version__=', '').replace("'", "")
37+
38+
return v
39+
40+
41+
def configure_pdoc(**kwargs):
42+
pdoc.render.configure(**{**PDOC_CONFIG, **kwargs})
2443

2544

26-
def generate(source, output_dir):
27-
pdoc.pdoc(source, output_directory=Path(output_dir), format='html')
45+
def run_pdoc(source, output):
46+
pdoc.pdoc(source, output_directory=output, format='html')
47+
48+
49+
__version__ = get_directory_version(HERE / '..' / 'screen_brightness_control')
50+
51+
pdoc.docstrings.GOOGLE_LIST_SECTIONS.extend(["Returns", "Yields"])
52+
PDOC_CONFIG = dict(docformat='google', template_directory=TEMPLATES, footer_text=f'screen_brightness_control v{__version__}')
53+
configure_pdoc()
2854

2955

3056
if __name__ == '__main__':
57+
parser = argparse.ArgumentParser()
58+
parser.add_argument('-p', '--path', help='Generate documentation for this path', default=str(HERE / '..' / 'screen_brightness_control'))
59+
parser.add_argument('--clean', help='Remove any existing documentation of any version', action='store_true')
60+
args = parser.parse_args()
61+
3162
# generate top level documentation
63+
makedir(OUTPUT_DIR, destroy=args.clean)
3264
os.environ['BUILD_DOCS_TOPLEVEL'] = '1'
33-
# pdoc.render.configure(search=False)
34-
pdoc.pdoc(HERE / 'source', output_directory=Path(OUTPUT_DIR), format='html')
65+
run_pdoc(HERE / 'source', OUTPUT_DIR)
3566
os.environ['BUILD_DOCS_TOPLEVEL'] = '0'
3667

37-
# generate current version documentation
3868
os.makedirs(OUTPUT_DIR / 'docs', exist_ok=True)
39-
# pdoc.render.configure(search=True)
40-
pdoc.pdoc(HERE / '..' / 'screen_brightness_control', output_directory=Path(OUTPUT_DIR / 'docs' / __version__), format='html') # noqa: E501
69+
70+
# generate documentation for specified path
71+
args.path = Path(args.path)
72+
path_version = get_directory_version(args.path)
73+
makedir(OUTPUT_DIR / 'docs' / path_version)
74+
configure_pdoc(footer_text=f'screen_brightness_control v{path_version}')
75+
run_pdoc(args.path, OUTPUT_DIR / 'docs' / path_version)
4176

4277
# read version switcher js
4378
with open(TEMPLATES / 'version_navigator.js', 'r') as f:

0 commit comments

Comments
 (0)