Skip to content

Commit 00ddacf

Browse files
authored
move validate_rst_title_capitalization to pre-commit (#39779)
1 parent d7efe26 commit 00ddacf

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ repos:
180180
language: pygrep
181181
types: [python]
182182
files: ^pandas/tests/
183+
- id: title-capitalization
184+
name: Validate correct capitalization among titles in documentation
185+
entry: python scripts/validate_rst_title_capitalization.py
186+
language: python
187+
types: [rst]
188+
files: ^doc/source/(development|reference)/
183189
- repo: https://github.com/asottile/yesqa
184190
rev: v1.2.2
185191
hooks:

ci/code_checks.sh

-4
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
233233
$BASE_DIR/scripts/validate_docstrings.py --format=actions --errors=GL03,GL04,GL05,GL06,GL07,GL09,GL10,SS02,SS04,SS05,PR03,PR04,PR05,PR10,EX04,RT01,RT04,RT05,SA02,SA03
234234
RET=$(($RET + $?)) ; echo $MSG "DONE"
235235

236-
MSG='Validate correct capitalization among titles in documentation' ; echo $MSG
237-
$BASE_DIR/scripts/validate_rst_title_capitalization.py $BASE_DIR/doc/source/development $BASE_DIR/doc/source/reference
238-
RET=$(($RET + $?)) ; echo $MSG "DONE"
239-
240236
fi
241237

242238
### TYPING ###

scripts/validate_rst_title_capitalization.py

+9-47
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
#!/usr/bin/env python3
21
"""
32
Validate that the titles in the rst files follow the proper capitalization convention.
43
54
Print the titles that do not follow the convention.
65
76
Usage::
8-
./scripts/validate_rst_title_capitalization.py doc/source/development/contributing.rst
9-
./scripts/validate_rst_title_capitalization.py doc/source/
107
8+
As pre-commit hook (recommended):
9+
pre-commit run title-capitalization --all-files
10+
11+
From the command-line:
12+
python scripts/validate_rst_title_capitalization.py <rst file>
1113
"""
1214
import argparse
13-
import glob
14-
import os
1515
import re
1616
import sys
1717
from typing import Iterable, List, Tuple
@@ -233,45 +233,14 @@ def find_titles(rst_file: str) -> Iterable[Tuple[str, int]]:
233233
previous_line = line
234234

235235

236-
def find_rst_files(source_paths: List[str]) -> Iterable[str]:
237-
"""
238-
Given the command line arguments of directory paths, this method
239-
yields the strings of the .rst file directories that these paths contain.
240-
241-
Parameters
242-
----------
243-
source_paths : str
244-
List of directories to validate, provided through command line arguments.
245-
246-
Yields
247-
-------
248-
str
249-
Directory address of a .rst files found in command line argument directories.
250-
"""
251-
252-
for directory_address in source_paths:
253-
if not os.path.exists(directory_address):
254-
raise ValueError(
255-
"Please enter a valid path, pointing to a valid file/directory."
256-
)
257-
elif directory_address.endswith(".rst"):
258-
yield directory_address
259-
else:
260-
yield from glob.glob(
261-
pathname=f"{directory_address}/**/*.rst", recursive=True
262-
)
263-
264-
265-
def main(source_paths: List[str], output_format: str) -> int:
236+
def main(source_paths: List[str]) -> int:
266237
"""
267238
The main method to print all headings with incorrect capitalization.
268239
269240
Parameters
270241
----------
271242
source_paths : str
272243
List of directories to validate, provided through command line arguments.
273-
output_format : str
274-
Output format of the script.
275244
276245
Returns
277246
-------
@@ -281,7 +250,7 @@ def main(source_paths: List[str], output_format: str) -> int:
281250

282251
number_of_errors: int = 0
283252

284-
for filename in find_rst_files(source_paths):
253+
for filename in source_paths:
285254
for title, line_number in find_titles(filename):
286255
if title != correct_title_capitalization(title):
287256
print(
@@ -297,16 +266,9 @@ def main(source_paths: List[str], output_format: str) -> int:
297266
parser = argparse.ArgumentParser(description="Validate heading capitalization")
298267

299268
parser.add_argument(
300-
"paths", nargs="+", default=".", help="Source paths of file/directory to check."
301-
)
302-
303-
parser.add_argument(
304-
"--format",
305-
"-f",
306-
default="{source_path}:{line_number}:{msg}:{heading}:{correct_heading}",
307-
help="Output format of incorrectly capitalized titles",
269+
"paths", nargs="*", help="Source paths of file/directory to check."
308270
)
309271

310272
args = parser.parse_args()
311273

312-
sys.exit(main(args.paths, args.format))
274+
sys.exit(main(args.paths))

0 commit comments

Comments
 (0)