|
1 | 1 | #!/bin/python |
| 2 | +# flake8: noqa: E501 |
2 | 3 | '''Script to generate documentation for this project.''' |
| 4 | +import argparse |
| 5 | +import glob |
3 | 6 | import os |
4 | 7 | import sys |
| 8 | +import shutil |
5 | 9 | from pathlib import Path |
6 | 10 |
|
7 | 11 | import pdoc |
|
11 | 15 | TEMPLATES = HERE / 'templates' |
12 | 16 | OUTPUT_DIR = HERE / 'docs' |
13 | 17 |
|
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__ |
20 | 18 |
|
| 19 | +def makedir(path: Path, destroy=False): |
| 20 | + if destroy: |
| 21 | + if os.path.isdir(path): |
| 22 | + shutil.rmtree(path) |
21 | 23 |
|
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}) |
24 | 43 |
|
25 | 44 |
|
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() |
28 | 54 |
|
29 | 55 |
|
30 | 56 | 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 | + |
31 | 62 | # generate top level documentation |
| 63 | + makedir(OUTPUT_DIR, destroy=args.clean) |
32 | 64 | 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) |
35 | 66 | os.environ['BUILD_DOCS_TOPLEVEL'] = '0' |
36 | 67 |
|
37 | | - # generate current version documentation |
38 | 68 | 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) |
41 | 76 |
|
42 | 77 | # read version switcher js |
43 | 78 | with open(TEMPLATES / 'version_navigator.js', 'r') as f: |
|
0 commit comments