|
8 | 8 | from pathlib import Path |
9 | 9 |
|
10 | 10 |
|
11 | | -for path in Path("src").rglob("*"): |
12 | | - if path.is_dir(): |
13 | | - directory = path |
14 | | - cwd = os.getcwd() |
15 | | - os.chdir(directory) |
16 | | - print(directory) |
17 | | - |
18 | | - try: |
19 | | - with open("README.md", "w", encoding="utf-8", newline="\n") as indexfile: |
20 | | - print( |
21 | | - f"# List of source files stored in `{directory}` directory", |
22 | | - file=indexfile, |
23 | | - ) |
24 | | - print("", file=indexfile) |
25 | | - files = sorted(os.listdir()) |
26 | | - |
27 | | - for file in files: |
28 | | - if file.endswith(".py"): |
29 | | - print(f"## [{file}]({file})", file=indexfile) |
30 | | - with open(file, "r", encoding="utf-8") as fin: |
31 | | - source = fin.read() |
32 | | - try: |
33 | | - mod = ast.parse(source) |
34 | | - doc = ast.get_docstring(mod) |
35 | | - except SyntaxError: |
36 | | - doc = None |
37 | | - if doc: |
38 | | - print(doc.splitlines()[0], file=indexfile) |
39 | | - print(file=indexfile) |
40 | | - finally: |
41 | | - os.chdir(cwd) |
| 11 | +def generate_docfile(directory): |
| 12 | + """Generate README.md in the CWD.""" |
| 13 | + with open("README.md", "w", encoding="utf-8", newline="\n") as indexfile: |
| 14 | + print( |
| 15 | + f"# List of source files stored in `{directory}` directory", |
| 16 | + file=indexfile, |
| 17 | + ) |
| 18 | + print("", file=indexfile) |
| 19 | + files = sorted(os.listdir()) |
| 20 | + |
| 21 | + for file in files: |
| 22 | + if file.endswith(".py"): |
| 23 | + print(f"## [{file}]({file})", file=indexfile) |
| 24 | + with open(file, "r", encoding="utf-8") as fin: |
| 25 | + source = fin.read() |
| 26 | + try: |
| 27 | + mod = ast.parse(source) |
| 28 | + doc = ast.get_docstring(mod) |
| 29 | + except SyntaxError: |
| 30 | + doc = None |
| 31 | + if doc: |
| 32 | + print(doc.splitlines()[0], file=indexfile) |
| 33 | + print(file=indexfile) |
| 34 | + |
| 35 | + |
| 36 | +def generate_documentation_on_path(path): |
| 37 | + """Generate documentation for all the sources found in path.""" |
| 38 | + directory = path |
| 39 | + cwd = os.getcwd() |
| 40 | + os.chdir(directory) |
| 41 | + print(directory) |
| 42 | + |
| 43 | + try: |
| 44 | + generate_docfile(directory) |
| 45 | + finally: |
| 46 | + os.chdir(cwd) |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + """Entry point to this script, regenerates documentation in all directories.""" |
| 51 | + generate_documentation_on_path("src/") |
| 52 | + for path in Path("src").rglob("*"): |
| 53 | + if path.is_dir(): |
| 54 | + generate_documentation_on_path(path) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + main() |
0 commit comments