Skip to content

Commit 9a33322

Browse files
committed
Fixed logger stuff, some pylint changes
1 parent cb0bfe9 commit 9a33322

File tree

3 files changed

+44
-87
lines changed

3 files changed

+44
-87
lines changed

comment_spell_check/comment_spell_check.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,28 @@ def spell_check_words(spell_checker: SpellChecker, words: list[str]):
167167
return True
168168

169169

170-
def find_misspellings(
171-
spell: SpellChecker, line: str, logger: logging.Logger
172-
) -> list[str]:
170+
def find_misspellings(spell: SpellChecker, line: str) -> list[str]:
173171
"""Find misspellings in a line of text."""
174172

173+
logger = logging.getLogger("comment_spell_check")
175174
words = filter_string(line)
176175

177176
mistakes = []
178177

179178
for word in words:
180179
if not (word.lower() in spell or word in spell):
181-
logger.info(f"Misspelled word: {word}")
180+
logger.info("Misspelled word: %s", word)
182181
mistakes.append(word)
183182
return mistakes
184183

185184

186-
def remove_contractions(word: str, logger: logging.Logger):
185+
def remove_contractions(word: str):
187186
"""Remove contractions from the word."""
187+
188+
logger = logging.getLogger("comment_spell_check")
188189
for contraction in CONTRACTIONS:
189190
if word.endswith(contraction):
190-
logger.info(f"Contraction: {word} -> {word[: -len(contraction)]}")
191+
logger.info("Contraction: %s -> %s", word, word[: -len(contraction)])
191192
return word[: -len(contraction)]
192193
return word
193194

@@ -203,25 +204,25 @@ def remove_prefix(word: str, prefixes: list[str]):
203204
def spell_check_comment(
204205
spell: SpellChecker,
205206
c: comment_parser.common.Comment,
206-
logger: logging.Logger,
207207
prefixes: list[str] = None,
208208
) -> list[str]:
209209
"""Check comment and return list of identified issues if any."""
210210

211-
logger.info(f"Line {c.line_number()}: {c}")
211+
logger = logging.getLogger("comment_spell_check")
212+
logger.info("Line #%d: %s", c.line_number(), c.text())
212213

213214
line = c.text()
214215
if "https://" in line or "http://" in line:
215216
line = url_remove.remove_urls(line)
216-
logger.debug(f" Removed URLs: {line}")
217+
logger.debug(" Removed URLs: %s", line)
217218

218-
bad_words = find_misspellings(spell, line, logger)
219+
bad_words = find_misspellings(spell, line)
219220

220221
mistakes = []
221222
for error_word in bad_words:
222-
logger.debug(f" Error: {error_word}")
223+
logger.debug(" Error: %s", error_word)
223224

224-
error_word = remove_contractions(error_word, logger)
225+
error_word = remove_contractions(error_word)
225226

