18
18
#
19
19
# ==========================================================================*/
20
20
21
+ """ spell check the comments in code. """
22
+
21
23
import sys
22
24
import os
23
25
import fnmatch
33
35
34
36
from comment_parser import comment_parser
35
37
36
- from comment_spell_check .lib import bibtex_loader
38
+ try :
39
+ from comment_spell_check .lib import bibtex_loader
40
+ except ImportError :
41
+ from lib import bibtex_loader
37
42
38
43
__version__ = "unknown"
39
44
61
66
CONTRACTIONS = ["'d" , "'s" , "'th" ]
62
67
63
68
64
- def splitCamelCase (word ):
69
+ def split_camel_case (word ):
65
70
"""Split a camel case string into individual words."""
66
71
67
72
result = []
@@ -75,16 +80,16 @@ def splitCamelCase(word):
75
80
current_word = ""
76
81
current_word = current_word + x
77
82
78
- if len (current_word ):
83
+ if len (current_word ) > 0 :
79
84
result .append (current_word )
80
85
81
86
return result
82
87
83
88
84
- def getMimeType (filepath ):
89
+ def get_mime_type (filepath ):
85
90
"""Map ``filepath`` extension to file type."""
86
- name , ext = os .path .splitext (filepath )
87
- return SUFFIX2MIME .get (ext , "text/plain" )
91
+ parts = os .path .splitext (filepath )
92
+ return SUFFIX2MIME .get (parts [ 1 ] , "text/plain" )
88
93
89
94
90
95
def load_text_file (filename ):
@@ -96,7 +101,7 @@ def load_text_file(filename):
96
101
97
102
output = []
98
103
lc = 0
99
- with open (filename ) as fp :
104
+ with open (filename , encoding = "utf-8" ) as fp :
100
105
for line in fp :
101
106
line = line .strip ()
102
107
lc = lc + 1
@@ -116,7 +121,7 @@ def spell_check_words(spell_checker: SpellChecker, words: list[str]):
116
121
def spell_check_comment (
117
122
spell_checker : SpellChecker ,
118
123
c : comment_parser .common .Comment ,
119
- prefixes : list [str ] = [] ,
124
+ prefixes : list [str ] = None ,
120
125
output_lvl = 2 ,
121
126
) -> list [str ]:
122
127
"""Check comment and return list of identified issues if any."""
@@ -131,7 +136,7 @@ def spell_check_comment(
131
136
error_word = error .word
132
137
133
138
if output_lvl > 1 :
134
- print (f"Error: { error_word } " )
139
+ print (f" Error: { error_word } " )
135
140
136
141
valid = False
137
142
@@ -142,7 +147,8 @@ def spell_check_comment(
142
147
error_word = error_word [: - len (contraction )]
143
148
if output_lvl > 1 :
144
149
print (
145
- f"Stripping contraction: { original_error_word } -> { error_word } "
150
+ " Stripping contraction: "
151
+ + f"{ original_error_word } -> { error_word } "
146
152
)
147
153
if spell_checker .check (error_word ):
148
154
valid = True
@@ -151,62 +157,69 @@ def spell_check_comment(
151
157
if valid :
152
158
continue
153
159
160
+ if prefixes is None :
161
+ prefixes = []
162
+
154
163
# Check if the bad word starts with a prefix.
155
164
# If so, spell check the word without that prefix.
165
+
156
166
for pre in prefixes :
157
167
if error_word .startswith (pre ):
158
168
# check if the word is only the prefix
159
169
if len (pre ) == len (error_word ):
160
170
if output_lvl > 1 :
161
- print (f"Prefix '{ pre } ' matches word" )
171
+ print (f" Prefix '{ pre } ' matches word" )
162
172
valid = True
163
173
break
164
174
165
175
# remove the prefix
166
176
wrd = error_word [len (pre ) :]
167
177
if output_lvl > 1 :
168
- print (f"Trying without '{ pre } ' prefix: { error_word } -> { wrd } " )
178
+ print (f" Trying without '{ pre } ' prefix: { error_word } -> { wrd } " )
169
179
try :
170
180
if spell_checker .check (wrd ):
171
181
valid = True
172
- break
173
182
else :
174
183
# Try splitting camel case words and checking each sub-words
175
184
if output_lvl > 1 :
176
- print ("Trying splitting camel case word: {wrd}" )
177
- sub_words = splitCamelCase (wrd )
185
+ print (f" Trying splitting camel case word: { wrd } " )
186
+ sub_words = split_camel_case (wrd )
187
+ if output_lvl > 1 :
188
+ print (" Sub-words: " , sub_words )
178
189
if len (sub_words ) > 1 and spell_check_words (
179
190
spell_checker , sub_words
180
191
):
181
192
valid = True
182
193
break
183
- except BaseException :
184
- print (f"Caught an exception for word { error_word } { wrd } " )
194
+ except TypeError :
195
+ print (f" Caught an exception for word { error_word } { wrd } " )
185
196
186
197
if valid :
187
198
continue
188
199
189
200
# Try splitting camel case words and checking each sub-word
190
201
if output_lvl > 1 :
191
- print (f"Trying splitting camel case word: { error_word } " )
192
- sub_words = splitCamelCase (error_word )
202
+ print (f" Trying splitting camel case word: { error_word } " )
203
+ sub_words = split_camel_case (error_word )
193
204
if len (sub_words ) > 1 and spell_check_words (spell_checker , sub_words ):
194
205
continue
195
206
196
207
if output_lvl > 1 :
197
- msg = f"error : '{ error_word } ', suggestions: { spell_checker .suggest ()} "
208
+ msg = f" Error : '{ error_word } ', suggestions: { spell_checker .suggest ()} "
198
209
else :
199
210
msg = error_word
200
211
mistakes .append (msg )
201
212
202
213
return mistakes
203
214
204
215
205
- def spell_check_file (filename , spell_checker , mime_type = "" , output_lvl = 1 , prefixes = []):
216
+ def spell_check_file (
217
+ filename , spell_checker , mime_type = "" , output_lvl = 1 , prefixes = None
218
+ ):
206
219
"""Check spelling in ``filename``."""
207
220
208
221
if len (mime_type ) == 0 :
209
- mime_type = getMimeType (filename )
222
+ mime_type = get_mime_type (filename )
210
223
211
224
if output_lvl > 0 :
212
225
print (f"spell_check_file: { filename } , { mime_type } " )
@@ -217,7 +230,7 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
217
230
else :
218
231
try :
219
232
clist = comment_parser .extract_comments (filename , mime = mime_type )
220
- except BaseException :
233
+ except TypeError :
221
234
print (f"Parser failed, skipping file { filename } " )
222
235
return []
223
236
@@ -227,7 +240,7 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
227
240
mistakes = spell_check_comment (
228
241
spell_checker , c , prefixes = prefixes , output_lvl = output_lvl
229
242
)
230
- if len (mistakes ):
243
+ if len (mistakes ) > 0 :
231
244
if output_lvl > 0 :
232
245
print (f"\n Line number { c .line_number ()} " )
233
246
if output_lvl > 0 :
@@ -253,7 +266,7 @@ def exclude_check(name, exclude_list):
253
266
if exclude_list is None :
254
267
return False
255
268
for pattern in exclude_list :
256
- match = re .findall ("%s" % pattern , name )
269
+ match = re .findall (pattern , name )
257
270
if len (match ) > 0 :
258
271
return True
259
272
return False
@@ -271,6 +284,7 @@ def skip_check(name, skip_list):
271
284
272
285
273
286
def parse_args ():
287
+ """parse the command-line arguments."""
274
288
parser = argparse .ArgumentParser ()
275
289
276
290
parser .add_argument ("filenames" , nargs = "*" )
@@ -398,7 +412,7 @@ def add_dict(enchant_dict, filename, verbose=False):
398
412
if verbose :
399
413
print (f"Additional dictionary: { filename } " )
400
414
401
- with open (filename ) as f :
415
+ with open (filename , encoding = "utf-8" ) as f :
402
416
lines = f .read ().splitlines ()
403
417
404
418
# You better not have more than 1 word in a line
@@ -435,13 +449,14 @@ def create_spell_checker(args, output_lvl):
435
449
for bib in args .bibtex :
436
450
bibtex_loader .add_bibtex (my_dict , bib , any ([args .brief , output_lvl >= 0 ]))
437
451
438
- # Create the SpellChecker
452
+ # Create the spell checking object
439
453
spell_checker = SpellChecker (my_dict , filters = [EmailFilter , URLFilter ])
440
454
441
455
return spell_checker
442
456
443
457
444
458
def main ():
459
+ """comment_spell_check main function."""
445
460
args = parse_args ()
446
461
447
462
# Set the amount of debugging messages to print.
0 commit comments