|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Validate that the titles in the rst files follow the proper capitalization convention. |
| 4 | +
|
| 5 | +Print the titles that do not follow the convention. |
| 6 | +
|
| 7 | +Usage:: |
| 8 | +./scripts/validate_rst_title_capitalization.py doc/source/development/contributing.rst |
| 9 | +./scripts/validate_rst_title_capitalization.py doc/source/ |
| 10 | +
|
| 11 | +""" |
| 12 | +import argparse |
| 13 | +import sys |
| 14 | +import re |
| 15 | +import os |
| 16 | +from typing import Tuple, Generator, List |
| 17 | +import glob |
| 18 | + |
| 19 | + |
| 20 | +CAPITALIZATION_EXCEPTIONS = { |
| 21 | + "pandas", |
| 22 | + "Python", |
| 23 | + "IPython", |
| 24 | + "PyTables", |
| 25 | + "Excel", |
| 26 | + "JSON", |
| 27 | + "HTML", |
| 28 | + "SAS", |
| 29 | + "SQL", |
| 30 | + "BigQuery", |
| 31 | + "STATA", |
| 32 | + "Interval", |
| 33 | + "PEP8", |
| 34 | + "Period", |
| 35 | + "Series", |
| 36 | + "Index", |
| 37 | + "DataFrame", |
| 38 | + "C", |
| 39 | + "Git", |
| 40 | + "GitHub", |
| 41 | + "NumPy", |
| 42 | + "Apache", |
| 43 | + "Arrow", |
| 44 | + "Parquet", |
| 45 | + "MultiIndex", |
| 46 | + "NumFOCUS", |
| 47 | + "sklearn", |
| 48 | + "Docker", |
| 49 | +} |
| 50 | + |
| 51 | +CAP_EXCEPTIONS_DICT = {word.lower(): word for word in CAPITALIZATION_EXCEPTIONS} |
| 52 | + |
| 53 | +err_msg = "Heading capitalization formatted incorrectly. Please correctly capitalize" |
| 54 | + |
| 55 | +symbols = ("*", "=", "-", "^", "~", "#", '"') |
| 56 | + |
| 57 | + |
| 58 | +def correct_title_capitalization(title: str) -> str: |
| 59 | + """ |
| 60 | + Algorithm to create the correct capitalization for a given title. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + title : str |
| 65 | + Heading string to correct. |
| 66 | +
|
| 67 | + Returns |
| 68 | + ------- |
| 69 | + str |
| 70 | + Correctly capitalized heading. |
| 71 | + """ |
| 72 | + |
| 73 | + # Strip all non-word characters from the beginning of the title to the |
| 74 | + # first word character. |
| 75 | + correct_title: str = re.sub(r"^\W*", "", title).capitalize() |
| 76 | + |
| 77 | + # Remove a URL from the title. We do this because words in a URL must |
| 78 | + # stay lowercase, even if they are a capitalization exception. |
| 79 | + removed_https_title = re.sub(r"<https?:\/\/.*[\r\n]*>", "", correct_title) |
| 80 | + |
| 81 | + # Split a title into a list using non-word character delimiters. |
| 82 | + word_list = re.split(r"\W", removed_https_title) |
| 83 | + |
| 84 | + for word in word_list: |
| 85 | + if word.lower() in CAP_EXCEPTIONS_DICT: |
| 86 | + correct_title = re.sub( |
| 87 | + rf"\b{word}\b", CAP_EXCEPTIONS_DICT[word.lower()], correct_title |
| 88 | + ) |
| 89 | + |
| 90 | + return correct_title |
| 91 | + |
| 92 | + |
| 93 | +def find_titles(rst_file: str) -> Generator[Tuple[str, int], None, None]: |
| 94 | + """ |
| 95 | + Algorithm to identify particular text that should be considered headings in an |
| 96 | + RST file. |
| 97 | +
|
| 98 | + See <https://thomas-cokelaer.info/tutorials/sphinx/rest_syntax.html> for details |
| 99 | + on what constitutes a string as a heading in RST. |
| 100 | +
|
| 101 | + Parameters |
| 102 | + ---------- |
| 103 | + rst_file : str |
| 104 | + RST file to scan through for headings. |
| 105 | +
|
| 106 | + Yields |
| 107 | + ------- |
| 108 | + title : str |
| 109 | + A heading found in the rst file. |
| 110 | +
|
| 111 | + line_number : int |
| 112 | + The corresponding line number of the heading. |
| 113 | + """ |
| 114 | + |
| 115 | + with open(rst_file, "r") as fd: |
| 116 | + previous_line = "" |
| 117 | + for i, line in enumerate(fd): |
| 118 | + line = line[:-1] |
| 119 | + line_chars = set(line) |
| 120 | + if ( |
| 121 | + len(line_chars) == 1 |
| 122 | + and line_chars.pop() in symbols |
| 123 | + and len(line) == len(previous_line) |
| 124 | + ): |
| 125 | + yield re.sub(r"[`\*_]", "", previous_line), i |
| 126 | + previous_line = line |
| 127 | + |
| 128 | + |
| 129 | +def find_rst_files(source_paths: List[str]) -> Generator[str, None, None]: |
| 130 | + """ |
| 131 | + Given the command line arguments of directory paths, this method |
| 132 | + yields the strings of the .rst file directories that these paths contain. |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + source_paths : str |
| 137 | + List of directories to validate, provided through command line arguments. |
| 138 | +
|
| 139 | + Yields |
| 140 | + ------- |
| 141 | + str |
| 142 | + Directory address of a .rst files found in command line argument directories. |
| 143 | + """ |
| 144 | + |
| 145 | + for directory_address in source_paths: |
| 146 | + if not os.path.exists(directory_address): |
| 147 | + raise ValueError( |
| 148 | + "Please enter a valid path, pointing to a valid file/directory." |
| 149 | + ) |
| 150 | + elif directory_address.endswith(".rst"): |
| 151 | + yield directory_address |
| 152 | + else: |
| 153 | + for filename in glob.glob( |
| 154 | + pathname=f"{directory_address}/**/*.rst", recursive=True |
| 155 | + ): |
| 156 | + yield filename |
| 157 | + |
| 158 | + |
| 159 | +def main(source_paths: List[str], output_format: str) -> bool: |
| 160 | + """ |
| 161 | + The main method to print all headings with incorrect capitalization. |
| 162 | +
|
| 163 | + Parameters |
| 164 | + ---------- |
| 165 | + source_paths : str |
| 166 | + List of directories to validate, provided through command line arguments. |
| 167 | + output_format : str |
| 168 | + Output format of the script. |
| 169 | +
|
| 170 | + Returns |
| 171 | + ------- |
| 172 | + int |
| 173 | + Number of incorrect headings found overall. |
| 174 | + """ |
| 175 | + |
| 176 | + number_of_errors: int = 0 |
| 177 | + |
| 178 | + for filename in find_rst_files(source_paths): |
| 179 | + for title, line_number in find_titles(filename): |
| 180 | + if title != correct_title_capitalization(title): |
| 181 | + print( |
| 182 | + f"""{filename}:{line_number}:{err_msg} "{title}" to "{ |
| 183 | + correct_title_capitalization(title)}" """ |
| 184 | + ) |
| 185 | + number_of_errors += 1 |
| 186 | + |
| 187 | + return number_of_errors |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + parser = argparse.ArgumentParser(description="Validate heading capitalization") |
| 192 | + |
| 193 | + parser.add_argument( |
| 194 | + "paths", nargs="+", default=".", help="Source paths of file/directory to check." |
| 195 | + ) |
| 196 | + |
| 197 | + parser.add_argument( |
| 198 | + "--format", |
| 199 | + "-f", |
| 200 | + default="{source_path}:{line_number}:{msg}:{heading}:{correct_heading}", |
| 201 | + help="Output format of incorrectly capitalized titles", |
| 202 | + ) |
| 203 | + |
| 204 | + args = parser.parse_args() |
| 205 | + |
| 206 | + sys.exit(main(args.paths, args.format)) |
0 commit comments