226227
prefixes = prefixes or []
227228
error_word = remove_prefix(error_word, prefixes)
@@ -231,10 +232,8 @@ def spell_check_comment(
231232

232233
# Try splitting camel case words and checking each sub-word
233234
sub_words = split_camel_case(error_word)
234-
logger.debug(
235-
f" Trying splitting camel case word: {error_word}\n"
236-
+ f" Sub-words: {sub_words}\n"
237-
)
235+
logger.debug( " Trying splitting camel case word: %s", error_word)
236+
logger.debug( " Sub-words: %s", sub_words)
238237

239238
if len(sub_words) > 1 and spell_check_words(spell, sub_words):
240239
continue
@@ -248,7 +247,6 @@ def spell_check_comment(
248247
def spell_check_file(
249248
filename: str,
250249
spell_checker: SpellChecker,
251-
logger: logging.Logger,
252250
mime_type: str = "",
253251
prefixes=None,
254252
):
@@ -257,7 +255,8 @@ def spell_check_file(
257255
if len(mime_type) == 0:
258256
mime_type = get_mime_type(filename)
259257

260-
logger.info(f"spell_check_file: {filename}, {mime_type}")
258+
logger = logging.getLogger("comment_spell_check")
259+
logger.info("spell_check_file: %s, %s", filename, mime_type)
261260

262261
# Returns a list of comment_parser.parsers.common.Comments
263262
if mime_type == "text/plain":
@@ -266,19 +265,19 @@ def spell_check_file(
266265
try:
267266
clist = comment_parser.extract_comments(filename, mime=mime_type)
268267
except TypeError:
269-
logger.error(f"Parser failed, skipping file {filename}")
268+
logger.error("Parser failed, skipping file %s", filename)
270269
return []
271270

272271
bad_words = []
273272
line_count = 0
274273

275274
for c in clist:
276-
mistakes = spell_check_comment(spell_checker, c, logger, prefixes=prefixes)
275+
mistakes = spell_check_comment(spell_checker, c, prefixes=prefixes)
277276
if len(mistakes) > 0:
278-
logger.info(f"\nLine number {c.line_number()}")
277+
logger.info("\nLine number %s", c.line_number())
279278
logger.info(c.text())
280279
for m in mistakes:
281-
logger.info(f" {m}")
280+
logger.info(" %s", m)
282281
bad_words.append([m, filename, c.line_number()])
283282
line_count = line_count + 1
284283

@@ -437,15 +436,16 @@ def parse_args():
437436
return args
438437

439438

440-
def build_dictionary_list(args, logger):
439+
def build_dictionary_list(args):
441440
"""build a list of dictionaries to use for spell checking."""
442441
dict_list = []
443442
initial_dct = Path(__file__).parent / "additional_dictionary.txt"
444443

444+
logger = logging.getLogger("comment_spell_check")
445445
if initial_dct.exists():
446446
dict_list.append(initial_dct)
447447
else:
448-
logger.warning(f"Initial dictionary not found: {initial_dct}")
448+
logger.warning("Initial dictionary not found: %s", initial_dct)
449449

450450
if not isinstance(args.dict, list):
451451
return dict_list
@@ -458,17 +458,17 @@ def build_dictionary_list(args, logger):
458458
return dict_list
459459

460460

461-
def add_bibtex_words(
462-
spell: SpellChecker, bibtex_files: list = None, logger: logging.Logger = None
463-
):
461+
def add_bibtex_words(spell: SpellChecker, bibtex_files: list[str]):
464462
"""Add words from bibtex files to the spell checker."""
465463

466464
if list is None:
467465
return
468466

467+
logger = logging.getLogger("comment_spell_check")
468+
469469
for bibtex_file in bibtex_files:
470-
logger.info(f"Loading bibtex file: {bibtex_file}")
471-
bibtex_loader.add_bibtex(spell, bibtex_file, logger)
470+
logger.info("Loading bibtex file: %s", bibtex_file)
471+
bibtex_loader.add_bibtex(spell, bibtex_file)
472472

473473

474474
def output_results(args, bad_words):
@@ -499,7 +499,7 @@ def output_results(args, bad_words):
499499
print(f"\n{len(bad_words)} misspellings found")
500500

501501

502-
def setup_logger(name, args):
502+
def setup_logger(args):
503503
"""Sets up a logger that outputs to the console."""
504504

505505
level = logging.INFO
@@ -510,7 +510,7 @@ def setup_logger(name, args):
510510
if args.brief:
511511
level = logging.WARNING
512512

513-
logger = logging.getLogger(name)
513+
logger = logging.getLogger("comment_spell_check")
514514
logger.setLevel(level)
515515

516516
# Create a console handler
@@ -534,14 +534,14 @@ def setup_logger(name, args):
534534
def main():
535535
"""comment_spell_check main function."""
536536
args = parse_args()
537-
logger = setup_logger("comment_spell_check", args)
537+
logger = setup_logger(args)
538538

539-
dict_list = build_dictionary_list(args, logger)
539+
dict_list = build_dictionary_list(args)
540540

541-
spell = create_checker.create_checker(dict_list, logger)
541+
spell = create_checker.create_checker(dict_list)
542542

543543
if args.bibtex:
544-
add_bibtex_words(spell, args.bibtex, logger)
544+
add_bibtex_words(spell, args.bibtex)
545545

546546
file_list = []
547547
if len(args.filenames):
@@ -583,7 +583,6 @@ def main():
583583
result, lc = spell_check_file(
584584
x,
585585
spell,
586-
logger,
587586
args.mime_type,
588587
prefixes=prefixes,
589588
)
@@ -601,7 +600,6 @@ def main():
601600
result, lc = spell_check_file(
602601
f,
603602
spell,
604-
logger,
605603
args.mime_type,
606604
prefixes=prefixes,
607605
)

comment_spell_check/utils/bibtex_loader.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ def split_bibtex_name(name: str):
1919
return words
2020

2121

22-
def add_bibtex(spell: spellchecker.SpellChecker, filename: str, logger: logging.Logger):
22+
def add_bibtex(spell: spellchecker.SpellChecker, filename: str):
2323
"""Update ``spell`` spell checking dictionary with names
2424
from ``filename``, a Bibtex file."""
2525

26-
logger.info(f"Bibtex file: {filename}")
26+
logger = logging.getLogger("comment_spell_check.bibtex_loader")
27+
logger.info("Bibtex file: %s", filename)
2728

2829
word_list = []
2930

@@ -34,5 +35,5 @@ def add_bibtex(spell: spellchecker.SpellChecker, filename: str, logger: logging.
3435
words = split_bibtex_name(k)
3536
word_list.extend(words)
3637

37-
logger.info(f"Words: {word_list}")
38+
logger.info("Words: %s", word_list)
3839
spell.word_frequency.load_words(word_list)

comment_spell_check/utils/create_checker.py

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,33 @@
22
additional dictionaries if provided.
33
"""
44

5-
import sys
65
import logging
76
import importlib.resources
87
import spellchecker
98

109

11-
def create_checker(dict_list: list[str] = None, logger: logging.Logger = None):
10+
def create_checker(dict_list: list[str] = None) -> spellchecker.SpellChecker:
1211
"""Create a case sensitive spell checker with the English dictionary and
1312
additional dictionaries if provided."""
1413

14+
logger = logging.getLogger("comment_spell_check.create_checker")
15+
1516
# create an empty SpellChecker object, because we want a case
1617
# sensitive checker
1718
checker = spellchecker.SpellChecker(language=None, case_sensitive=True)
1819

1920
# load the English dictionary
2021
lib_path = importlib.resources.files(spellchecker)
2122
english_dict = str(lib_path) + "/resources/en.json.gz"
22-
logger.info(f"Loading English dictionary from: {english_dict}")
23+
logger.info("Loading English dictionary from: %s", english_dict)
2324
checker.word_frequency.load_dictionary(english_dict)
2425

2526
# load the additional dictionaries
2627
if not isinstance(dict_list, list):
2728
return checker
2829
if len(dict_list) > 0:
2930
for d in dict_list:
30-
logger.info(f"Loading additional dictionary from: {d}")
31+
logger.info("Loading additional dictionary from: %s", d)
3132
checker.word_frequency.load_text_file(d)
3233

3334
return checker
34-
35-
36-
if __name__ == "__main__":
37-
38-
# set up logging
39-
lg = logging.getLogger(__name__)
40-
lg.setLevel(logging.INFO)
41-
ch = logging.StreamHandler()
42-
ch.setLevel(logging.INFO)
43-
lg.addHandler(ch)
44-
45-
lg.info("\nStarting spell checker")
46-
if len(sys.argv) > 1:
47-
lg.info("Arguments: %s", sys.argv[1:])
48-
49-
# create the spell checker
50-
spell = create_checker(sys.argv[1:], lg)
51-
52-
# find those words that may be misspelled
53-
misspelled = spell.unknown(["something", "is", "hapenning", "here"])
54-
55-
for word in misspelled:
56-
lg.warning("\nMisspelled: %s", word)
57-
# Get the one `most likely` answer
58-
lg.warning("Most likely word: %s", spell.correction(word))
59-
60-
# Get a list of `likely` options
61-
lg.warning("Candidate words: %s", spell.candidates(word))
62-
63-
# test if case sensitive checking from the additional dictionaries works
64-
my_words = [
65-
"Zsize",
66-
"Zuerich",
67-
"accesor",
68-
"accessor",
69-
"zsize",
70-
"zuerich",
71-
"sdfasdfas",
72-
]
73-
74-
lg.info("\nTesting case sensitive checking from the additional dictionaries")
75-
for w in my_words:
76-
lg.info("Checking word: %s, %s", w, w in spell)

0 commit comments

Comments
 (0)