Skip to content

Commit 6e23e03

Browse files
committed
Refactor: Move some code to new files for reuse
No new code is introduced; only existing code is shuffled around and the functions moved are unchanged as well.
1 parent 8097264 commit 6e23e03

File tree

3 files changed

+104
-62
lines changed

3 files changed

+104
-62
lines changed

codespell_lib/_codespell.py

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
from ._version import ( # type: ignore[import-not-found]
4444
__version__ as VERSION, # noqa: N812
4545
)
46+
from .spellchecker import Misspelling, build_dict
47+
from ._text_util import fix_case
4648

4749
word_regex_def = r"[\w\-'’]+" # noqa: RUF001
4850
# While we want to treat characters like ( or " as okay for a starting break,
@@ -52,9 +54,6 @@
5254
"(\\b(?:https?|[ts]?ftp|file|git|smb)://[^\\s]+(?=$|\\s)|"
5355
"\\b[\\w.%+-]+@[\\w.-]+\\b)"
5456
)
55-
# Pass all misspellings through this translation table to generate
56-
# alternative misspellings and fixes.
57-
alt_chars = (("'", "’"),) # noqa: RUF001
5857
inline_ignore_regex = re.compile(r"[^\w\s]\s?codespell:ignore\b(\s+(?P<words>[\w,]*))?")
5958
USAGE = """
6059
\t%prog [OPTIONS] [file1 file2 ... fileN]
@@ -167,13 +166,6 @@ def match(self, filename: str) -> bool:
167166
return any(fnmatch.fnmatch(filename, p) for p in self.pattern_list)
168167

169168

170-
class Misspelling:
171-
def __init__(self, data: str, fix: bool, reason: str) -> None:
172-
self.data = data
173-
self.fix = fix
174-
self.reason = reason
175-
176-
177169
class TermColors:
178170
def __init__(self) -> None:
179171
self.FILE = "\033[33m"
@@ -703,48 +695,6 @@ def build_ignore_words(
703695
)
704696

705697

706-
def add_misspelling(
707-
key: str,
708-
data: str,
709-
misspellings: Dict[str, Misspelling],
710-
) -> None:
711-
data = data.strip()
712-
713-
if "," in data:
714-
fix = False
715-
data, reason = data.rsplit(",", 1)
716-
reason = reason.lstrip()
717-
else:
718-
fix = True
719-
reason = ""
720-
721-
misspellings[key] = Misspelling(data, fix, reason)
722-
723-
724-
def build_dict(
725-
filename: str,
726-
misspellings: Dict[str, Misspelling],
727-
ignore_words: Set[str],
728-
) -> None:
729-
with open(filename, encoding="utf-8") as f:
730-
translate_tables = [(x, str.maketrans(x, y)) for x, y in alt_chars]
731-
for line in f:
732-
[key, data] = line.split("->")
733-
# TODO: For now, convert both to lower.
734-
# Someday we can maybe add support for fixing caps.
735-
key = key.lower()
736-
data = data.lower()
737-
if key not in ignore_words:
738-
add_misspelling(key, data, misspellings)
739-
# generate alternative misspellings/fixes
740-
for x, table in translate_tables:
741-
if x in key:
742-
alt_key = key.translate(table)
743-
alt_data = data.translate(table)
744-
if alt_key not in ignore_words:
745-
add_misspelling(alt_key, alt_data, misspellings)
746-
747-
748698
def is_hidden(filename: str, check_hidden: bool) -> bool:
749699
bfilename = os.path.basename(filename)
750700

@@ -759,16 +709,6 @@ def is_text_file(filename: str) -> bool:
759709
return b"\x00" not in s
760710

761711

762-
def fix_case(word: str, fixword: str) -> str:
763-
if word == word.capitalize():
764-
return ", ".join(w.strip().capitalize() for w in fixword.split(","))
765-
if word == word.upper():
766-
return fixword.upper()
767-
# they are both lower case
768-
# or we don't have any idea
769-
return fixword
770-
771-
772712
def ask_for_word_fix(
773713
line: str,
774714
match: Match[str],

codespell_lib/_text_util.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# This program is free software; you can redistribute it and/or modify
3+
# it under the terms of the GNU General Public License as published by
4+
# the Free Software Foundation; version 2 of the License.
5+
#
6+
# This program is distributed in the hope that it will be useful,
7+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
# GNU General Public License for more details.
10+
#
11+
# You should have received a copy of the GNU General Public License
12+
# along with this program; if not, see
13+
# https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
14+
"""
15+
Copyright (C) 2010-2011 Lucas De Marchi <lucas.de.marchi@gmail.com>
16+
Copyright (C) 2011 ProFUSION embedded systems
17+
"""
18+
19+
20+
def fix_case(word: str, fixword: str) -> str:
21+
if word == word.capitalize():
22+
return ", ".join(w.strip().capitalize() for w in fixword.split(","))
23+
if word == word.upper():
24+
return fixword.upper()
25+
# they are both lower case
26+
# or we don't have any idea
27+
return fixword

codespell_lib/spellchecker.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# This program is free software; you can redistribute it and/or modify
3+
# it under the terms of the GNU General Public License as published by
4+
# the Free Software Foundation; version 2 of the License.
5+
#
6+
# This program is distributed in the hope that it will be useful,
7+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
# GNU General Public License for more details.
10+
#
11+
# You should have received a copy of the GNU General Public License
12+
# along with this program; if not, see
13+
# https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
14+
"""
15+
Copyright (C) 2010-2011 Lucas De Marchi <lucas.de.marchi@gmail.com>
16+
Copyright (C) 2011 ProFUSION embedded systems
17+
"""
18+
19+
from typing import (
20+
Dict,
21+
Set,
22+
)
23+
24+
# Pass all misspellings through this translation table to generate
25+
# alternative misspellings and fixes.
26+
alt_chars = (("'", "’"),) # noqa: RUF001
27+
28+
29+
class Misspelling:
30+
def __init__(self, data: str, fix: bool, reason: str) -> None:
31+
self.data = data
32+
self.fix = fix
33+
self.reason = reason
34+
35+
36+
def add_misspelling(
37+
key: str,
38+
data: str,
39+
misspellings: Dict[str, Misspelling],
40+
) -> None:
41+
data = data.strip()
42+
43+
if "," in data:
44+
fix = False
45+
data, reason = data.rsplit(",", 1)
46+
reason = reason.lstrip()
47+
else:
48+
fix = True
49+
reason = ""
50+
51+
misspellings[key] = Misspelling(data, fix, reason)
52+
53+
54+
def build_dict(
55+
filename: str,
56+
misspellings: Dict[str, Misspelling],
57+
ignore_words: Set[str],
58+
) -> None:
59+
with open(filename, encoding="utf-8") as f:
60+
translate_tables = [(x, str.maketrans(x, y)) for x, y in alt_chars]
61+
for line in f:
62+
[key, data] = line.split("->")
63+
# TODO: For now, convert both to lower.
64+
# Someday we can maybe add support for fixing caps.
65+
key = key.lower()
66+
data = data.lower()
67+
if key not in ignore_words:
68+
add_misspelling(key, data, misspellings)
69+
# generate alternative misspellings/fixes
70+
for x, table in translate_tables:
71+
if x in key:
72+
alt_key = key.translate(table)
73+
alt_data = data.translate(table)
74+
if alt_key not in ignore_words:
75+
add_misspelling(alt_key, alt_data, misspellings)

0 commit comments

Comments
 (0)