|
18 | 18 | logger = logging.getLogger(__name__) |
19 | 19 |
|
20 | 20 |
|
21 | | -def is_slug(slug): |
22 | | - """Check to see if the given slug is valid. Assumes extension will be rst. |
| 21 | +def get_slug_path(slug): |
| 22 | + """Convert the given slug to a pathlib path. Assumes extension will be rst. |
23 | 23 |
|
24 | 24 | :param slug: String path slug |
25 | 25 | """ |
26 | 26 | path = SOURCE_PATH.joinpath(slug + ".rst") |
27 | 27 | logging.debug("Expanded slug: %s", path) |
28 | | - return path.is_file() |
| 28 | + return path |
29 | 29 |
|
30 | 30 |
|
31 | 31 | def process_toc_txt(path): |
32 | 32 | """Process a single toc.txt file |
33 | 33 |
|
34 | 34 | :param path: Pathlib path to toc.txt |
| 35 | + :return: Set of valid paths |
35 | 36 | """ |
| 37 | + valid_paths = set() |
36 | 38 | with path.open("r", encoding="utf-8") as file: |
37 | 39 | logging.debug("Processing %s", path) |
38 | 40 | for line_number, line in enumerate(file): |
39 | 41 | clean_line = COMMENT_REGEX.sub("", line).strip() |
40 | | - if clean_line and not is_slug(clean_line): |
41 | | - logging.warning("Invalid slug: %s:%i %s", path, line_number + 1, clean_line) |
| 42 | + if clean_line: |
| 43 | + slug_path = get_slug_path(clean_line) |
| 44 | + if slug_path.is_file(): |
| 45 | + valid_paths.add(slug_path) |
| 46 | + else: |
| 47 | + logging.warning( |
| 48 | + "Invalid slug: %s:%i %s", path, line_number + 1, clean_line |
| 49 | + ) |
| 50 | + return valid_paths |
| 51 | + |
| 52 | + |
| 53 | +def process_src_tree(toc_paths): |
| 54 | + """Ensure all RST files in the src tree are listed in the given set""" |
| 55 | + src_paths = set() |
| 56 | + for path in SOURCE_PATH.glob("**/*.rst"): |
| 57 | + # files that begin with an underscore are included by other rst files |
| 58 | + # they should not be listed in the toc |
| 59 | + if path.stem[0] != "_": |
| 60 | + src_paths.add(path) |
| 61 | + for path in src_paths - toc_paths: |
| 62 | + logging.warning("File not in any toc: %s", path) |
42 | 63 |
|
43 | 64 |
|
44 | 65 | def process_all(): |
45 | 66 | """Process all toc.txt files in the config directory""" |
| 67 | + toc_paths = set() |
46 | 68 | for path in CONFIG_PATH.glob("**/*toc.txt"): |
47 | 69 | if path.is_file(): |
48 | | - process_toc_txt(path) |
| 70 | + toc_paths |= process_toc_txt(path) |
| 71 | + process_src_tree(toc_paths) |
49 | 72 |
|
50 | 73 |
|
51 | 74 | def main(): |
|
0 commit comments