24
24
25
25
# Keynames that would not follow capitalization convention
26
26
CAPITALIZATION_EXCEPTIONS = {
27
- 'pandas' , 'Python' , 'IPython' , 'PyTables' , 'Excel' , 'JSON' ,
28
- 'HTML' , 'SAS' , 'SQL' , 'BigQuery' , 'STATA' , 'Interval' , 'PEP8' ,
29
- 'Period' , 'Series' , 'Index' , 'DataFrame' , 'C' , 'Git' , 'GitHub' , 'NumPy' ,
30
- 'Apache' , 'Arrow' , 'Parquet' , 'MultiIndex' , 'NumFOCUS' , 'sklearn-pandas'
27
+ "pandas" ,
28
+ "Python" ,
29
+ "IPython" ,
30
+ "PyTables" ,
31
+ "Excel" ,
32
+ "JSON" ,
33
+ "HTML" ,
34
+ "SAS" ,
35
+ "SQL" ,
36
+ "BigQuery" ,
37
+ "STATA" ,
38
+ "Interval" ,
39
+ "PEP8" ,
40
+ "Period" ,
41
+ "Series" ,
42
+ "Index" ,
43
+ "DataFrame" ,
44
+ "C" ,
45
+ "Git" ,
46
+ "GitHub" ,
47
+ "NumPy" ,
48
+ "Apache" ,
49
+ "Arrow" ,
50
+ "Parquet" ,
51
+ "MultiIndex" ,
52
+ "NumFOCUS" ,
53
+ "sklearn" ,
54
+ "Docker" ,
31
55
}
32
56
33
57
# Lowercase representation of CAPITALIZATION_EXCEPTIONS
38
62
bad_title_dict = {}
39
63
40
64
# Error Message:
41
- err_msg = ' Heading capitalization formatted incorrectly. Please correctly capitalize'
65
+ err_msg = " Heading capitalization formatted incorrectly. Please correctly capitalize"
42
66
43
67
44
68
def follow_capitalization_convention (title : str ) -> bool :
45
- '''
69
+ """
46
70
Algorithm to determine if a heading follows the capitalization convention
47
71
48
72
This method returns true if the title follows the convention
@@ -58,13 +82,13 @@ def follow_capitalization_convention(title: str) -> bool:
58
82
bool
59
83
True if capitalization is correct, False if not
60
84
61
- '''
85
+ """
62
86
63
87
# split with delimiters comma, semicolon and space, parentheses, colon, slashes
64
- word_list = re .split (r' [;,/():\s]\s*' , title )
88
+ word_list = re .split (r" [;,- /():\s]\s*" , title )
65
89
66
90
# Edge Case: First word is an empty string
67
- if ( len (word_list [0 ]) == 0 ) :
91
+ if len (word_list [0 ]) == 0 :
68
92
return False
69
93
70
94
# Dealing with the first word of the title
@@ -73,7 +97,7 @@ def follow_capitalization_convention(title: str) -> bool:
73
97
if word_list [0 ].lower () in CAPITALIZATION_EXCEPTIONS_LOWER :
74
98
return False
75
99
# First letter of first word must be uppercase
76
- if ( not word_list [0 ][0 ].isupper () ):
100
+ if not word_list [0 ][0 ].isupper ():
77
101
return False
78
102
# Remaining letters of first word must not be uppercase
79
103
for j in range (1 , len (word_list [0 ])):
@@ -96,7 +120,7 @@ def follow_capitalization_convention(title: str) -> bool:
96
120
97
121
98
122
def findTitles (rst_file : str ) -> Generator [List [str ], List [int ], None ]:
99
- '''
123
+ """
100
124
Algorithm to identify particular text that should be considered headings in an
101
125
RST file
102
126
@@ -116,7 +140,7 @@ def findTitles(rst_file: str) -> Generator[List[str], List[int], None]:
116
140
line_number_list : List[int]
117
141
The corresponding line numbers of the headings in title_list
118
142
119
- '''
143
+ """
120
144
121
145
# title_list is the list of headings that is encountered in the doctree
122
146
title_list : List [str ] = []
@@ -126,47 +150,52 @@ def findTitles(rst_file: str) -> Generator[List[str], List[int], None]:
126
150
127
151
# Open and read the .rst file and store the string of data into input
128
152
f = open (rst_file , "r" )
129
- input = f .read ().split ('\n ' )
153
+ input = f .read ().split ("\n " )
154
+ f .close ()
130
155
131
156
# Regular expressions that denote a title beforehand
132
157
regex = {
133
- '*' : r'^(?:\*{1})*$' , '=' : r'^(?:={1})*$' , '-' : r'^(?:-{1})*$' ,
134
- '^' : r'^(?:\^{1})*$' , '~' : r'^(?:~{1})*$' , '#' : r'^(?:#{1})*$' ,
135
- '"' : r'^(?:"{1})*$'
158
+ "*" : r"^(?:\*{1})*$" ,
159
+ "=" : r"^(?:={1})*$" ,
160
+ "-" : r"^(?:-{1})*$" ,
161
+ "^" : r"^(?:\^{1})*$" ,
162
+ "~" : r"^(?:~{1})*$" ,
163
+ "#" : r"^(?:#{1})*$" ,
164
+ '"' : r'^(?:"{1})*$' ,
136
165
}
137
166
138
167
# '*`_' markers are removed from original string text.
139
- table = str .maketrans ("" , "" , ' *`_' )
168
+ table = str .maketrans ("" , "" , " *`_" )
140
169
141
170
# Loop through input lines, appending if they are considered headings
142
171
for lineno in range (1 , len (input )):
143
- if ( len (input [lineno ]) != 0 and len (input [lineno - 1 ]) != 0 ) :
172
+ if len (input [lineno ]) != 0 and len (input [lineno - 1 ]) != 0 :
144
173
for key in regex :
145
174
match = re .search (regex [key ], input [lineno ])
146
- if ( match is not None ) :
147
- if ( lineno >= 2 ) :
148
- if ( input [lineno ] == input [lineno - 2 ]) :
149
- if ( len (input [lineno ]) == len (input [lineno - 1 ]) ):
175
+ if match is not None :
176
+ if lineno >= 2 :
177
+ if input [lineno ] == input [lineno - 2 ]:
178
+ if len (input [lineno ]) == len (input [lineno - 1 ]):
150
179
title_list .append (input [lineno - 1 ].translate (table ))
151
180
line_number_list .append (lineno )
152
181
break
153
- if ( len (input [lineno ]) >= len (input [lineno - 1 ]) ):
182
+ if len (input [lineno ]) >= len (input [lineno - 1 ]):
154
183
title_list .append (input [lineno - 1 ].translate (table ))
155
184
line_number_list .append (lineno )
156
185
157
186
return title_list , line_number_list
158
187
159
188
160
189
def fill_bad_title_dict (rst_file : str ) -> None :
161
- '''
190
+ """
162
191
Method that fills up the bad_title_dict with incorrectly capitalized headings
163
192
164
193
Parameters
165
194
----------
166
195
rst_file : str
167
196
Directory address of a .rst file as a string
168
197
169
- '''
198
+ """
170
199
171
200
# Ensure this file doesn't already have a bad_title_dict slot
172
201
if rst_file in bad_title_dict :
@@ -185,7 +214,7 @@ def fill_bad_title_dict(rst_file: str) -> None:
185
214
186
215
187
216
def find_rst_files (source_paths : List [str ]) -> List [str ]:
188
- '''
217
+ """
189
218
Given the command line arguments of directory paths, this method
190
219
yields the strings of the .rst file directories that these paths contain
191
220
@@ -199,15 +228,15 @@ def find_rst_files(source_paths: List[str]) -> List[str]:
199
228
directory_address : str
200
229
Directory address of a .rst files found in command line argument directories
201
230
202
- '''
231
+ """
203
232
204
233
# Loop through source_paths, recursively looking for .rst files
205
234
for directory_address in source_paths :
206
235
if not os .path .exists (directory_address ):
207
236
raise ValueError (
208
237
"Please enter a valid path, pointing to a valid file/directory."
209
238
)
210
- elif ( directory_address .endswith (".rst" ) ):
239
+ elif directory_address .endswith (".rst" ):
211
240
yield directory_address
212
241
else :
213
242
for (dirpath , dirnames , filenames ) in walk (directory_address ):
@@ -217,7 +246,7 @@ def find_rst_files(source_paths: List[str]) -> List[str]:
217
246
218
247
219
248
def main (source_paths : List [str ], output_format : str ) -> bool :
220
- '''
249
+ """
221
250
The main method to print all headings with incorrect capitalization
222
251
223
252
Parameters
@@ -232,9 +261,9 @@ def main(source_paths: List[str], output_format: str) -> bool:
232
261
is_failed : bool
233
262
True if there are headings that are printed, False if not
234
263
235
- '''
264
+ """
236
265
237
- is_failed : bool = False
266
+ is_failed : bool = False
238
267
239
268
# Make a list of all RST files from command line directory list
240
269
directory_list = find_rst_files (source_paths )
@@ -245,23 +274,21 @@ def main(source_paths: List[str], output_format: str) -> bool:
245
274
fill_bad_title_dict (filename )
246
275
247
276
# Return an exit status of 0 if there are no bad titles in the dictionary
248
- if ( len (bad_title_dict ) == 0 ) :
277
+ if len (bad_title_dict ) == 0 :
249
278
return is_failed
250
279
251
280
# Print bad_title_dict Results
252
- print ()
281
+ is_failed = True
253
282
for key in bad_title_dict :
254
283
for line in bad_title_dict [key ]:
255
- print (
256
- key + ":" + str (line [1 ]) + ": " + err_msg + " \" " + line [0 ] + "\" "
257
- )
284
+ print (key + ":" + str (line [1 ]) + ": " + err_msg + ' "' + line [0 ] + '"' )
258
285
259
286
# Exit status of 0
260
287
return is_failed
261
288
262
289
263
290
if __name__ == "__main__" :
264
- parser = argparse .ArgumentParser (description = ' Validate heading capitalization' )
291
+ parser = argparse .ArgumentParser (description = " Validate heading capitalization" )
265
292
266
293
parser .add_argument (
267
294
"paths" , nargs = "+" , default = "." , help = "Source paths of file/directory to check."
0 commit comments