1818#
1919# ==========================================================================*/
2020
21+ """ spell check the comments in code. """
22+
2123import sys
2224import os
2325import fnmatch
3335
3436from comment_parser import comment_parser
3537
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
3742
3843__version__ = "unknown"
3944
6166CONTRACTIONS = ["'d" , "'s" , "'th" ]
6267
6368
64- def splitCamelCase (word ):
69+ def split_camel_case (word ):
6570 """Split a camel case string into individual words."""
6671
6772 result = []
@@ -75,16 +80,16 @@ def splitCamelCase(word):
7580 current_word = ""
7681 current_word = current_word + x
7782
78- if len (current_word ):
83+ if len (current_word ) > 0 :
7984 result .append (current_word )
8085
8186 return result
8287
8388
84- def getMimeType (filepath ):
89+ def get_mime_type (filepath ):
8590 """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" )
8893
8994
9095def load_text_file (filename ):
@@ -96,7 +101,7 @@ def load_text_file(filename):
96101
97102 output = []
98103 lc = 0
99- with open (filename ) as fp :
104+ with open (filename , encoding = "utf-8" ) as fp :
100105 for line in fp :
101106 line = line .strip ()
102107 lc = lc + 1
@@ -116,7 +121,7 @@ def spell_check_words(spell_checker: SpellChecker, words: list[str]):
116121def spell_check_comment (
117122 spell_checker : SpellChecker ,
118123 c : comment_parser .common .Comment ,
119- prefixes : list [str ] = [] ,
124+ prefixes : list [str ] = None ,
120125 output_lvl = 2 ,
121126) -> list [str ]:
122127 """Check comment and return list of identified issues if any."""
@@ -151,8 +156,12 @@ def spell_check_comment(
151156 if valid :
152157 continue
153158
159+ if prefixes is None :
160+ prefixes = []
161+
154162 # Check if the bad word starts with a prefix.
155163 # If so, spell check the word without that prefix.
164+
156165 for pre in prefixes :
157166 if error_word .startswith (pre ):
158167 # check if the word is only the prefix
@@ -169,18 +178,17 @@ def spell_check_comment(
169178 try :
170179 if spell_checker .check (wrd ):
171180 valid = True
172- break
173181 else :
174182 # Try splitting camel case words and checking each sub-words
175183 if output_lvl > 1 :
176184 print ("Trying splitting camel case word: {wrd}" )
177- sub_words = splitCamelCase (wrd )
185+ sub_words = split_camel_case (wrd )
178186 if len (sub_words ) > 1 and spell_check_words (
179187 spell_checker , sub_words
180188 ):
181189 valid = True
182190 break
183- except BaseException :
191+ except TypeError :
184192 print (f"Caught an exception for word { error_word } { wrd } " )
185193
186194 if valid :
@@ -189,7 +197,7 @@ def spell_check_comment(
189197 # Try splitting camel case words and checking each sub-word
190198 if output_lvl > 1 :
191199 print (f"Trying splitting camel case word: { error_word } " )
192- sub_words = splitCamelCase (error_word )
200+ sub_words = get_mime_type (error_word )
193201 if len (sub_words ) > 1 and spell_check_words (spell_checker , sub_words ):
194202 continue
195203
@@ -202,11 +210,11 @@ def spell_check_comment(
202210 return mistakes
203211
204212
205- def spell_check_file (filename , spell_checker , mime_type = "" , output_lvl = 1 , prefixes = [] ):
213+ def spell_check_file (filename , spell_checker , mime_type = "" , output_lvl = 1 , prefixes = None ):
206214 """Check spelling in ``filename``."""
207215
208216 if len (mime_type ) == 0 :
209- mime_type = getMimeType (filename )
217+ mime_type = get_mime_type (filename )
210218
211219 if output_lvl > 0 :
212220 print (f"spell_check_file: { filename } , { mime_type } " )
@@ -217,7 +225,7 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
217225 else :
218226 try :
219227 clist = comment_parser .extract_comments (filename , mime = mime_type )
220- except BaseException :
228+ except TypeError :
221229 print (f"Parser failed, skipping file { filename } " )
222230 return []
223231
@@ -227,7 +235,7 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
227235 mistakes = spell_check_comment (
228236 spell_checker , c , prefixes = prefixes , output_lvl = output_lvl
229237 )
230- if len (mistakes ):
238+ if len (mistakes ) > 0 :
231239 if output_lvl > 0 :
232240 print (f"\n Line number { c .line_number ()} " )
233241 if output_lvl > 0 :
@@ -253,7 +261,7 @@ def exclude_check(name, exclude_list):
253261 if exclude_list is None :
254262 return False
255263 for pattern in exclude_list :
256- match = re .findall ("%s" % pattern , name )
264+ match = re .findall (pattern , name )
257265 if len (match ) > 0 :
258266 return True
259267 return False
@@ -271,6 +279,7 @@ def skip_check(name, skip_list):
271279
272280
273281def parse_args ():
282+ """parse the command-line arguments."""
274283 parser = argparse .ArgumentParser ()
275284
276285 parser .add_argument ("filenames" , nargs = "*" )
@@ -398,7 +407,7 @@ def add_dict(enchant_dict, filename, verbose=False):
398407 if verbose :
399408 print (f"Additional dictionary: { filename } " )
400409
401- with open (filename ) as f :
410+ with open (filename , encoding = "utf-8" ) as f :
402411 lines = f .read ().splitlines ()
403412
404413 # You better not have more than 1 word in a line
@@ -442,6 +451,7 @@ def create_spell_checker(args, output_lvl):
442451
443452
444453def main ():
454+ """comment_spell_check main function."""
445455 args = parse_args ()
446456
447457 # Set the amount of debugging messages to print.
0 commit comments