dsh-plugin-linux-computer-use 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +124 -2
- package/cordis.patch.yml +35 -0
- package/docs/DETAIL.md +118 -0
- package/docs/preview.svg +27 -0
- package/icon.svg +9 -0
- package/index.js +332 -0
- package/linux-computer-use-prep.sh +281 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +61 -4
- package/server/cu_a11y.py +452 -0
- package/server/cu_display.py +523 -0
- package/server/cu_input.py +528 -0
- package/server/cu_sys.py +232 -0
- package/server/cu_unlock.py +280 -0
- package/server/linux_desktop_mcp.py +1362 -0
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-linux-computer-use",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Computer Use for a Wayland Linux desktop in DeepSeek Harness: screenshots, UI tree, pointer and keyboard input, clipboard, processes and settings, bridged as mcp__linux__* tools. · 让 DSH 接管 Ubuntu/GNOME 桌面:截屏、UI 树、鼠标键盘、剪贴板、进程与系统设置。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"dsh",
|
|
10
|
+
"dsh-plugin",
|
|
11
|
+
"computer-use",
|
|
12
|
+
"desktop-automation",
|
|
13
|
+
"mcp",
|
|
14
|
+
"wayland",
|
|
15
|
+
"gnome",
|
|
16
|
+
"at-spi",
|
|
17
|
+
"uinput",
|
|
18
|
+
"ubuntu",
|
|
19
|
+
"ubuntu-24-04",
|
|
20
|
+
"linux-desktop",
|
|
21
|
+
"screenshot",
|
|
22
|
+
"accessibility"
|
|
23
|
+
],
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./index.js",
|
|
26
|
+
"./locale/*.json": "./locale/*.json",
|
|
27
|
+
"./icon": "./icon.svg"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"index.js",
|
|
31
|
+
"cordis.patch.yml",
|
|
32
|
+
"locale/*.json",
|
|
33
|
+
"docs",
|
|
34
|
+
"icon.svg",
|
|
35
|
+
"server/*.py",
|
|
36
|
+
"linux-computer-use-prep.sh",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE",
|
|
39
|
+
"CHANGELOG.md"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"selftest": "python3 -u server/linux_desktop_mcp.py --selftest",
|
|
43
|
+
"status": "python3 -u server/linux_desktop_mcp.py --status",
|
|
44
|
+
"check": "node --check index.js && python3 -m py_compile server/*.py && npm pack --dry-run"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=20"
|
|
48
|
+
},
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/1804608397/dsh-plugin-linux-computer-use.git"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/1804608397/dsh-plugin-linux-computer-use#readme",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/1804608397/dsh-plugin-linux-computer-use/issues"
|
|
56
|
+
},
|
|
57
|
+
"author": "Beining Cheng <1804608397@users.noreply.github.com>",
|
|
58
|
+
"dsh": {
|
|
59
|
+
"bundle": {
|
|
60
|
+
"patch": "./cordis.patch.yml"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
"""AT-SPI2 accessibility-tree access for the Linux desktop MCP server.
|
|
2
|
+
|
|
3
|
+
Every X11 trick for "look at the UI" is unavailable on Wayland, so this module
|
|
4
|
+
reads the accessibility tree that GNOME exposes over ``org.a11y.Bus``. It gives
|
|
5
|
+
the model the same three things the Windows build got from UIA:
|
|
6
|
+
|
|
7
|
+
* a structural snapshot of the running applications and their widgets;
|
|
8
|
+
* stable element references the model can act on (``do_action``, ``focus``);
|
|
9
|
+
* text read/write and screen rectangles, so a click can target a widget
|
|
10
|
+
instead of a guessed pixel.
|
|
11
|
+
|
|
12
|
+
Tree walks are bounded by both a node cap and a wall-clock deadline: a single
|
|
13
|
+
unresponsive application must not hang the whole server.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import time
|
|
19
|
+
|
|
20
|
+
import gi
|
|
21
|
+
|
|
22
|
+
gi.require_version("Atspi", "2.0")
|
|
23
|
+
from gi.repository import Atspi # noqa: E402
|
|
24
|
+
|
|
25
|
+
STATE_FLAGS = (
|
|
26
|
+
"ENABLED", "SENSITIVE", "SHOWING", "VISIBLE", "FOCUSED", "FOCUSABLE",
|
|
27
|
+
"CHECKED", "EDITABLE", "SELECTED", "EXPANDED", "PRESSED",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class A11yError(RuntimeError):
|
|
32
|
+
"""The accessibility tree could not be read or the element is gone."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _state_names(acc) -> list[str]:
|
|
36
|
+
try:
|
|
37
|
+
state_set = acc.get_state_set()
|
|
38
|
+
except Exception: # noqa: BLE001 - dead proxies raise many GLib errors
|
|
39
|
+
return []
|
|
40
|
+
found = []
|
|
41
|
+
for flag in STATE_FLAGS:
|
|
42
|
+
value = getattr(Atspi.StateType, flag, None)
|
|
43
|
+
if value is None:
|
|
44
|
+
continue
|
|
45
|
+
try:
|
|
46
|
+
if state_set.contains(value):
|
|
47
|
+
found.append(flag.lower())
|
|
48
|
+
except Exception: # noqa: BLE001
|
|
49
|
+
break
|
|
50
|
+
return found
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _extents(acc) -> dict | None:
|
|
54
|
+
try:
|
|
55
|
+
iface = acc.get_component_iface()
|
|
56
|
+
except Exception: # noqa: BLE001
|
|
57
|
+
return None
|
|
58
|
+
if iface is None:
|
|
59
|
+
return None
|
|
60
|
+
try:
|
|
61
|
+
x, y, width, height = iface.get_extents(Atspi.CoordType.SCREEN)
|
|
62
|
+
except Exception: # noqa: BLE001
|
|
63
|
+
return None
|
|
64
|
+
if width <= 0 or height <= 0:
|
|
65
|
+
return None
|
|
66
|
+
return {"x": int(x), "y": int(y), "width": int(width), "height": int(height)}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _actions(acc) -> list[str]:
|
|
70
|
+
try:
|
|
71
|
+
iface = acc.get_action_iface()
|
|
72
|
+
except Exception: # noqa: BLE001
|
|
73
|
+
return []
|
|
74
|
+
if iface is None:
|
|
75
|
+
return []
|
|
76
|
+
names = []
|
|
77
|
+
try:
|
|
78
|
+
count = iface.get_n_actions()
|
|
79
|
+
except Exception: # noqa: BLE001
|
|
80
|
+
return []
|
|
81
|
+
for index in range(min(count, 8)):
|
|
82
|
+
try:
|
|
83
|
+
names.append(iface.get_action_name(index) or f"action{index}")
|
|
84
|
+
except Exception: # noqa: BLE001
|
|
85
|
+
break
|
|
86
|
+
return names
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _text_preview(acc, limit: int = 160) -> str:
|
|
90
|
+
try:
|
|
91
|
+
iface = acc.get_text_iface()
|
|
92
|
+
except Exception: # noqa: BLE001
|
|
93
|
+
return ""
|
|
94
|
+
if iface is None:
|
|
95
|
+
return ""
|
|
96
|
+
try:
|
|
97
|
+
count = iface.get_character_count()
|
|
98
|
+
if not count:
|
|
99
|
+
return ""
|
|
100
|
+
text = iface.get_text(0, min(int(count), limit))
|
|
101
|
+
except Exception: # noqa: BLE001
|
|
102
|
+
return ""
|
|
103
|
+
return (text or "").replace("\n", " ").strip()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class A11y:
|
|
107
|
+
"""Thin, defensive wrapper around the AT-SPI2 tree."""
|
|
108
|
+
|
|
109
|
+
def __init__(self) -> None:
|
|
110
|
+
self._started = False
|
|
111
|
+
self._registry: dict[str, object] = {}
|
|
112
|
+
self._by_id: dict[int, str] = {}
|
|
113
|
+
self._counter = 0
|
|
114
|
+
self._apps: dict[str, object] = {}
|
|
115
|
+
self._apps_cache: list[dict] | None = None
|
|
116
|
+
self._apps_at = 0.0
|
|
117
|
+
self.apps_ttl = 5.0
|
|
118
|
+
|
|
119
|
+
# -- lifecycle --------------------------------------------------------- #
|
|
120
|
+
def start(self) -> None:
|
|
121
|
+
if self._started:
|
|
122
|
+
return
|
|
123
|
+
try:
|
|
124
|
+
# Tighten the D-Bus timeouts used for tree queries: one hung
|
|
125
|
+
# application must not stall a whole snapshot. 1s is far above the
|
|
126
|
+
# reply time of any healthy local toolkit.
|
|
127
|
+
Atspi.set_timeout(1000, 3000)
|
|
128
|
+
except Exception: # noqa: BLE001 - older libatspi may not export it
|
|
129
|
+
pass
|
|
130
|
+
try:
|
|
131
|
+
Atspi.init()
|
|
132
|
+
except Exception as error: # noqa: BLE001
|
|
133
|
+
raise A11yError(f"cannot initialise AT-SPI: {error}") from error
|
|
134
|
+
try:
|
|
135
|
+
desktop = Atspi.get_desktop(0)
|
|
136
|
+
except Exception as error: # noqa: BLE001
|
|
137
|
+
raise A11yError(
|
|
138
|
+
"AT-SPI is not reachable; is at-spi2-core running and "
|
|
139
|
+
"org.a11y.Bus available on the session bus? "
|
|
140
|
+
f"({error})"
|
|
141
|
+
) from error
|
|
142
|
+
if desktop is None:
|
|
143
|
+
raise A11yError("AT-SPI returned no desktop; accessibility is unavailable")
|
|
144
|
+
self._started = True
|
|
145
|
+
|
|
146
|
+
def _desktop(self):
|
|
147
|
+
self.start()
|
|
148
|
+
return Atspi.get_desktop(0)
|
|
149
|
+
|
|
150
|
+
# -- references -------------------------------------------------------- #
|
|
151
|
+
def _ref(self, acc) -> str:
|
|
152
|
+
key = id(acc)
|
|
153
|
+
known = self._by_id.get(key)
|
|
154
|
+
if known is not None and self._registry.get(known) is acc:
|
|
155
|
+
return known
|
|
156
|
+
self._counter += 1
|
|
157
|
+
ref = f"e{self._counter}"
|
|
158
|
+
self._registry[ref] = acc
|
|
159
|
+
self._by_id[key] = ref
|
|
160
|
+
if len(self._registry) > 4000:
|
|
161
|
+
# Keep memory bounded: forget the oldest quarter of the refs.
|
|
162
|
+
for stale in list(self._registry)[:1000]:
|
|
163
|
+
self._registry.pop(stale, None)
|
|
164
|
+
self._by_id = {id(obj): name for name, obj in self._registry.items()}
|
|
165
|
+
return ref
|
|
166
|
+
|
|
167
|
+
def resolve(self, ref: str):
|
|
168
|
+
self.start()
|
|
169
|
+
acc = self._registry.get(ref)
|
|
170
|
+
if acc is None:
|
|
171
|
+
raise A11yError(f"unknown element reference {ref!r}; take a fresh snapshot")
|
|
172
|
+
return acc
|
|
173
|
+
|
|
174
|
+
# -- applications ------------------------------------------------------ #
|
|
175
|
+
def apps(self, max_age: float | None = None) -> list[dict]:
|
|
176
|
+
ttl = self.apps_ttl if max_age is None else max_age
|
|
177
|
+
if self._apps_cache is not None and (time.monotonic() - self._apps_at) < ttl:
|
|
178
|
+
return [dict(item) for item in self._apps_cache]
|
|
179
|
+
desktop = self._desktop()
|
|
180
|
+
try:
|
|
181
|
+
count = desktop.get_child_count()
|
|
182
|
+
except Exception as error: # noqa: BLE001
|
|
183
|
+
raise A11yError(f"cannot enumerate applications: {error}") from error
|
|
184
|
+
found = []
|
|
185
|
+
self._apps.clear()
|
|
186
|
+
for index in range(count):
|
|
187
|
+
try:
|
|
188
|
+
app = desktop.get_child_at_index(index)
|
|
189
|
+
except Exception: # noqa: BLE001
|
|
190
|
+
continue
|
|
191
|
+
if app is None:
|
|
192
|
+
continue
|
|
193
|
+
try:
|
|
194
|
+
name = app.get_name() or ""
|
|
195
|
+
role = app.get_role_name() or ""
|
|
196
|
+
pid = app.get_process_id() or 0
|
|
197
|
+
except Exception: # noqa: BLE001
|
|
198
|
+
continue
|
|
199
|
+
ref = self._ref(app)
|
|
200
|
+
self._apps[name] = app
|
|
201
|
+
found.append({"ref": ref, "name": name, "role": role, "pid": int(pid)})
|
|
202
|
+
found.sort(key=lambda item: item["name"].lower())
|
|
203
|
+
self._apps_cache = found
|
|
204
|
+
self._apps_at = time.monotonic()
|
|
205
|
+
return [dict(item) for item in found]
|
|
206
|
+
|
|
207
|
+
def _app(self, name: str | None):
|
|
208
|
+
if not name:
|
|
209
|
+
return None
|
|
210
|
+
if not self._apps:
|
|
211
|
+
self.apps()
|
|
212
|
+
exact = self._apps.get(name)
|
|
213
|
+
if exact is not None:
|
|
214
|
+
return exact
|
|
215
|
+
lowered = name.lower()
|
|
216
|
+
for app_name, app in self._apps.items():
|
|
217
|
+
if lowered in app_name.lower():
|
|
218
|
+
return app
|
|
219
|
+
return None
|
|
220
|
+
|
|
221
|
+
# -- snapshot ---------------------------------------------------------- #
|
|
222
|
+
def node(self, acc, depth: int, budget: dict, deadline: float) -> dict | None:
|
|
223
|
+
if budget["nodes"] <= 0 or time.monotonic() > deadline:
|
|
224
|
+
budget["truncated"] = True
|
|
225
|
+
return None
|
|
226
|
+
budget["nodes"] -= 1
|
|
227
|
+
try:
|
|
228
|
+
role = acc.get_role_name() or ""
|
|
229
|
+
name = acc.get_name() or ""
|
|
230
|
+
except Exception: # noqa: BLE001
|
|
231
|
+
return None
|
|
232
|
+
item: dict = {"ref": self._ref(acc), "role": role}
|
|
233
|
+
if name:
|
|
234
|
+
item["name"] = name[:120]
|
|
235
|
+
states = _state_names(acc)
|
|
236
|
+
if states:
|
|
237
|
+
item["states"] = states
|
|
238
|
+
rect = _extents(acc)
|
|
239
|
+
if rect:
|
|
240
|
+
item["rect"] = rect
|
|
241
|
+
actions = _actions(acc)
|
|
242
|
+
if actions:
|
|
243
|
+
item["actions"] = actions
|
|
244
|
+
text = _text_preview(acc)
|
|
245
|
+
if text:
|
|
246
|
+
item["text"] = text
|
|
247
|
+
if depth > 0:
|
|
248
|
+
children = []
|
|
249
|
+
try:
|
|
250
|
+
count = acc.get_child_count()
|
|
251
|
+
except Exception: # noqa: BLE001
|
|
252
|
+
count = 0
|
|
253
|
+
for index in range(count):
|
|
254
|
+
child = self.node(acc.get_child_at_index(index), depth - 1, budget, deadline)
|
|
255
|
+
if child is None:
|
|
256
|
+
if budget["truncated"]:
|
|
257
|
+
break
|
|
258
|
+
continue
|
|
259
|
+
children.append(child)
|
|
260
|
+
if children:
|
|
261
|
+
item["children"] = children
|
|
262
|
+
return item
|
|
263
|
+
|
|
264
|
+
def snapshot(self, app: str | None = None, max_depth: int = 3,
|
|
265
|
+
max_nodes: int = 220, timeout_s: float = 20.0) -> dict:
|
|
266
|
+
self.start()
|
|
267
|
+
deadline = time.monotonic() + timeout_s
|
|
268
|
+
budget = {"nodes": max_nodes, "truncated": False}
|
|
269
|
+
if app:
|
|
270
|
+
target = self._app(app)
|
|
271
|
+
if target is None:
|
|
272
|
+
available = [item["name"] for item in self.apps()]
|
|
273
|
+
raise A11yError(f"no application matches {app!r}; running: {available}")
|
|
274
|
+
roots = [(target, app)]
|
|
275
|
+
else:
|
|
276
|
+
roots = []
|
|
277
|
+
for item in self.apps()[:12]:
|
|
278
|
+
target = self._apps.get(item["name"])
|
|
279
|
+
if target is not None:
|
|
280
|
+
roots.append((target, item["name"]))
|
|
281
|
+
tree = []
|
|
282
|
+
for root, label in roots:
|
|
283
|
+
node = self.node(root, max_depth, budget, deadline)
|
|
284
|
+
if node is not None:
|
|
285
|
+
node.setdefault("name", label)
|
|
286
|
+
node["app"] = label
|
|
287
|
+
tree.append(node)
|
|
288
|
+
return {
|
|
289
|
+
"applications": tree,
|
|
290
|
+
"truncated": bool(budget["truncated"]),
|
|
291
|
+
"nodes_used": max_nodes - max(0, budget["nodes"]),
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
# -- search ------------------------------------------------------------ #
|
|
295
|
+
def find(self, query: str | None = None, role: str | None = None,
|
|
296
|
+
app: str | None = None, limit: int = 20, timeout_s: float = 15.0) -> list[dict]:
|
|
297
|
+
self.start()
|
|
298
|
+
deadline = time.monotonic() + timeout_s
|
|
299
|
+
needle = (query or "").lower()
|
|
300
|
+
role_needle = (role or "").lower()
|
|
301
|
+
matches: list[dict] = []
|
|
302
|
+
queue: list[tuple] = []
|
|
303
|
+
if app:
|
|
304
|
+
target = self._app(app)
|
|
305
|
+
if target is None:
|
|
306
|
+
raise A11yError(f"no application matches {app!r}")
|
|
307
|
+
queue.append(target)
|
|
308
|
+
else:
|
|
309
|
+
for item in self.apps()[:20]:
|
|
310
|
+
target = self._apps.get(item["name"])
|
|
311
|
+
if target is not None:
|
|
312
|
+
queue.append(target)
|
|
313
|
+
visited = 0
|
|
314
|
+
while queue and len(matches) < limit and time.monotonic() < deadline and visited < 6000:
|
|
315
|
+
acc = queue.pop(0)
|
|
316
|
+
visited += 1
|
|
317
|
+
try:
|
|
318
|
+
name = acc.get_name() or ""
|
|
319
|
+
role_name = acc.get_role_name() or ""
|
|
320
|
+
except Exception: # noqa: BLE001
|
|
321
|
+
continue
|
|
322
|
+
haystack = f"{name}\n{_text_preview(acc, 200)}".lower()
|
|
323
|
+
hit = (not needle or needle in haystack) and (not role_needle or role_needle in role_name.lower())
|
|
324
|
+
if hit and (needle or role_needle):
|
|
325
|
+
matches.append(self.describe_object(acc, name, role_name))
|
|
326
|
+
try:
|
|
327
|
+
count = acc.get_child_count()
|
|
328
|
+
except Exception: # noqa: BLE001
|
|
329
|
+
count = 0
|
|
330
|
+
for index in range(count):
|
|
331
|
+
try:
|
|
332
|
+
queue.append(acc.get_child_at_index(index))
|
|
333
|
+
except Exception: # noqa: BLE001
|
|
334
|
+
continue
|
|
335
|
+
return matches
|
|
336
|
+
|
|
337
|
+
def describe_object(self, acc, name: str | None = None, role_name: str | None = None) -> dict:
|
|
338
|
+
try:
|
|
339
|
+
name = name if name is not None else (acc.get_name() or "")
|
|
340
|
+
role_name = role_name if role_name is not None else (acc.get_role_name() or "")
|
|
341
|
+
except Exception as error: # noqa: BLE001
|
|
342
|
+
raise A11yError(f"element is no longer available: {error}") from error
|
|
343
|
+
info: dict = {"ref": self._ref(acc), "role": role_name}
|
|
344
|
+
if name:
|
|
345
|
+
info["name"] = name[:160]
|
|
346
|
+
states = _state_names(acc)
|
|
347
|
+
if states:
|
|
348
|
+
info["states"] = states
|
|
349
|
+
rect = _extents(acc)
|
|
350
|
+
if rect:
|
|
351
|
+
info["rect"] = rect
|
|
352
|
+
info["center"] = [rect["x"] + rect["width"] // 2, rect["y"] + rect["height"] // 2]
|
|
353
|
+
actions = _actions(acc)
|
|
354
|
+
if actions:
|
|
355
|
+
info["actions"] = actions
|
|
356
|
+
text = _text_preview(acc, 400)
|
|
357
|
+
if text:
|
|
358
|
+
info["text"] = text
|
|
359
|
+
try:
|
|
360
|
+
description = acc.get_description() or ""
|
|
361
|
+
except Exception: # noqa: BLE001
|
|
362
|
+
description = ""
|
|
363
|
+
if description:
|
|
364
|
+
info["description"] = description[:200]
|
|
365
|
+
return info
|
|
366
|
+
|
|
367
|
+
def describe(self, ref: str) -> dict:
|
|
368
|
+
return self.describe_object(self.resolve(ref))
|
|
369
|
+
|
|
370
|
+
# -- interaction ------------------------------------------------------- #
|
|
371
|
+
def action(self, ref: str, action: str | int | None = None) -> dict:
|
|
372
|
+
acc = self.resolve(ref)
|
|
373
|
+
try:
|
|
374
|
+
iface = acc.get_action_iface()
|
|
375
|
+
except Exception as error: # noqa: BLE001
|
|
376
|
+
raise A11yError(f"element {ref} exposes no actions: {error}") from error
|
|
377
|
+
if iface is None:
|
|
378
|
+
raise A11yError(f"element {ref} exposes no actions")
|
|
379
|
+
index: int | None = None
|
|
380
|
+
names: list[str] = []
|
|
381
|
+
try:
|
|
382
|
+
count = iface.get_n_actions()
|
|
383
|
+
names = [(iface.get_action_name(i) or "") for i in range(count)]
|
|
384
|
+
except Exception: # noqa: BLE001
|
|
385
|
+
count = 0
|
|
386
|
+
if isinstance(action, int):
|
|
387
|
+
index = action
|
|
388
|
+
elif isinstance(action, str) and action:
|
|
389
|
+
lowered = action.lower()
|
|
390
|
+
for position, name in enumerate(names):
|
|
391
|
+
if name.lower() == lowered:
|
|
392
|
+
index = position
|
|
393
|
+
break
|
|
394
|
+
if index is None:
|
|
395
|
+
for position, name in enumerate(names):
|
|
396
|
+
if lowered in name.lower():
|
|
397
|
+
index = position
|
|
398
|
+
break
|
|
399
|
+
else:
|
|
400
|
+
index = 0 if count else None
|
|
401
|
+
if index is None or (count and index >= count):
|
|
402
|
+
raise A11yError(f"element {ref} has no matching action; available: {names}")
|
|
403
|
+
try:
|
|
404
|
+
done = bool(iface.do_action(index))
|
|
405
|
+
except Exception as error: # noqa: BLE001
|
|
406
|
+
raise A11yError(f"action failed on {ref}: {error}") from error
|
|
407
|
+
return {"ref": ref, "action": names[index] if index < len(names) else index, "performed": done}
|
|
408
|
+
|
|
409
|
+
def focus(self, ref: str) -> dict:
|
|
410
|
+
acc = self.resolve(ref)
|
|
411
|
+
try:
|
|
412
|
+
iface = acc.get_component_iface()
|
|
413
|
+
except Exception as error: # noqa: BLE001
|
|
414
|
+
raise A11yError(f"element {ref} has no component interface: {error}") from error
|
|
415
|
+
if iface is None:
|
|
416
|
+
raise A11yError(f"element {ref} has no component interface")
|
|
417
|
+
try:
|
|
418
|
+
done = bool(iface.grab_focus())
|
|
419
|
+
except Exception as error: # noqa: BLE001
|
|
420
|
+
raise A11yError(f"cannot focus {ref}: {error}") from error
|
|
421
|
+
return {"ref": ref, "focused": done}
|
|
422
|
+
|
|
423
|
+
def text(self, ref: str) -> dict:
|
|
424
|
+
acc = self.resolve(ref)
|
|
425
|
+
try:
|
|
426
|
+
iface = acc.get_text_iface()
|
|
427
|
+
except Exception as error: # noqa: BLE001
|
|
428
|
+
raise A11yError(f"element {ref} has no text interface: {error}") from error
|
|
429
|
+
if iface is None:
|
|
430
|
+
raise A11yError(f"element {ref} has no text interface")
|
|
431
|
+
try:
|
|
432
|
+
count = int(iface.get_character_count())
|
|
433
|
+
value = iface.get_text(0, count) if count else ""
|
|
434
|
+
except Exception as error: # noqa: BLE001
|
|
435
|
+
raise A11yError(f"cannot read text of {ref}: {error}") from error
|
|
436
|
+
return {"ref": ref, "text": value or "", "length": count}
|
|
437
|
+
|
|
438
|
+
def set_text(self, ref: str, value: str) -> dict:
|
|
439
|
+
acc = self.resolve(ref)
|
|
440
|
+
try:
|
|
441
|
+
iface = acc.get_editable_text_iface()
|
|
442
|
+
except Exception: # noqa: BLE001
|
|
443
|
+
iface = None
|
|
444
|
+
if iface is None:
|
|
445
|
+
raise A11yError(
|
|
446
|
+
f"element {ref} is not directly editable; focus it and type instead"
|
|
447
|
+
)
|
|
448
|
+
try:
|
|
449
|
+
ok = bool(iface.set_text_contents(value))
|
|
450
|
+
except Exception as error: # noqa: BLE001
|
|
451
|
+
raise A11yError(f"cannot set text of {ref}: {error}") from error
|
|
452
|
+
return {"ref": ref, "set": ok, "length": len(value)}
|