Debian Codegraph @

src/fenrirscreenreader/commands/onCursorChange/35000-spell_check.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 import os
8 import string
9
10 from fenrirscreenreader.core.i18n import _
11 from fenrirscreenreader.utils import word_utils
12
13 initialized = False
14 try:
15 import enchant
16
17 initialized = True
18 except Exception as e:
19 pass
20
21
22 class command:
23 def __init__(self):
24 self.language = ""
25 self.spellChecker = ""
26
27 def initialize(self, environment):
28 self.env = environment
29 self.update_spell_language()
30
31 def shutdown(self):
32 pass
33
34 def get_description(self):
35 return "No Description found"
36
37 def update_spell_language(self):
38 if not initialized:
39 return
40 self.spellChecker = enchant.Dict(
41 self.env["runtime"]["SettingsManager"].get_setting(
42 "general", "spell_check_language"
43 )
44 )
45 self.language = self.env["runtime"]["SettingsManager"].get_setting(
46 "general", "spell_check_language"
47 )
48
49 def run(self):
50 if not initialized:
51 return
52 if not self.env["runtime"]["SettingsManager"].get_setting_as_bool(
53 "general", "auto_spell_check"
54 ):
55 return
56 if (
57 self.env["runtime"]["SettingsManager"].get_setting(
58 "general", "spell_check_language"
59 )
60 != self.language
61 ):
62 try:
63 self.update_spell_language()
64 except Exception as e:
65 return
66
67 # just when horizontal cursor move worddetection is needed
68 if not self.env["runtime"][
69 "CursorManager"
70 ].is_cursor_horizontal_move():
71 return
72
73 # for now no new line
74 if self.env["runtime"]["CursorManager"].is_cursor_vertical_move():
75 return
76 # more than a keyecho?
77 if len(self.env["screen"]["new_delta"]) > 1:
78 return
79 # deletion
80 if self.env["runtime"]["ScreenManager"].is_negative_delta():
81 return
82 # first place could not be the end of a word
83 if self.env["screen"]["new_cursor"]["x"] == 0:
84 return
85
86 # get the word (just for speedup only look at current line
87 new_content = self.env["screen"]["new_content_text"].split("\n")[
88 self.env["screen"]["new_cursor"]["y"]
89 ]
90 x, y, curr_word, end_of_screen, line_break = (
91 word_utils.get_current_word(
92 self.env["screen"]["new_cursor"]["x"], 0, new_content
93 )
94 )
95 # was this a typed word?
96 if self.env["runtime"]["ScreenManager"].is_delta():
97 if not (
98 new_content[self.env["screen"]["old_cursor"]["x"]]
99 in string.whitespace + '!"#$%&()*+,-./:;<=>?@[\\]^_{|}~'
100 and x != self.env["screen"]["old_cursor"]["x"]
101 ):
102 return
103 else:
104 curr_word = curr_word.strip(
105 string.whitespace + '!"#$%&()*+,-./:;<=>?@[\\]^_{|}~'
106 )
107 else:
108 # or just arrow arround?
109 if not new_content[
110 self.env["screen"]["new_cursor"]["x"]
111 ].isspace():
112 return
113 if (
114 x + len(curr_word) != self.env["screen"]["new_cursor"]["x"]
115 ) and (
116 x + len(curr_word) != self.env["screen"]["new_cursor"]["x"] - 1
117 ):
118 return
119
120 # just on end of word
121 if self.env["screen"]["new_cursor"]["x"] > 0:
122 if (
123 not new_content[
124 self.env["screen"]["old_cursor"]["x"] - 1
125 ].lower()
126 in string.ascii_lowercase
127 ):
128 return
129
130 # ignore bash buildins
131 if curr_word in [
132 "cd",
133 "fg",
134 "bg",
135 "alias",
136 "bind",
137 "dir",
138 "caller",
139 "buildin",
140 "command",
141 "declare",
142 "echo",
143 "enable",
144 "help",
145 "let",
146 "local",
147 "logout",
148 "mapfile",
149 "printf",
150 "read",
151 "readarray",
152 "source",
153 "type",
154 "typeset",
155 "ulimit",
156 "unalias",
157 ]:
158 return
159 # ignore the application name
160 if curr_word.upper() == "FENRIR":
161 return
162 if curr_word[0] == "-":
163 return
164 if curr_word[0] == "/":
165 return
166 if curr_word[0] == "#":
167 return
168 if curr_word.startswith("./"):
169 return
170 if "@" in curr_word and "." in curr_word:
171 return
172 if curr_word[0] == "@":
173 return
174 if curr_word.isnumeric():
175 return
176 if curr_word.isdecimal():
177 return
178 if curr_word.isspace():
179 return
180
181 try:
182 if os.path.exists("/bin/" + curr_word):
183 return
184 except Exception as e:
185 pass
186 try:
187 if os.path.exists("/usr/bin/" + curr_word):
188 return
189 except Exception as e:
190 pass
191 try:
192 if os.path.exists("/sbin/" + curr_word):
193 return
194 except Exception as e:
195 pass
196
197 if not self.spellChecker.check(curr_word):
198 self.env["runtime"]["OutputManager"].present_text(
199 _("misspelled"),
200 sound_icon="mispell",
201 interrupt=False,
202 flush=False,
203 )
204
205 def set_callback(self, callback):
206 pass