Skip to content

Commit 4cca351

Browse files
committed
Add root dir to sys.path
Also moved command line arguement parsing down to utils subdir. And changed tests to use 'sys.executable' instead of 'python' when run.
1 parent 6c583df commit 4cca351

3 files changed

Lines changed: 160 additions & 140 deletions

File tree

comment_spell_check/comment_spell_check.py

Lines changed: 16 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import os
2525
import fnmatch
2626
import glob
27-
import argparse
2827
import re
2928
import unicodedata
3029
import logging
@@ -35,16 +34,16 @@
3534

3635
from spellchecker import SpellChecker
3736

38-
try:
39-
# This loads the modules from the installed package
40-
from comment_spell_check.utils import bibtex_loader
41-
from comment_spell_check.utils import create_checker
42-
from comment_spell_check.utils import url_remove
43-
except ImportError:
44-
# This loads the modules from the source directory
45-
from utils import bibtex_loader
46-
from utils import create_checker
47-
from utils import url_remove
37+
38+
# Insert the root directory of the project to the system path
39+
current_script_path = os.path.abspath(__file__)
40+
parent_directory = os.path.dirname(current_script_path)
41+
sys.path.insert(0, parent_directory + "/../")
42+
43+
from comment_spell_check.utils import parseargs
44+
from comment_spell_check.utils import bibtex_loader
45+
from comment_spell_check.utils import create_checker
46+
from comment_spell_check.utils import url_remove
4847

4948
__version__ = "unknown"
5049

@@ -313,129 +312,6 @@ def skip_check(name: str, skip_list: list[str] = None):
313312
return False
314313

315314

316-
def parse_args():
317-
"""parse the command-line arguments."""
318-
parser = argparse.ArgumentParser()
319-
320-
parser.add_argument("filenames", nargs="*")
321-
322-
parser.add_argument(
323-
"--brief",
324-
"-b",
325-
action="store_true",
326-
default=False,
327-
dest="brief",
328-
help="Make output brief",
329-
)
330-
331-
parser.add_argument(
332-
"--verbose",
333-
"-v",
334-
action="store_true",
335-
default=False,
336-
dest="verbose",
337-
help="Make output verbose",
338-
)
339-
340-
parser.add_argument(
341-
"--first",
342-
"-f",
343-
action="store_true",
344-
default=False,
345-
dest="first",
346-
help="Show only first occurrence of a mispelling",
347-
)
348-
349-
parser.add_argument(
350-
"--vim",
351-
"-V",
352-
action="store_true",
353-
default=False,
354-
dest="vim",
355-
help="Output results in vim command format",
356-
)
357-
358-
parser.add_argument(
359-
"--dict",
360-
"-d",
361-
"--ignore-words",
362-
"-I",
363-
action="append",
364-
dest="dict",
365-
help="File that contains words that will be ignored."
366-
" Argument can be passed multiple times."
367-
" File must contain 1 word per line.",
368-
)
369-
370-
parser.add_argument(
371-
"--exclude",
372-
"-e",
373-
action="append",
374-
dest="exclude",
375-
help="Specify regex for excluding files."
376-
" Argument can be passed multiple times.",
377-
)
378-
379-
parser.add_argument(
380-
"--skip",
381-
"-S",
382-
action="append",
383-
help="Comma-separated list of files to skip. It "
384-
"accepts globs as well. E.g.: if you want "
385-
"coment_spell_check.py to skip .eps and .txt files, "
386-
'you\'d give "*.eps,*.txt" to this option.'
387-
" Argument can be passed multiple times.",
388-
)
389-
390-
parser.add_argument(
391-
"--prefix",
392-
"-p",
393-
action="append",
394-
default=[],
395-
dest="prefixes",
396-
help="Add word prefix. Argument can be passed multiple times.",
397-
)
398-
399-
parser.add_argument(
400-
"--miss",
401-
"-m",
402-
action="store_true",
403-
default=False,
404-
dest="miss",
405-
help="Only output the misspelt words",
406-
)
407-
408-
parser.add_argument(
409-
"--suffix",
410-
"-s",
411-
action="append",
412-
default=[".h"],
413-
dest="suffix",
414-
help="File name suffix. Argument can be passed multiple times.",
415-
)
416-
417-
parser.add_argument(
418-
"--type",
419-
"-t",
420-
action="store",
421-
default="",
422-
dest="mime_type",
423-
help="Set file mime type. File name suffix will be ignored.",
424-
)
425-
426-
parser.add_argument(
427-
"--bibtex",
428-
action="append",
429-
dest="bibtex",
430-
help="Bibtex file to load for additional dictionary words.",
431-
)
432-
433-
parser.add_argument("--version", action="version", version=f"{__version__}")
434-
435-
args = parser.parse_args()
436-
return args
437-
438-
439315
def build_dictionary_list(args):
440316
"""build a list of dictionaries to use for spell checking."""
441317
dict_list = []
@@ -535,9 +411,8 @@ def setup_logger(args):
535411
return logger
536412

537413

538-
def main():
414+
def comment_spell_check(args):
539415
"""comment_spell_check main function."""
540-
args = parse_args()
541416
logger = setup_logger(args)
542417

