Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions bin/check_toc_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,57 @@
logger = logging.getLogger(__name__)


def is_slug(slug):
"""Check to see if the given slug is valid. Assumes extension will be rst.
def get_slug_path(slug):
"""Convert the given slug to a pathlib path. Assumes extension will be rst.

:param slug: String path slug
"""
path = SOURCE_PATH.joinpath(slug + ".rst")
logging.debug("Expanded slug: %s", path)
return path.is_file()
return path


def process_toc_txt(path):
"""Process a single toc.txt file

:param path: Pathlib path to toc.txt
:return: Set of valid paths
"""
valid_paths = set()
with path.open("r", encoding="utf-8") as file:
logging.debug("Processing %s", path)
for line_number, line in enumerate(file):
clean_line = COMMENT_REGEX.sub("", line).strip()
if clean_line and not is_slug(clean_line):
logging.warning("Invalid slug: %s:%i %s", path, line_number + 1, clean_line)
if clean_line:
slug_path = get_slug_path(clean_line)
if slug_path.is_file():
valid_paths.add(slug_path)
else:
logging.warning(
"Invalid slug: %s:%i %s", path, line_number + 1, clean_line
)
return valid_paths


def process_src_tree(toc_paths):
"""Ensure all RST files in the src tree are listed in the given set"""
src_paths = set()
for path in SOURCE_PATH.glob("**/*.rst"):
# files that begin with an underscore are included by other rst files
# they should not be listed in the toc
if path.stem[0] != "_":
src_paths.add(path)
for path in src_paths - toc_paths:
logging.warning("File not in any toc: %s", path)


def process_all():
"""Process all toc.txt files in the config directory"""
toc_paths = set()
for path in CONFIG_PATH.glob("**/*toc.txt"):
if path.is_file():
process_toc_txt(path)
toc_paths |= process_toc_txt(path)
process_src_tree(toc_paths)


def main():
Expand Down