|
29 | 29 | from logHandler import log # noqa: E402 |
30 | 30 | import braille # noqa: E402 |
31 | 31 | import gui.contextHelp # noqa: E402 |
| 32 | +import textInfos # noqa: E402 |
32 | 33 |
|
33 | 34 |
|
34 | 35 | class HelpCommand(object): |
@@ -223,7 +224,6 @@ def initNamespace(self): |
223 | 224 | import config |
224 | 225 | import controlTypes |
225 | 226 | import globalPlugins |
226 | | - import textInfos |
227 | 227 | import vision |
228 | 228 |
|
229 | 229 | self.namespace.clear() |
@@ -255,25 +255,35 @@ def updateNamespaceSnapshotVars(self): |
255 | 255 | Typically, used before the NVDA python console is opened, after which, calls |
256 | 256 | to the 'api' module will refer to this new focus. |
257 | 257 | """ |
258 | | - try: |
259 | | - caretPos = api.getCaretPosition() |
260 | | - except RuntimeError: |
261 | | - log.debug("Unable to set caretPos snapshot variable for python console.") |
262 | | - caretPos = None |
263 | 258 |
|
264 | | - self._namespaceSnapshotVars = { |
265 | | - "focus": api.getFocusObject(), |
| 259 | + def _getCaretPos() -> textInfos.TextInfo | None: |
| 260 | + try: |
| 261 | + return api.getCaretPosition() |
| 262 | + except RuntimeError: |
| 263 | + log.debug("Unable to set caretPos snapshot variable for python console.") |
| 264 | + return None |
| 265 | + |
| 266 | + self._namespaceSnapshotVarsGetters = { |
| 267 | + "focus": api.getFocusObject), |
266 | 268 | # Copy the focus ancestor list, as it gets mutated once it is replaced in api.setFocusObject. |
267 | | - "focusAnc": list(api.getFocusAncestors()), |
268 | | - "fdl": api.getFocusDifferenceLevel(), |
269 | | - "fg": api.getForegroundObject(), |
270 | | - "nav": api.getNavigatorObject(), |
271 | | - "caretObj": api.getCaretObject(), |
272 | | - "caretPos": caretPos, |
273 | | - "review": api.getReviewPosition(), |
274 | | - "mouse": api.getMouseObject(), |
275 | | - "brlRegions": braille.handler.buffer.regions, |
| 269 | + "focusAnc": (lambda: list(api.getFocusAncestors())), |
| 270 | + "fdl": (lambda: api.getFocusDifferenceLevel()), |
| 271 | + "fg": (lambda: api.getForegroundObject()), |
| 272 | + "nav": (lambda: api.getNavigatorObject()), |
| 273 | + "caretObj": (lambda: api.getCaretObject()), |
| 274 | + "caretPos": _getCaretPos, |
| 275 | + "review": (lambda: api.getReviewPosition()), |
| 276 | + "mouse": (lambda: api.getMouseObject()), |
| 277 | + "brlRegions": (lambda: braille.handler.buffer.regions), |
276 | 278 | } |
| 279 | + self._namespaceSnapshotVars = {} |
| 280 | + for name, getter in self._namespaceSnapshotVarsGetters.items(): |
| 281 | + try: |
| 282 | + value = getter() |
| 283 | + except Exception: |
| 284 | + log.error(f"Unable to set {name} snapshot variable for python console.", exc_info=True) |
| 285 | + value = None |
| 286 | + self._namespaceSnapshotVars[name] = value |
277 | 287 | self.namespace.update(self._namespaceSnapshotVars) |
278 | 288 |
|
279 | 289 | def removeNamespaceSnapshotVars(self): |
|
0 commit comments