-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebModule.py
More file actions
332 lines (281 loc) · 9.54 KB
/
webModule.py
File metadata and controls
332 lines (281 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# globalPlugins/webAccess/webModuleHandler/webModule.py
# -*- coding: utf-8 -*-
# This file is part of Web Access for NVDA.
# Copyright (C) 2015-2024 Accessolutions (http://accessolutions.fr)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See the file COPYING.txt at the root of this distribution for more details.
__version__ = "2024.07.22"
__author__ = (
"Yannick Plassiard <yan@mistigri.org>, "
"Frédéric Brugnot <f.brugnot@accessolutions.fr>, "
"Julien Cochuyt <j.cochuyt@accessolutions.fr>"
)
from collections import OrderedDict
import addonHandler
addonHandler.initTranslation()
import api
import baseObject
import config
import controlTypes
from logHandler import log
import scriptHandler
import speech
import ui
from ..lib.markdown2 import markdown
from ..lib.packaging import version
from ..webAppLib import playWebAppSound
from .. import ruleHandler
class InvalidApiVersion(version.InvalidVersion):
pass
class WebModuleDataLayer(baseObject.AutoPropertyObject):
def __init__(self, name, data, storeRef, rulesOnly=False, readOnly=None):
self.name = name
self.data = data
self.storeRef = storeRef
self.rulesOnly = rulesOnly
if readOnly is not None:
self.readOnly = readOnly
self.dirty = False
def __repr__(self):
return "<WebModuleDataLayer (name={!r}, storeRef={!r}, rulesOnly={!r}".format(
self.name, self.storeRef, self.rulesOnly
)
def _get_readOnly(self):
storeRef = self.storeRef
if storeRef is not None:
if not (isinstance(storeRef, tuple) and len(storeRef) > 1):
log.error("Unhandled storeRef format: {!r}".format(storeRef))
return False
storeName = self.storeRef[0]
if storeName == "userConfig":
if config.conf["webAccess"]["disableUserConfig"]:
return True
elif storeName == "scratchpad":
if not config.conf["webAccess"]["devMode"]:
return True
elif storeName == "addons":
if not (
config.conf["webAccess"]["devMode"]
and config.conf["webAccess"]["writeInAddons"]
):
return True
return False
class WebModule(baseObject.ScriptableObject):
API_VERSION = version.parse("0.4")
FORMAT_VERSION_STR = "0.7-dev"
FORMAT_VERSION = version.parse(FORMAT_VERSION_STR)
def __init__(self):
super(WebModule, self).__init__()
self.layers = [] # List of `WebModuleDataLayer` instances
self.activePageTitle = None
self.activePageIdentifier = None
self.ruleManager = ruleHandler.RuleManager(self)
def __repr__(self):
return "WebModule {name}".format(
name=self.name if self.name is not None else "<noName>"
)
def _get_help(self):
return self._getLayeredProperty("help")
def _set_help(self, value):
self._setLayeredProperty("help", value)
def _get_name(self):
return self._getLayeredProperty("name")
def _set_name(self, value):
self._setLayeredProperty("name", value)
_cache_pageTitle = False
def _get_pageTitle(self):
title = self.activePageTitle
if not title:
try:
title = self.ruleManager.getPageTitle()
except Exception:
log.exception(
'Error while retrieving page title'
' in WebModule "{}"'.format(
self.name
)
)
if not title:
title = api.getForegroundObject().name
return title
def _get_url(self):
return self._getLayeredProperty("url")
def _set_url(self, value):
self._setLayeredProperty("url", value)
def _get_windowTitle(self):
return self._getLayeredProperty("windowTitle")
def _set_windowTitle(self, value):
self._setLayeredProperty("windowTitle", value)
def chooseNVDAObjectOverlayClasses(self, obj, clsList):
"""
Choose NVDAObject overlay classes for a given NVDAObject.
This works in a similar manner as the methods with the same name in
AppModule and GlobalPlugin but comes into play much later: It is
called only when the TreeInterceptor is set on the NVDAObject. Hence,
if removing a class from the list, beware its earlier presence might
have had side effects.
Also, this method should return:
- A sequence of the newly classes for which the method
`initOverlayClass` should be called once the object is mutated.
- `True`, if the object should be mutated but no method should
be called.
- Any negative value, if the object should not be mutated at all.
"""
return False
def createRule(self, data):
return ruleHandler.Rule(self.ruleManager, data)
def dump(self, layerName):
layer = self.getLayer(layerName, raiseIfMissing=True)
data = layer.data
data["formatVersion"] = self.FORMAT_VERSION_STR
data["Rules"] = self.ruleManager.dump(layerName)
return layer
def isReadOnly(self):
try:
return not bool(self._getWritableLayer())
except LookupError:
return True
def load(self, layerName, index=None, data=None, storeRef=None, rulesOnly=False, readOnly=None):
for candidateIndex, layer in enumerate(self.layers):
if layer.name == layerName:
self.unload(layerName)
if index is None:
index = candidateIndex
if data is not None:
from .dataRecovery import recover
recover(data)
layer = WebModuleDataLayer(layerName, data, storeRef, rulesOnly=rulesOnly)
elif storeRef is not None:
from . import store
layer = store.getData(storeRef)
layer.name = layerName
layer.rulesOnly = rulesOnly
data = layer.data
from .dataRecovery import recover
recover(data)
else:
data = OrderedDict({"WebModule": OrderedDict()})
data["WebModule"] = OrderedDict()
data["WebModule"]["name"] = self.name
for attr in ("url", "windowTitle"):
value = getattr(self, attr)
if value:
data["WebModule"][attr] = value
layer = WebModuleDataLayer(layerName, data, storeRef, rulesOnly=rulesOnly)
if index is not None:
self.layers.insert(index, layer)
else:
self.layers.append(layer)
self.ruleManager.load(layer=layer.name, index=index, data=data.get("Rules", {}))
def getLayer(self, layerName, raiseIfMissing=False):
for layer in self.layers:
if layer.name == layerName:
return layer
if raiseIfMissing:
raise LookupError(repr(layerName))
return None
def unload(self, layerName):
for index, layer in enumerate(self.layers):
if layer.name == layerName:
break
else:
raise LookupError(layerName)
self.ruleManager.unload(layerName)
del self.layers[index]
def terminate(self):
self.ruleManager.terminate()
def _getLayeredProperty(self, name, startLayerIndex=-1, raiseIfMissing=False):
for index, layer in list(enumerate(self.layers))[startLayerIndex::-1]:
if layer.rulesOnly:
continue
data = layer.data["WebModule"]
if name not in data:
continue
if index > 0 and name in data.get("overrides", {}):
overridden = self._getLayeredProperty(name, startLayerIndex=index - 1)
if overridden != data["overrides"][name]:
return overridden
return data[name]
if raiseIfMissing:
raise LookupError("name={!r}, startLayerIndex={!r}".format(name, startLayerIndex))
def _getWritableLayer(self):
for layer in reversed(self.layers):
if not layer.readOnly and not layer.rulesOnly:
return layer
break
raise LookupError("No suitable data layer")
def _setLayeredProperty(self, name, value):
layer = self._getWritableLayer()
data = layer.data["WebModule"]
if data.get(name) != value:
layer.dirty = True
data[name] = value
if "overrides" in data:
data["overrides"].pop(name, None)
try:
overridden = self._getLayeredProperty(name, startLayerIndex=-2)
except LookupError:
return
if data["overrides"].get(name) != overridden:
layer.dirty = True
data["overrides"][name] = overridden
def event_webApp_init(self, obj, nextHandler):
self.loadUserFile()
nextHandler()
def event_webApp_pageChanged(self, pageTitle, nextHandler):
speech.cancelSpeech()
playWebAppSound("pageChanged")
speech.speakMessage(pageTitle)
def event_webApp_gainFocus(self, obj, nextHandler):
if obj.role not in [controlTypes.ROLE_DOCUMENT, controlTypes.ROLE_FRAME, controlTypes.ROLE_INTERNALFRAME]:
nextHandler()
def event_focusEntered(self, obj, nextHandler):
if obj.role != controlTypes.ROLE_DOCUMENT:
nextHandler()
def event_gainFocus(self, obj, nextHandler):
nextHandler()
def event_webApp_loseFocus(self, obj, nextHandler):
playWebAppSound("webAppLoseFocus")
nextHandler()
def script_contextualHelp(self, gesture):
if not self.help:
# Translators: Presented when requesting a missing contextual help
ui.message(_("No contextual help available."))
return
ui.browseableMessage(
markdown(self.help),
# Translators: Title of the Contextual Help dialog
_("Contextual Help"),
True
)
def script_title(self, gesture):
title = self.pageTitle
repeatCount = scriptHandler.getLastScriptRepeatCount()
if repeatCount == 0:
ui.message(title)
elif repeatCount == 1:
speech.speakSpelling(title)
else:
if api.copyToClip(title):
ui.message(_("%s copied to clipboard") % title)
def script_sayWebModuleName(self, gesture):
# Translators: Speak name of current web module
ui.message(_("Current web module is: {name}").format(name=self.name))
__gestures = {
"kb:nvda+h": "contextualHelp",
"kb:nvda+t": "title",
"kb:nvda+shift+t": "sayWebModuleName",
}