543418
dict_list = build_dictionary_list(args)
@@ -618,5 +493,10 @@ def main():
618493
sys.exit(len(bad_words))
619494

620495

496+
def main():
497+
parser = parseargs.create_parser()
498+
args = parseargs.parse_args()
499+
comment_spell_check(args)
500+
621501
if __name__ == "__main__":
622502
main()
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
import argparse
3+
from importlib.metadata import version, PackageNotFoundError
4+
5+
__version__ = "unknown"
6+
7+
try:
8+
__version__ = version("comment_spell_check")
9+
except PackageNotFoundError:
10+
# package is not installed
11+
pass
12+
13+
14+
def create_parser():
15+
parser = argparse.ArgumentParser()
16+
17+
parser.add_argument("filenames", nargs="*")
18+
19+
parser.add_argument(
20+
"--brief",
21+
"-b",
22+
action="store_true",
23+
default=False,
24+
dest="brief",
25+
help="Make output brief",
26+
)
27+
28+
parser.add_argument(
29+
"--verbose",
30+
"-v",
31+
action="store_true",
32+
default=False,
33+
dest="verbose",
34+
help="Make output verbose",
35+
)
36+
37+
parser.add_argument(
38+
"--first",
39+
"-f",
40+
action="store_true",
41+
default=False,
42+
dest="first",
43+
help="Show only first occurrence of a mispelling",
44+
)
45+
46+
parser.add_argument(
47+
"--vim",
48+
"-V",
49+
action="store_true",
50+
default=False,
51+
dest="vim",
52+
help="Output results in vim command format",
53+
)
54+
55+
parser.add_argument(
56+
"--dict",
57+
"-d",
58+
"--ignore-words",
59+
"-I",
60+
action="append",
61+
dest="dict",
62+
help="File that contains words that will be ignored."
63+
" Argument can be passed multiple times."
64+
" File must contain 1 word per line.",
65+
)
66+
67+
parser.add_argument(
68+
"--exclude",
69+
"-e",
70+
action="append",
71+
dest="exclude",
72+
help="Specify regex for excluding files."
73+
" Argument can be passed multiple times.",
74+
)
75+
76+
parser.add_argument(
77+
"--skip",
78+
"-S",
79+
action="append",
80+
help="Comma-separated list of files to skip. It "
81+
"accepts globs as well. E.g.: if you want "
82+
"coment_spell_check.py to skip .eps and .txt files, "
83+
'you\'d give "*.eps,*.txt" to this option.'
84+
" Argument can be passed multiple times.",
85+
)
86+
87+
parser.add_argument(
88+
"--prefix",
89+
"-p",
90+
action="append",
91+
default=[],
92+
dest="prefixes",
93+
help="Add word prefix. Argument can be passed multiple times.",
94+
)
95+
96+
parser.add_argument(
97+
"--miss",
98+
"-m",
99+
action="store_true",
100+
default=False,
101+
dest="miss",
102+
help="Only output the misspelt words",
103+
)
104+
105+
parser.add_argument(
106+
"--suffix",
107+
"-s",
108+
action="append",
109+
default=[".h"],
110+
dest="suffix",
111+
help="File name suffix. Argument can be passed multiple times.",
112+
)
113+
114+
parser.add_argument(
115+
"--type",
116+
"-t",
117+
action="store",
118+
default="",
119+
dest="mime_type",
120+
help="Set file mime type. File name suffix will be ignored.",
121+
)
122+
123+
parser.add_argument(
124+
"--bibtex",
125+
action="append",
126+
dest="bibtex",
127+
help="Bibtex file to load for additional dictionary words.",
128+
)
129+
130+
parser.add_argument("--version", action="version", version=f"{__version__}")
131+
return parser
132+
133+
def parse_args(parser=create_parser()):
134+
"""parse the command-line arguments."""
135+
136+
args = parser.parse_args()
137+
return args
138+

tests/test_comment_spell_check.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#
1717
# ==========================================================================*/
1818

19+
import sys
20+
import os
1921
import unittest
2022
import subprocess
2123

@@ -33,7 +35,7 @@ def test_basic(self):
3335
"""Basic test"""
3436
runresult = subprocess.run(
3537
[
36-
"python",
38+
sys.executable,
3739
"comment_spell_check.py",
3840
"--miss",
3941
"--dict",
@@ -51,7 +53,7 @@ def test_codebase(self):
5153
"""Code base test"""
5254
runresult = subprocess.run(
5355
[
54-
"python",
56+
sys.executable,
5557
"comment_spell_check.py",
5658
"--verbose",
5759
"--prefix",
@@ -71,7 +73,7 @@ def test_version(self):
7173
"""Version test"""
7274
runresult = subprocess.run(
7375
[
74-
"python",
76+
sys.executable,
7577
"comment_spell_check.py",
7678
"--version",
7779
],
@@ -89,7 +91,7 @@ def test_bibtex(self):
8991
"""Bibtext test"""
9092
runresult = subprocess.run(
9193
[
92-
"python",
94+
sys.executable,
9395
"comment_spell_check.py",
9496
"--bibtex",
9597
"../tests/itk.bib",

0 commit comments

Comments
 (0)