43
43
from ._version import ( # type: ignore[import-not-found]
44
44
__version__ as VERSION , # noqa: N812
45
45
)
46
+ from .spellchecker import Misspelling , build_dict
47
+ from ._text_util import fix_case
46
48
47
49
word_regex_def = r"[\w\-'’]+" # noqa: RUF001
48
50
# While we want to treat characters like ( or " as okay for a starting break,
52
54
"(\\ b(?:https?|[ts]?ftp|file|git|smb)://[^\\ s]+(?=$|\\ s)|"
53
55
"\\ b[\\ w.%+-]+@[\\ w.-]+\\ b)"
54
56
)
55
- # Pass all misspellings through this translation table to generate
56
- # alternative misspellings and fixes.
57
- alt_chars = (("'" , "’" ),) # noqa: RUF001
58
57
inline_ignore_regex = re .compile (r"[^\w\s]\s?codespell:ignore\b(\s+(?P<words>[\w,]*))?" )
59
58
USAGE = """
60
59
\t %prog [OPTIONS] [file1 file2 ... fileN]
@@ -167,13 +166,6 @@ def match(self, filename: str) -> bool:
167
166
return any (fnmatch .fnmatch (filename , p ) for p in self .pattern_list )
168
167
169
168
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
-
177
169
class TermColors :
178
170
def __init__ (self ) -> None :
179
171
self .FILE = "\033 [33m"
@@ -703,48 +695,6 @@ def build_ignore_words(
703
695
)
704
696
705
697
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
-
748
698
def is_hidden (filename : str , check_hidden : bool ) -> bool :
749
699
bfilename = os .path .basename (filename )
750
700
@@ -759,16 +709,6 @@ def is_text_file(filename: str) -> bool:
759
709
return b"\x00 " not in s
760
710
761
711
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
-
772
712
def ask_for_word_fix (
773
713
line : str ,
774
714
match : Match [str ],
0 commit comments