Skip to content

Commit 7722805

Browse files
committed
Fixes for Pylint and test refactoring
The tests were changed to launch with the python executable and run in the comment_spell_check subdir.
1 parent 59d771f commit 7722805

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

comment_spell_check/comment_spell_check.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#
1919
# ==========================================================================*/
2020

21+
""" spell check the comments in code. """
22+
2123
import sys
2224
import os
2325
import fnmatch
@@ -33,7 +35,10 @@
3335

3436
from 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

@@ -61,7 +66,7 @@
6166
CONTRACTIONS = ["'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

9095
def 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]):
116121
def 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"\nLine 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

273281
def 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

444453
def main():
454+
"""comment_spell_check main function."""
445455
args = parse_args()
446456

447457
# Set the amount of debugging messages to print.

comment_spell_check/lib/bibtex_loader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import bibtexparser
21

2+
""" Load Bibtex files into a spell checking dictionary. """
3+
4+
import bibtexparser
35

46
def split_bibtex_name(name):
57
"""

tests/test_comment_spell_check.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ def test_basic(self):
3333
"""Basic test"""
3434
runresult = subprocess.run(
3535
[
36-
"comment_spell_check",
36+
"python",
37+
"comment_spell_check.py",
3738
"--miss",
3839
"--dict",
39-
"tests/dict.txt",
40+
"../tests/dict.txt",
4041
"--prefix",
4142
"myprefix",
42-
"tests/example.h",
43+
"../tests/example.h",
4344
],
45+
cwd="comment_spell_check",
4446
stdout=subprocess.PIPE,
4547
)
4648
self.assertEqual(runresult.returncode, 0, runresult.stdout)
@@ -49,7 +51,8 @@ def test_codebase(self):
4951
"""Code base test"""
5052
runresult = subprocess.run(
5153
[
52-
"comment_spell_check",
54+
"python",
55+
"comment_spell_check.py",
5356
"--miss",
5457
"--prefix",
5558
"myprefix",
@@ -59,6 +62,7 @@ def test_codebase(self):
5962
".md",
6063
".",
6164
],
65+
cwd="comment_spell_check",
6266
stdout=subprocess.PIPE,
6367
)
6468
self.assertEqual(runresult.returncode, 0, runresult.stdout)
@@ -67,9 +71,11 @@ def test_version(self):
6771
"""Version test"""
6872
runresult = subprocess.run(
6973
[
70-
"comment_spell_check",
74+
"python",
75+
"comment_spell_check.py",
7176
"--version",
7277
],
78+
cwd="comment_spell_check",
7379
stdout=subprocess.PIPE,
7480
)
7581
self.assertEqual(runresult.returncode, 0)
@@ -83,11 +89,13 @@ def test_bibtex(self):
8389
"""Bibtext test"""
8490
runresult = subprocess.run(
8591
[
86-
"comment_spell_check",
92+
"python",
93+
"comment_spell_check.py",
8794
"--bibtex",
88-
"tests/itk.bib",
89-
"tests/bibtest.py",
95+
"../tests/itk.bib",
96+
"../tests/bibtest.py",
9097
],
98+
cwd="comment_spell_check",
9199
stdout=subprocess.PIPE,
92100
)
93101
self.assertEqual(runresult.returncode, 0, runresult.stdout)

0 commit comments

Comments
 (0)