Skip to content

Commit df01730

Browse files
author
awu42
committed
Simplified validate_rst_title_capitalization main method (pandas-dev#26941)
1 parent 3256615 commit df01730

File tree

1 file changed

+9
-64
lines changed

1 file changed

+9
-64
lines changed

scripts/validate_rst_title_capitalization.py

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949

5050
CAP_EXCEPTIONS_DICT = {word.lower(): word for word in CAPITALIZATION_EXCEPTIONS}
5151

52-
bad_title_dict = {}
53-
5452
err_msg = "Heading capitalization formatted incorrectly. Please correctly capitalize"
5553

5654

@@ -79,36 +77,12 @@ def correct_title_capitalization(title: str) -> str:
7977
for word in word_list:
8078
if word.lower() in CAP_EXCEPTIONS_DICT:
8179
correct_title = re.sub(
82-
r"\b" + word + r"\b", CAP_EXCEPTIONS_DICT[word.lower()], correct_title
80+
rf"\b{word}\b", CAP_EXCEPTIONS_DICT[word.lower()], correct_title
8381
)
8482

8583
return correct_title
8684

8785

88-
def is_following_capitalization_convention(title: str) -> bool:
89-
"""
90-
Function to return if a given title is capitalized correctly
91-
92-
Parameters
93-
----------
94-
title : str
95-
Heading string to validate
96-
97-
Returns
98-
-------
99-
bool
100-
True if title capitalized correctly, False if not
101-
102-
"""
103-
104-
correct_title = correct_title_capitalization(title)
105-
106-
if title != correct_title:
107-
return False
108-
else:
109-
return True
110-
111-
11286
def find_titles(rst_file: str) -> Generator[Tuple[str, int], None, None]:
11387
"""
11488
Algorithm to identify particular text that should be considered headings in an
@@ -161,28 +135,6 @@ def find_titles(rst_file: str) -> Generator[Tuple[str, int], None, None]:
161135
yield lines[line_no - 1].translate(table), line_no
162136

163137

164-
def fill_bad_title_dict(rst_file: str) -> None:
165-
"""
166-
Method that fills up the bad_title_dict with incorrectly capitalized headings
167-
168-
Parameters
169-
----------
170-
rst_file : str
171-
Directory address of a .rst file as a string
172-
173-
"""
174-
175-
if rst_file in bad_title_dict:
176-
return
177-
178-
for title, line_number in find_titles(rst_file):
179-
if not is_following_capitalization_convention(title):
180-
if rst_file not in bad_title_dict:
181-
bad_title_dict[rst_file] = [(title, line_number)]
182-
else:
183-
bad_title_dict[rst_file].append((title, line_number))
184-
185-
186138
def find_rst_files(source_paths: List[str]) -> Generator[str, None, None]:
187139
"""
188140
Given the command line arguments of directory paths, this method
@@ -234,21 +186,14 @@ def main(source_paths: List[str], output_format: str) -> bool:
234186

235187
number_of_errors: int = 0
236188

237-
directory_list = find_rst_files(source_paths)
238-
239-
for filename in directory_list:
240-
fill_bad_title_dict(filename)
241-
242-
if len(bad_title_dict) == 0:
243-
return number_of_errors
244-
245-
for key in bad_title_dict:
246-
for line in bad_title_dict[key]:
247-
print(
248-
f"""{key}:{line[1]}:{err_msg} "{line[0]}" to "{
249-
correct_title_capitalization(line[0])}" """
250-
)
251-
number_of_errors += 1
189+
for filename in find_rst_files(source_paths):
190+
for title, line_number in find_titles(filename):
191+
if title != correct_title_capitalization(title):
192+
print(
193+
f"""{filename}:{line_number}:{err_msg} "{title}" to "{
194+
correct_title_capitalization(title)}" """
195+
)
196+
number_of_errors += 1
252197

253198
return number_of_errors
254199

0 commit comments

Comments
 (0)