networka 0.1.9__py3-none-any.whl
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.
- network_toolkit/__about__.py +4 -0
- network_toolkit/__init__.py +8 -0
- network_toolkit/__main__.py +10 -0
- network_toolkit/builtin_sequences/arista_eos/common.yml +53 -0
- network_toolkit/builtin_sequences/mikrotik_routeros/common.yml +52 -0
- network_toolkit/cli.py +265 -0
- network_toolkit/commands/__init__.py +8 -0
- network_toolkit/commands/backup.py +441 -0
- network_toolkit/commands/complete.py +172 -0
- network_toolkit/commands/config.py +1068 -0
- network_toolkit/commands/diff.py +485 -0
- network_toolkit/commands/download.py +197 -0
- network_toolkit/commands/firmware.py +780 -0
- network_toolkit/commands/info.py +377 -0
- network_toolkit/commands/list.py +345 -0
- network_toolkit/commands/routerboard_upgrade.py +204 -0
- network_toolkit/commands/run.py +839 -0
- network_toolkit/commands/schema_unified.py +91 -0
- network_toolkit/commands/sequence.py +17 -0
- network_toolkit/commands/ssh.py +540 -0
- network_toolkit/commands/ssh_engines.py +0 -0
- network_toolkit/commands/ssh_fallback.py +97 -0
- network_toolkit/commands/ssh_platform.py +120 -0
- network_toolkit/commands/upload.py +237 -0
- network_toolkit/commands/vendor_config_backup.py +378 -0
- network_toolkit/common/color_magic.py +177 -0
- network_toolkit/common/command.py +135 -0
- network_toolkit/common/command_base.py +135 -0
- network_toolkit/common/command_helpers.py +214 -0
- network_toolkit/common/credentials.py +86 -0
- network_toolkit/common/defaults.py +17 -0
- network_toolkit/common/errors.py +130 -0
- network_toolkit/common/interactive_confirmation.py +144 -0
- network_toolkit/common/logging.py +47 -0
- network_toolkit/common/output.py +594 -0
- network_toolkit/common/output_clean.py +244 -0
- network_toolkit/common/paths.py +86 -0
- network_toolkit/common/resolver.py +207 -0
- network_toolkit/common/styles.py +199 -0
- network_toolkit/common/table_generator.py +169 -0
- network_toolkit/common/table_providers.py +716 -0
- network_toolkit/config.py +1506 -0
- network_toolkit/credentials.py +288 -0
- network_toolkit/device.py +1658 -0
- network_toolkit/device_transfers.py +170 -0
- network_toolkit/exceptions.py +78 -0
- network_toolkit/ip_device.py +241 -0
- network_toolkit/platforms/__init__.py +21 -0
- network_toolkit/platforms/base.py +267 -0
- network_toolkit/platforms/cisco_ios/__init__.py +6 -0
- network_toolkit/platforms/cisco_ios/constants.py +15 -0
- network_toolkit/platforms/cisco_ios/operations.py +426 -0
- network_toolkit/platforms/cisco_iosxe/__init__.py +6 -0
- network_toolkit/platforms/cisco_iosxe/constants.py +15 -0
- network_toolkit/platforms/cisco_iosxe/operations.py +580 -0
- network_toolkit/platforms/cisco_shared/__init__.py +0 -0
- network_toolkit/platforms/cisco_shared/operations.py +0 -0
- network_toolkit/platforms/factory.py +190 -0
- network_toolkit/platforms/mikrotik_routeros/__init__.py +8 -0
- network_toolkit/platforms/mikrotik_routeros/confirmation_patterns.py +24 -0
- network_toolkit/platforms/mikrotik_routeros/constants.py +31 -0
- network_toolkit/platforms/mikrotik_routeros/operations.py +686 -0
- network_toolkit/results_enhanced.py +364 -0
- network_toolkit/schemas/__init__.py +8 -0
- network_toolkit/sequence_manager.py +239 -0
- network_toolkit/shell_completion/bash_completion_nw.sh +339 -0
- network_toolkit/shell_completion/zsh_completion_nw.zsh +136 -0
- network_toolkit/transport/__init__.py +15 -0
- network_toolkit/transport/factory.py +113 -0
- network_toolkit/transport/interfaces.py +46 -0
- network_toolkit/transport/nornir_inventory.py +173 -0
- network_toolkit/transport/nornir_netmiko.py +277 -0
- network_toolkit/transport/scrapli_sync.py +36 -0
- network_toolkit/tui/__init__.py +18 -0
- network_toolkit/tui/app.py +1275 -0
- network_toolkit/tui/clipboard.py +46 -0
- network_toolkit/tui/compat.py +111 -0
- network_toolkit/tui/constants.py +111 -0
- network_toolkit/tui/controller.py +550 -0
- network_toolkit/tui/data.py +149 -0
- network_toolkit/tui/dialogs.py +196 -0
- network_toolkit/tui/helpers.py +55 -0
- network_toolkit/tui/keymap.py +31 -0
- network_toolkit/tui/layout.py +111 -0
- network_toolkit/tui/models.py +93 -0
- network_toolkit/tui/output_manager.py +372 -0
- network_toolkit/tui/prompt.py +172 -0
- network_toolkit/tui/services.py +285 -0
- networka-0.1.9.dist-info/METADATA +316 -0
- networka-0.1.9.dist-info/RECORD +93 -0
- networka-0.1.9.dist-info/WHEEL +4 -0
- networka-0.1.9.dist-info/entry_points.txt +2 -0
- networka-0.1.9.dist-info/licenses/LICENSE +73 -0
|
@@ -0,0 +1,1275 @@
|
|
|
1
|
+
"""Textual app for selecting targets and actions.
|
|
2
|
+
|
|
3
|
+
First iteration focuses on selection only:
|
|
4
|
+
|
|
5
|
+
Execution is not wired yet; we only collect selections and show a preview.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import asyncio
|
|
11
|
+
import logging
|
|
12
|
+
import threading
|
|
13
|
+
import time
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Any, ClassVar
|
|
16
|
+
|
|
17
|
+
from network_toolkit.tui.clipboard import copy_to_clipboard
|
|
18
|
+
from network_toolkit.tui.compat import TextualNotInstalledError, load_textual
|
|
19
|
+
from network_toolkit.tui.constants import CSS as APP_CSS
|
|
20
|
+
from network_toolkit.tui.controller import TuiController
|
|
21
|
+
from network_toolkit.tui.data import TuiData
|
|
22
|
+
from network_toolkit.tui.keymap import KEYMAP
|
|
23
|
+
from network_toolkit.tui.layout import compose_root
|
|
24
|
+
from network_toolkit.tui.models import RunCallbacks, SelectionState
|
|
25
|
+
from network_toolkit.tui.output_manager import OutputPanelManager
|
|
26
|
+
from network_toolkit.tui.services import ExecutionService
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _new_rich_text() -> Any: # pragma: no cover - optional rich dependency helper
|
|
30
|
+
try:
|
|
31
|
+
from rich.text import Text
|
|
32
|
+
|
|
33
|
+
return Text()
|
|
34
|
+
except Exception:
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
# models.SelectionState is used instead of local dataclass
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def run(config: str | Path = "config") -> None:
|
|
41
|
+
"""Entry to run the TUI app.
|
|
42
|
+
|
|
43
|
+
Imports Textual at runtime to keep it an optional dependency.
|
|
44
|
+
"""
|
|
45
|
+
try:
|
|
46
|
+
compat = load_textual()
|
|
47
|
+
app_cls: Any = compat.App
|
|
48
|
+
except TextualNotInstalledError as exc: # pragma: no cover
|
|
49
|
+
raise RuntimeError(str(exc)) from exc
|
|
50
|
+
|
|
51
|
+
data = TuiData(config)
|
|
52
|
+
|
|
53
|
+
# Use shared SelectionState model for state
|
|
54
|
+
state = SelectionState(
|
|
55
|
+
devices=set(), groups=set(), sequences=set(), command_text=""
|
|
56
|
+
)
|
|
57
|
+
# Services layer for plan building and execution
|
|
58
|
+
service = ExecutionService(data)
|
|
59
|
+
|
|
60
|
+
# Note: Preview screen removed; we now show output in bottom TextLog
|
|
61
|
+
|
|
62
|
+
class _App(app_cls):
|
|
63
|
+
_errors: list[str]
|
|
64
|
+
_meta: list[str]
|
|
65
|
+
_output_lines: list[str]
|
|
66
|
+
_summary_filter: str
|
|
67
|
+
_output_filter: str
|
|
68
|
+
_summary_user_hidden: bool
|
|
69
|
+
_output_user_hidden: bool
|
|
70
|
+
_run_active: bool
|
|
71
|
+
_dark_mode: bool
|
|
72
|
+
_ui_thread_ident: int
|
|
73
|
+
_controller: TuiController
|
|
74
|
+
# Per-device output structures
|
|
75
|
+
_output_device_logs: dict[str, Any]
|
|
76
|
+
_output_device_lines: dict[str, list[str]]
|
|
77
|
+
_output_mgr: OutputPanelManager
|
|
78
|
+
|
|
79
|
+
CSS = APP_CSS
|
|
80
|
+
BINDINGS: ClassVar[list[Any]] = [
|
|
81
|
+
compat.mk_binding(
|
|
82
|
+
k.key,
|
|
83
|
+
k.action,
|
|
84
|
+
k.description,
|
|
85
|
+
show=k.show,
|
|
86
|
+
key_display=k.key_display,
|
|
87
|
+
priority=k.priority,
|
|
88
|
+
)
|
|
89
|
+
for k in KEYMAP
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
def compose(self) -> Any:
|
|
93
|
+
# Delegate UI tree building to the layout module
|
|
94
|
+
yield from compose_root(compat)
|
|
95
|
+
|
|
96
|
+
async def on_mount(self) -> None: # Populate lists after UI mounts
|
|
97
|
+
# Create controller on mount to avoid overriding __init__ on dynamic base
|
|
98
|
+
self._controller = TuiController(self, compat, data, service, state)
|
|
99
|
+
await self._controller.on_mount()
|
|
100
|
+
# Initialize per-device output structures
|
|
101
|
+
self._output_device_logs = {}
|
|
102
|
+
self._output_device_lines = {}
|
|
103
|
+
# Create output manager abstraction
|
|
104
|
+
self._output_mgr = OutputPanelManager(self, compat)
|
|
105
|
+
|
|
106
|
+
async def on_input_changed(self, event: Any) -> None: # Textual Input change
|
|
107
|
+
await self._controller.on_input_changed(event)
|
|
108
|
+
|
|
109
|
+
async def action_confirm(self) -> None:
|
|
110
|
+
out_log = self.query_one("#output-log")
|
|
111
|
+
if hasattr(out_log, "clear"):
|
|
112
|
+
out_log.clear()
|
|
113
|
+
# reset buffered output lines for fresh run
|
|
114
|
+
try:
|
|
115
|
+
self._output_lines = []
|
|
116
|
+
except Exception:
|
|
117
|
+
pass
|
|
118
|
+
# reset summary and any previous errors
|
|
119
|
+
self._errors = []
|
|
120
|
+
self._meta = []
|
|
121
|
+
start_ts: float | None = None
|
|
122
|
+
try:
|
|
123
|
+
self.query_one("#run-summary").update("")
|
|
124
|
+
self._hide_summary_panel()
|
|
125
|
+
self._hide_output_panel()
|
|
126
|
+
except Exception:
|
|
127
|
+
pass
|
|
128
|
+
self._add_meta("Starting run...")
|
|
129
|
+
# Temporarily silence library logging to avoid background metadata
|
|
130
|
+
logging.disable(logging.CRITICAL)
|
|
131
|
+
try:
|
|
132
|
+
# Mark run active and disable inputs & run button to avoid focus capture
|
|
133
|
+
self._run_active = True
|
|
134
|
+
self._set_inputs_enabled(False)
|
|
135
|
+
self._set_run_enabled(False)
|
|
136
|
+
start_ts = time.monotonic()
|
|
137
|
+
self._collect_state()
|
|
138
|
+
devices = await asyncio.to_thread(
|
|
139
|
+
service.resolve_devices, state.devices, state.groups
|
|
140
|
+
)
|
|
141
|
+
if not devices:
|
|
142
|
+
self._render_summary("No devices selected.")
|
|
143
|
+
try:
|
|
144
|
+
msg = "Status: idle — No devices selected."
|
|
145
|
+
self.query_one("#run-status").update(msg)
|
|
146
|
+
self._refresh_bottom_visibility()
|
|
147
|
+
except Exception:
|
|
148
|
+
pass
|
|
149
|
+
return
|
|
150
|
+
plan = service.build_plan(devices, state.sequences, state.command_text)
|
|
151
|
+
if not plan:
|
|
152
|
+
self._render_summary("No sequences or commands provided.")
|
|
153
|
+
try:
|
|
154
|
+
msg = "Status: idle — No sequences or commands provided."
|
|
155
|
+
self.query_one("#run-status").update(msg)
|
|
156
|
+
self._refresh_bottom_visibility()
|
|
157
|
+
except Exception:
|
|
158
|
+
pass
|
|
159
|
+
return
|
|
160
|
+
# Update status and run with streaming
|
|
161
|
+
total = len(plan)
|
|
162
|
+
try:
|
|
163
|
+
self._show_bottom_panel()
|
|
164
|
+
self.query_one("#run-status").update(f"Status: running 0/{total}")
|
|
165
|
+
except Exception:
|
|
166
|
+
pass
|
|
167
|
+
summary_result = await service.run_plan(
|
|
168
|
+
plan,
|
|
169
|
+
RunCallbacks(
|
|
170
|
+
on_output=lambda m: self.call_from_thread(
|
|
171
|
+
self._output_append, m
|
|
172
|
+
),
|
|
173
|
+
on_error=lambda m: self.call_from_thread(self._add_error, m),
|
|
174
|
+
on_meta=lambda m: self.call_from_thread(self._add_meta, m),
|
|
175
|
+
),
|
|
176
|
+
)
|
|
177
|
+
# Update summary panel (include errors if any)
|
|
178
|
+
try:
|
|
179
|
+
elapsed = time.monotonic() - start_ts
|
|
180
|
+
summary_with_time = (
|
|
181
|
+
f"{summary_result.human_summary()} (duration: {elapsed:.2f}s)"
|
|
182
|
+
)
|
|
183
|
+
self._render_summary(summary_with_time)
|
|
184
|
+
# Reflect summary on the status line; hint about errors if present
|
|
185
|
+
err_count = 0
|
|
186
|
+
try:
|
|
187
|
+
err_count = len(getattr(self, "_errors", []) or [])
|
|
188
|
+
except Exception:
|
|
189
|
+
err_count = 0
|
|
190
|
+
status_msg = f"Status: idle — {summary_with_time}"
|
|
191
|
+
if err_count:
|
|
192
|
+
status_msg += " — errors available (press s)"
|
|
193
|
+
self.query_one("#run-status").update(status_msg)
|
|
194
|
+
self._refresh_bottom_visibility()
|
|
195
|
+
except Exception:
|
|
196
|
+
pass
|
|
197
|
+
except Exception as e:
|
|
198
|
+
try:
|
|
199
|
+
elapsed = (
|
|
200
|
+
(time.monotonic() - start_ts) if (start_ts is not None) else 0.0
|
|
201
|
+
)
|
|
202
|
+
except Exception:
|
|
203
|
+
elapsed = 0.0
|
|
204
|
+
self._render_summary(f"Run failed: {e} (after {elapsed:.2f}s)")
|
|
205
|
+
try:
|
|
206
|
+
self.query_one("#run-status").update(
|
|
207
|
+
f"Status: idle — Run failed: {e}"
|
|
208
|
+
)
|
|
209
|
+
self._refresh_bottom_visibility()
|
|
210
|
+
except Exception:
|
|
211
|
+
pass
|
|
212
|
+
finally:
|
|
213
|
+
logging.disable(logging.NOTSET)
|
|
214
|
+
# Re-enable controls after run
|
|
215
|
+
try:
|
|
216
|
+
self._run_active = False
|
|
217
|
+
self._set_inputs_enabled(True)
|
|
218
|
+
self._set_run_enabled(True)
|
|
219
|
+
except Exception:
|
|
220
|
+
pass
|
|
221
|
+
|
|
222
|
+
async def on_input_submitted(self, event: Any) -> None: # Textual Input submit
|
|
223
|
+
await self._controller.on_input_submitted(event)
|
|
224
|
+
|
|
225
|
+
async def on_button_pressed(self, event: Any) -> None:
|
|
226
|
+
await self._controller.on_button_pressed(event)
|
|
227
|
+
|
|
228
|
+
async def action_cancel(self) -> None:
|
|
229
|
+
await self._controller.action_cancel()
|
|
230
|
+
|
|
231
|
+
def _dispatch_ui(self, fn: Any, *args: Any, **kwargs: Any) -> None:
|
|
232
|
+
"""Invoke a UI-mutating function safely from any thread.
|
|
233
|
+
|
|
234
|
+
If called from the UI thread, call directly. Otherwise, use
|
|
235
|
+
call_from_thread when available. Fallback to direct call.
|
|
236
|
+
"""
|
|
237
|
+
# If we are already on the UI thread, call directly
|
|
238
|
+
try:
|
|
239
|
+
ui_ident = getattr(self, "_ui_thread_ident", None)
|
|
240
|
+
except Exception:
|
|
241
|
+
ui_ident = None
|
|
242
|
+
current_ident = None
|
|
243
|
+
try:
|
|
244
|
+
current_ident = threading.get_ident()
|
|
245
|
+
except Exception:
|
|
246
|
+
pass
|
|
247
|
+
if ui_ident is not None and current_ident == ui_ident:
|
|
248
|
+
try:
|
|
249
|
+
fn(*args, **kwargs)
|
|
250
|
+
return
|
|
251
|
+
except Exception as exc:
|
|
252
|
+
logging.debug(f"UI dispatch direct call failed: {exc}")
|
|
253
|
+
# Otherwise schedule on UI thread if possible
|
|
254
|
+
try:
|
|
255
|
+
if hasattr(self, "call_from_thread"):
|
|
256
|
+
self.call_from_thread(fn, *args, **kwargs)
|
|
257
|
+
return
|
|
258
|
+
except Exception:
|
|
259
|
+
pass
|
|
260
|
+
# Last resort
|
|
261
|
+
try:
|
|
262
|
+
fn(*args, **kwargs)
|
|
263
|
+
except Exception as exc:
|
|
264
|
+
logging.debug(f"UI dispatch failed: {exc}")
|
|
265
|
+
|
|
266
|
+
def on_key(self, event: Any) -> None:
|
|
267
|
+
self._controller.on_key(event)
|
|
268
|
+
|
|
269
|
+
def _collect_state(self) -> None:
|
|
270
|
+
dev_list = self.query_one("#list-devices")
|
|
271
|
+
grp_list = self.query_one("#list-groups")
|
|
272
|
+
seq_list = self.query_one("#list-sequences")
|
|
273
|
+
cmd_input = self.query_one("#input-commands")
|
|
274
|
+
|
|
275
|
+
state.devices = self._selected_values(dev_list)
|
|
276
|
+
state.groups = self._selected_values(grp_list)
|
|
277
|
+
state.sequences = self._selected_values(seq_list)
|
|
278
|
+
state.command_text = getattr(cmd_input, "value", "") or ""
|
|
279
|
+
|
|
280
|
+
def _selected_values(self, sel: Any) -> set[str]:
|
|
281
|
+
"""Normalize selected items from a SelectionList to a set of strings."""
|
|
282
|
+
# Prefer newer API if available
|
|
283
|
+
if hasattr(sel, "selected_values"):
|
|
284
|
+
try:
|
|
285
|
+
values = sel.selected_values
|
|
286
|
+
if values is not None:
|
|
287
|
+
return {str(v) for v in values}
|
|
288
|
+
except Exception:
|
|
289
|
+
pass
|
|
290
|
+
# Fallback to iterating over selected items which may be strings or option objects
|
|
291
|
+
out: set[str] = set()
|
|
292
|
+
try:
|
|
293
|
+
for item in getattr(sel, "selected", []) or []:
|
|
294
|
+
val = getattr(item, "value", item)
|
|
295
|
+
out.add(str(val))
|
|
296
|
+
except Exception:
|
|
297
|
+
# Last resort: empty set on unexpected structure
|
|
298
|
+
return set()
|
|
299
|
+
return out
|
|
300
|
+
|
|
301
|
+
def _populate_selection_list(
|
|
302
|
+
self,
|
|
303
|
+
widget_id: str,
|
|
304
|
+
items: list[str],
|
|
305
|
+
*,
|
|
306
|
+
selected: set[str] | None = None,
|
|
307
|
+
) -> None:
|
|
308
|
+
"""Populate a SelectionList with best-effort API compatibility across Textual versions.
|
|
309
|
+
|
|
310
|
+
If `selected` is provided, best-effort pre-select those values.
|
|
311
|
+
"""
|
|
312
|
+
try:
|
|
313
|
+
sel = self.query_one(f"#{widget_id}")
|
|
314
|
+
except Exception:
|
|
315
|
+
return
|
|
316
|
+
|
|
317
|
+
# Clear existing options
|
|
318
|
+
for method in ("clear_options", "clear"):
|
|
319
|
+
if hasattr(sel, method):
|
|
320
|
+
try:
|
|
321
|
+
getattr(sel, method)()
|
|
322
|
+
break
|
|
323
|
+
except Exception:
|
|
324
|
+
pass
|
|
325
|
+
|
|
326
|
+
# Try bulk add first
|
|
327
|
+
if hasattr(sel, "add_options"):
|
|
328
|
+
sel_set = selected or set()
|
|
329
|
+
opts_variants: list[list[object]] = [
|
|
330
|
+
[(label, label, (label in sel_set)) for label in items],
|
|
331
|
+
[(label, label) for label in items],
|
|
332
|
+
list(items),
|
|
333
|
+
]
|
|
334
|
+
for opts in opts_variants:
|
|
335
|
+
try:
|
|
336
|
+
sel.add_options(opts)
|
|
337
|
+
# Some variants ignore selection; enforce afterwards if needed
|
|
338
|
+
if selected:
|
|
339
|
+
self._try_apply_selection(sel, selected)
|
|
340
|
+
return
|
|
341
|
+
except Exception as exc:
|
|
342
|
+
logging.debug(f"add_options variant failed: {exc}")
|
|
343
|
+
|
|
344
|
+
# Fallback to adding one by one with different signatures
|
|
345
|
+
if hasattr(sel, "add_option"):
|
|
346
|
+
for label in items:
|
|
347
|
+
added = False
|
|
348
|
+
for args in (
|
|
349
|
+
(label, label, (selected is not None and label in selected)),
|
|
350
|
+
(label, label),
|
|
351
|
+
(label,),
|
|
352
|
+
((label, label, False),),
|
|
353
|
+
((label, label),),
|
|
354
|
+
):
|
|
355
|
+
try:
|
|
356
|
+
sel.add_option(*args)
|
|
357
|
+
added = True
|
|
358
|
+
break
|
|
359
|
+
except Exception as exc:
|
|
360
|
+
logging.debug(f"add_option signature failed: {exc}")
|
|
361
|
+
if not added:
|
|
362
|
+
# Last resort: try 'append' of an option-like tuple
|
|
363
|
+
try:
|
|
364
|
+
if hasattr(sel, "append"):
|
|
365
|
+
sel.append(
|
|
366
|
+
(
|
|
367
|
+
label,
|
|
368
|
+
label,
|
|
369
|
+
(selected is not None and label in selected),
|
|
370
|
+
)
|
|
371
|
+
)
|
|
372
|
+
except Exception as exc:
|
|
373
|
+
logging.debug(f"append to selection list failed: {exc}")
|
|
374
|
+
# Enforce selection afterwards
|
|
375
|
+
if selected:
|
|
376
|
+
self._try_apply_selection(sel, selected)
|
|
377
|
+
|
|
378
|
+
def _try_apply_selection(self, sel: Any, selected: set[str]) -> None:
|
|
379
|
+
"""Best-effort to set selected values on a SelectionList across versions."""
|
|
380
|
+
try:
|
|
381
|
+
if hasattr(sel, "set_value"):
|
|
382
|
+
sel.set_value(list(selected))
|
|
383
|
+
return
|
|
384
|
+
except Exception:
|
|
385
|
+
pass
|
|
386
|
+
try:
|
|
387
|
+
if hasattr(sel, "selected") and isinstance(sel.selected, list):
|
|
388
|
+
sel.selected = list(selected)
|
|
389
|
+
return
|
|
390
|
+
except Exception as exc:
|
|
391
|
+
logging.debug(f"Direct selected assignment failed: {exc}")
|
|
392
|
+
# Fallback: toggle/select by value methods
|
|
393
|
+
for val in selected:
|
|
394
|
+
for method in ("select", "select_by_value", "toggle_value"):
|
|
395
|
+
if hasattr(sel, method):
|
|
396
|
+
try:
|
|
397
|
+
getattr(sel, method)(val)
|
|
398
|
+
break
|
|
399
|
+
except Exception as exc:
|
|
400
|
+
logging.debug(f"Selecting value via {method} failed: {exc}")
|
|
401
|
+
|
|
402
|
+
def _clear_selection_list(self, widget_id: str) -> None:
|
|
403
|
+
"""Deselect all options in a SelectionList with broad API compatibility."""
|
|
404
|
+
try:
|
|
405
|
+
sel = self.query_one(f"#{widget_id}")
|
|
406
|
+
except Exception:
|
|
407
|
+
return
|
|
408
|
+
# Try explicit clearing methods first
|
|
409
|
+
for method in (
|
|
410
|
+
"clear_selection",
|
|
411
|
+
"clear_selected",
|
|
412
|
+
"deselect_all",
|
|
413
|
+
"select_none",
|
|
414
|
+
):
|
|
415
|
+
if hasattr(sel, method):
|
|
416
|
+
try:
|
|
417
|
+
getattr(sel, method)()
|
|
418
|
+
return
|
|
419
|
+
except Exception as exc:
|
|
420
|
+
logging.debug(f"{method} failed on {widget_id}: {exc}")
|
|
421
|
+
# Next, try setting empty value/selected list
|
|
422
|
+
for attr, _ in (("set_value", []), ("selected", [])):
|
|
423
|
+
try:
|
|
424
|
+
if hasattr(sel, attr):
|
|
425
|
+
if attr == "set_value":
|
|
426
|
+
sel.set_value([])
|
|
427
|
+
else:
|
|
428
|
+
setattr(sel, attr, [])
|
|
429
|
+
return
|
|
430
|
+
except Exception as exc:
|
|
431
|
+
logging.debug(f"Clearing via {attr} failed on {widget_id}: {exc}")
|
|
432
|
+
# Fallback: toggle off currently selected values
|
|
433
|
+
try:
|
|
434
|
+
current: set[str] = self._selected_values(sel)
|
|
435
|
+
except Exception:
|
|
436
|
+
current = set()
|
|
437
|
+
if not current:
|
|
438
|
+
return
|
|
439
|
+
for val in list(current):
|
|
440
|
+
for method in (
|
|
441
|
+
"deselect",
|
|
442
|
+
"deselect_by_value",
|
|
443
|
+
"unselect",
|
|
444
|
+
"toggle_value",
|
|
445
|
+
):
|
|
446
|
+
if hasattr(sel, method):
|
|
447
|
+
try:
|
|
448
|
+
getattr(sel, method)(val)
|
|
449
|
+
break
|
|
450
|
+
except Exception as exc:
|
|
451
|
+
logging.debug(
|
|
452
|
+
f"Deselecting value via {method} failed on {widget_id}: {exc}"
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
def _clear_all_selections(self) -> None:
|
|
456
|
+
"""Deselect all currently selected devices, groups, and sequences."""
|
|
457
|
+
try:
|
|
458
|
+
self._clear_selection_list("list-devices")
|
|
459
|
+
except Exception:
|
|
460
|
+
pass
|
|
461
|
+
try:
|
|
462
|
+
self._clear_selection_list("list-groups")
|
|
463
|
+
except Exception:
|
|
464
|
+
pass
|
|
465
|
+
try:
|
|
466
|
+
self._clear_selection_list("list-sequences")
|
|
467
|
+
except Exception:
|
|
468
|
+
pass
|
|
469
|
+
# Also clear state model selections
|
|
470
|
+
try:
|
|
471
|
+
state.devices.clear()
|
|
472
|
+
state.groups.clear()
|
|
473
|
+
state.sequences.clear()
|
|
474
|
+
except Exception:
|
|
475
|
+
pass
|
|
476
|
+
|
|
477
|
+
# Device resolution, plan building, and execution handled by services layer
|
|
478
|
+
|
|
479
|
+
def _add_error(self, msg: str) -> None:
|
|
480
|
+
try:
|
|
481
|
+
self._errors.append(str(msg))
|
|
482
|
+
except Exception:
|
|
483
|
+
self._errors = [str(msg)]
|
|
484
|
+
# refresh summary live
|
|
485
|
+
try:
|
|
486
|
+
self._render_summary()
|
|
487
|
+
except Exception:
|
|
488
|
+
pass
|
|
489
|
+
|
|
490
|
+
def _render_summary(self, base_summary: str | None = None) -> None:
|
|
491
|
+
try:
|
|
492
|
+
errors: list[str] = getattr(self, "_errors", [])
|
|
493
|
+
except Exception:
|
|
494
|
+
errors = []
|
|
495
|
+
try:
|
|
496
|
+
meta: list[str] = getattr(self, "_meta", [])
|
|
497
|
+
except Exception:
|
|
498
|
+
meta = []
|
|
499
|
+
renderable: Any = None
|
|
500
|
+
# If a filter is active, compile plain text and filter lines for simplicity
|
|
501
|
+
filt = (getattr(self, "_summary_filter", "") or "").strip().lower()
|
|
502
|
+
if filt:
|
|
503
|
+
lines: list[str] = []
|
|
504
|
+
if base_summary:
|
|
505
|
+
lines.append(base_summary)
|
|
506
|
+
# Info first
|
|
507
|
+
if meta:
|
|
508
|
+
lines.append("Info:")
|
|
509
|
+
lines.extend([f" • {m}" for m in meta])
|
|
510
|
+
# Errors at the bottom
|
|
511
|
+
if errors:
|
|
512
|
+
lines.append("Errors:")
|
|
513
|
+
lines.extend([f" • {e}" for e in errors])
|
|
514
|
+
filtered = [ln for ln in lines if filt in ln.lower()]
|
|
515
|
+
renderable = "\n".join(filtered)
|
|
516
|
+
else:
|
|
517
|
+
# Try to build a Rich Text for styled output (errors in red)
|
|
518
|
+
try:
|
|
519
|
+
rich_text = _new_rich_text()
|
|
520
|
+
if rich_text is not None:
|
|
521
|
+
first = True
|
|
522
|
+
if base_summary:
|
|
523
|
+
rich_text.append(base_summary)
|
|
524
|
+
first = False
|
|
525
|
+
if meta:
|
|
526
|
+
if not first:
|
|
527
|
+
rich_text.append("\n")
|
|
528
|
+
rich_text.append("Info:", style="bold")
|
|
529
|
+
for m in meta:
|
|
530
|
+
rich_text.append("\n • ")
|
|
531
|
+
rich_text.append(str(m))
|
|
532
|
+
first = False
|
|
533
|
+
if errors:
|
|
534
|
+
if not first:
|
|
535
|
+
rich_text.append("\n")
|
|
536
|
+
rich_text.append("Errors:", style="bold")
|
|
537
|
+
for err in errors:
|
|
538
|
+
rich_text.append("\n • ")
|
|
539
|
+
rich_text.append(str(err), style="red")
|
|
540
|
+
renderable = rich_text
|
|
541
|
+
except Exception:
|
|
542
|
+
renderable = None
|
|
543
|
+
if renderable is None:
|
|
544
|
+
# Fallback to plain text without styling
|
|
545
|
+
parts: list[str] = []
|
|
546
|
+
if base_summary:
|
|
547
|
+
parts.append(base_summary)
|
|
548
|
+
if meta:
|
|
549
|
+
parts.append("Info:")
|
|
550
|
+
parts.extend([f" • {m}" for m in meta])
|
|
551
|
+
if errors:
|
|
552
|
+
parts.append("Errors:")
|
|
553
|
+
parts.extend([f" • {e}" for e in errors])
|
|
554
|
+
renderable = "\n".join(parts)
|
|
555
|
+
try:
|
|
556
|
+
widget = self.query_one("#run-summary")
|
|
557
|
+
except Exception:
|
|
558
|
+
widget = None
|
|
559
|
+
if widget is not None:
|
|
560
|
+
try:
|
|
561
|
+
# Clear previous content if it's a Log-like widget
|
|
562
|
+
if hasattr(widget, "clear"):
|
|
563
|
+
widget.clear()
|
|
564
|
+
except Exception:
|
|
565
|
+
pass
|
|
566
|
+
try:
|
|
567
|
+
if hasattr(widget, "update"):
|
|
568
|
+
widget.update(renderable)
|
|
569
|
+
elif hasattr(widget, "write"):
|
|
570
|
+
text = str(renderable)
|
|
571
|
+
for line in text.splitlines():
|
|
572
|
+
widget.write(line)
|
|
573
|
+
else:
|
|
574
|
+
# Last resort
|
|
575
|
+
widget.update(str(renderable))
|
|
576
|
+
except Exception:
|
|
577
|
+
pass
|
|
578
|
+
# Auto-show summary only when there are errors; otherwise keep user control
|
|
579
|
+
should_show = bool(errors)
|
|
580
|
+
if should_show and not getattr(self, "_summary_user_hidden", False):
|
|
581
|
+
self._show_summary_panel()
|
|
582
|
+
|
|
583
|
+
def _add_meta(self, msg: str) -> None:
|
|
584
|
+
try:
|
|
585
|
+
self._meta.append(str(msg))
|
|
586
|
+
except Exception:
|
|
587
|
+
self._meta = [str(msg)]
|
|
588
|
+
# refresh summary live
|
|
589
|
+
try:
|
|
590
|
+
self._render_summary()
|
|
591
|
+
except Exception:
|
|
592
|
+
pass
|
|
593
|
+
|
|
594
|
+
def _output_append(self, msg: str) -> None:
|
|
595
|
+
"""Append output to the All tab via the manager."""
|
|
596
|
+
self._output_mgr.append_all(msg)
|
|
597
|
+
|
|
598
|
+
def _sanitize_id(self, name: str) -> str:
|
|
599
|
+
try:
|
|
600
|
+
import re
|
|
601
|
+
|
|
602
|
+
return re.sub(r"[^A-Za-z0-9_.-]", "-", name)
|
|
603
|
+
except Exception:
|
|
604
|
+
return name.replace(" ", "-")
|
|
605
|
+
|
|
606
|
+
def _ensure_device_tab(self, device: str) -> Any:
|
|
607
|
+
"""Ensure a per-device output tab and return its TextLog widget."""
|
|
608
|
+
return self._output_mgr.ensure_device_tab(device)
|
|
609
|
+
|
|
610
|
+
def _output_append_device(self, device: str, msg: str) -> None:
|
|
611
|
+
"""Append output for a specific device and also to the All tab."""
|
|
612
|
+
try:
|
|
613
|
+
text = str(msg)
|
|
614
|
+
except Exception:
|
|
615
|
+
text = f"{msg}"
|
|
616
|
+
lines = list(text.splitlines())
|
|
617
|
+
if not lines:
|
|
618
|
+
return
|
|
619
|
+
# Delegate to manager
|
|
620
|
+
self._output_mgr.append_device(device, msg)
|
|
621
|
+
|
|
622
|
+
def _reset_output_tabs(self) -> None:
|
|
623
|
+
"""Clear All output and remove all per-device tabs; reset buffers."""
|
|
624
|
+
self._output_mgr.reset()
|
|
625
|
+
|
|
626
|
+
def _apply_output_filter(self, value: str) -> None:
|
|
627
|
+
self._output_mgr.apply_filter(value)
|
|
628
|
+
|
|
629
|
+
def _show_output_panel(self) -> None:
|
|
630
|
+
try:
|
|
631
|
+
panel = self.query_one("#output-panel")
|
|
632
|
+
except Exception:
|
|
633
|
+
return
|
|
634
|
+
try:
|
|
635
|
+
panel.remove_class("hidden")
|
|
636
|
+
except Exception:
|
|
637
|
+
try:
|
|
638
|
+
styles = getattr(panel, "styles", None)
|
|
639
|
+
if styles and hasattr(styles, "display"):
|
|
640
|
+
styles.display = "block"
|
|
641
|
+
except Exception:
|
|
642
|
+
pass
|
|
643
|
+
self._refresh_bottom_visibility()
|
|
644
|
+
|
|
645
|
+
def _hide_output_panel(self) -> None:
|
|
646
|
+
try:
|
|
647
|
+
panel = self.query_one("#output-panel")
|
|
648
|
+
except Exception:
|
|
649
|
+
return
|
|
650
|
+
try:
|
|
651
|
+
panel.add_class("hidden")
|
|
652
|
+
except Exception:
|
|
653
|
+
try:
|
|
654
|
+
styles = getattr(panel, "styles", None)
|
|
655
|
+
if styles and hasattr(styles, "display"):
|
|
656
|
+
styles.display = "none"
|
|
657
|
+
except Exception:
|
|
658
|
+
pass
|
|
659
|
+
self._refresh_bottom_visibility()
|
|
660
|
+
|
|
661
|
+
def _maybe_show_output_panel(self) -> None:
|
|
662
|
+
"""Show output panel only if user hasn't hidden it explicitly."""
|
|
663
|
+
if not getattr(self, "_output_user_hidden", False):
|
|
664
|
+
self._show_output_panel()
|
|
665
|
+
|
|
666
|
+
def action_toggle_summary(self) -> None:
|
|
667
|
+
"""Toggle visibility of the summary panel."""
|
|
668
|
+
try:
|
|
669
|
+
panel = self.query_one("#summary-panel")
|
|
670
|
+
except Exception:
|
|
671
|
+
return
|
|
672
|
+
# Determine if currently hidden via 'hidden' class
|
|
673
|
+
try:
|
|
674
|
+
classes: set[str] = set(getattr(panel, "classes", []) or [])
|
|
675
|
+
except Exception:
|
|
676
|
+
classes = set()
|
|
677
|
+
is_hidden = "hidden" in classes
|
|
678
|
+
if is_hidden:
|
|
679
|
+
# User explicitly wants to see it; clear the hidden flag
|
|
680
|
+
self._summary_user_hidden = False
|
|
681
|
+
# Always show when user toggles on, regardless of current content
|
|
682
|
+
self._show_summary_panel()
|
|
683
|
+
else:
|
|
684
|
+
# User hides the panel; set the flag and hide
|
|
685
|
+
self._summary_user_hidden = True
|
|
686
|
+
self._hide_summary_panel()
|
|
687
|
+
self._refresh_bottom_visibility()
|
|
688
|
+
|
|
689
|
+
def action_toggle_output(self) -> None:
|
|
690
|
+
"""Toggle visibility of the output panel."""
|
|
691
|
+
try:
|
|
692
|
+
panel = self.query_one("#output-panel")
|
|
693
|
+
except Exception:
|
|
694
|
+
return
|
|
695
|
+
try:
|
|
696
|
+
classes: set[str] = set(getattr(panel, "classes", []) or [])
|
|
697
|
+
except Exception:
|
|
698
|
+
classes = set()
|
|
699
|
+
is_hidden = "hidden" in classes
|
|
700
|
+
if is_hidden:
|
|
701
|
+
self._output_user_hidden = False
|
|
702
|
+
# Always show when user toggles on
|
|
703
|
+
self._show_output_panel()
|
|
704
|
+
else:
|
|
705
|
+
self._output_user_hidden = True
|
|
706
|
+
self._hide_output_panel()
|
|
707
|
+
self._refresh_bottom_visibility()
|
|
708
|
+
|
|
709
|
+
def _set_inputs_enabled(self, enabled: bool) -> None:
|
|
710
|
+
"""Enable/disable search and command inputs during a run to avoid key capture."""
|
|
711
|
+
ids = [
|
|
712
|
+
"#filter-devices",
|
|
713
|
+
"#filter-groups",
|
|
714
|
+
"#filter-sequences",
|
|
715
|
+
"#filter-summary",
|
|
716
|
+
"#filter-output",
|
|
717
|
+
"#input-commands",
|
|
718
|
+
]
|
|
719
|
+
for wid in ids:
|
|
720
|
+
try:
|
|
721
|
+
w = self.query_one(wid)
|
|
722
|
+
except Exception:
|
|
723
|
+
w = None
|
|
724
|
+
if w is None:
|
|
725
|
+
continue
|
|
726
|
+
# Prefer 'disabled' attribute when available
|
|
727
|
+
try:
|
|
728
|
+
if hasattr(w, "disabled"):
|
|
729
|
+
w.disabled = not enabled
|
|
730
|
+
elif hasattr(w, "can_focus"):
|
|
731
|
+
w.can_focus = enabled
|
|
732
|
+
except Exception:
|
|
733
|
+
pass
|
|
734
|
+
|
|
735
|
+
def _set_run_enabled(self, enabled: bool) -> None:
|
|
736
|
+
try:
|
|
737
|
+
btn = self.query_one("#run-button")
|
|
738
|
+
if hasattr(btn, "disabled"):
|
|
739
|
+
btn.disabled = not enabled
|
|
740
|
+
except Exception:
|
|
741
|
+
pass
|
|
742
|
+
|
|
743
|
+
# --- Clipboard utilities & copy actions
|
|
744
|
+
def _copy_to_clipboard(self, text: str) -> bool:
|
|
745
|
+
return copy_to_clipboard(self, text)
|
|
746
|
+
|
|
747
|
+
def action_copy_status(self) -> None:
|
|
748
|
+
"""Copy the current status line text to clipboard."""
|
|
749
|
+
try:
|
|
750
|
+
status_widget = self.query_one("#run-status")
|
|
751
|
+
except Exception:
|
|
752
|
+
status_widget = None
|
|
753
|
+
text = ""
|
|
754
|
+
if status_widget is not None:
|
|
755
|
+
try:
|
|
756
|
+
content = getattr(status_widget, "renderable", None)
|
|
757
|
+
if content is None:
|
|
758
|
+
content = getattr(status_widget, "text", None)
|
|
759
|
+
text = str(content) if content is not None else ""
|
|
760
|
+
except Exception:
|
|
761
|
+
text = ""
|
|
762
|
+
if not text:
|
|
763
|
+
text = "Status: idle"
|
|
764
|
+
ok = self._copy_to_clipboard(text)
|
|
765
|
+
self._add_meta(
|
|
766
|
+
"Status copied to clipboard" if ok else "Could not access clipboard"
|
|
767
|
+
)
|
|
768
|
+
|
|
769
|
+
def action_copy_last_error(self) -> None:
|
|
770
|
+
"""Copy the last error message to clipboard.
|
|
771
|
+
|
|
772
|
+
Priority order:
|
|
773
|
+
1) Last recorded error from error callbacks
|
|
774
|
+
2) Last output line containing 'error'
|
|
775
|
+
3) Entire status line if it mentions an error
|
|
776
|
+
"""
|
|
777
|
+
err_text: str | None = None
|
|
778
|
+
try:
|
|
779
|
+
errs = getattr(self, "_errors", []) or []
|
|
780
|
+
if errs:
|
|
781
|
+
err_text = str(errs[-1])
|
|
782
|
+
except Exception:
|
|
783
|
+
err_text = None
|
|
784
|
+
if not err_text:
|
|
785
|
+
try:
|
|
786
|
+
lines = list(getattr(self, "_output_lines", []) or [])
|
|
787
|
+
except Exception:
|
|
788
|
+
lines = []
|
|
789
|
+
for ln in reversed(lines):
|
|
790
|
+
try:
|
|
791
|
+
if "error" in ln.lower():
|
|
792
|
+
err_text = ln
|
|
793
|
+
break
|
|
794
|
+
except Exception as exc:
|
|
795
|
+
logging.debug(f"Scanning output line for error failed: {exc}")
|
|
796
|
+
continue
|
|
797
|
+
if not err_text:
|
|
798
|
+
try:
|
|
799
|
+
status_widget = self.query_one("#run-status")
|
|
800
|
+
content = getattr(status_widget, "renderable", None)
|
|
801
|
+
if content is None:
|
|
802
|
+
content = getattr(status_widget, "text", None)
|
|
803
|
+
status_text = str(content) if content is not None else ""
|
|
804
|
+
if "error" in status_text.lower():
|
|
805
|
+
err_text = status_text
|
|
806
|
+
except Exception:
|
|
807
|
+
err_text = None
|
|
808
|
+
if not err_text:
|
|
809
|
+
self._add_meta("No error found to copy")
|
|
810
|
+
return
|
|
811
|
+
ok = self._copy_to_clipboard(err_text)
|
|
812
|
+
self._add_meta(
|
|
813
|
+
"Error copied to clipboard" if ok else "Could not access clipboard"
|
|
814
|
+
)
|
|
815
|
+
|
|
816
|
+
def _show_summary_panel(self) -> None:
|
|
817
|
+
# Ensure bottom area is visible and unhide summary panel
|
|
818
|
+
self._show_bottom_panel()
|
|
819
|
+
try:
|
|
820
|
+
panel = self.query_one("#summary-panel")
|
|
821
|
+
except Exception:
|
|
822
|
+
panel = None
|
|
823
|
+
if panel is not None:
|
|
824
|
+
try:
|
|
825
|
+
panel.remove_class("hidden")
|
|
826
|
+
except Exception:
|
|
827
|
+
try:
|
|
828
|
+
styles = getattr(panel, "styles", None)
|
|
829
|
+
if styles and hasattr(styles, "display"):
|
|
830
|
+
styles.display = "block"
|
|
831
|
+
except Exception:
|
|
832
|
+
pass
|
|
833
|
+
self._refresh_bottom_visibility()
|
|
834
|
+
|
|
835
|
+
def _show_help_panel(self) -> None:
|
|
836
|
+
try:
|
|
837
|
+
panel = self.query_one("#help-panel")
|
|
838
|
+
except Exception:
|
|
839
|
+
return
|
|
840
|
+
try:
|
|
841
|
+
panel.remove_class("hidden")
|
|
842
|
+
except Exception:
|
|
843
|
+
try:
|
|
844
|
+
styles = getattr(panel, "styles", None)
|
|
845
|
+
if styles and hasattr(styles, "display"):
|
|
846
|
+
styles.display = "block"
|
|
847
|
+
except Exception:
|
|
848
|
+
pass
|
|
849
|
+
self._refresh_bottom_visibility()
|
|
850
|
+
|
|
851
|
+
def _hide_help_panel(self) -> None:
|
|
852
|
+
try:
|
|
853
|
+
panel = self.query_one("#help-panel")
|
|
854
|
+
except Exception:
|
|
855
|
+
return
|
|
856
|
+
try:
|
|
857
|
+
panel.add_class("hidden")
|
|
858
|
+
except Exception:
|
|
859
|
+
try:
|
|
860
|
+
styles = getattr(panel, "styles", None)
|
|
861
|
+
if styles and hasattr(styles, "display"):
|
|
862
|
+
styles.display = "none"
|
|
863
|
+
except Exception:
|
|
864
|
+
pass
|
|
865
|
+
self._refresh_bottom_visibility()
|
|
866
|
+
|
|
867
|
+
def _show_bottom_panel(self) -> None:
|
|
868
|
+
try:
|
|
869
|
+
container = self.query_one("#bottom")
|
|
870
|
+
except Exception:
|
|
871
|
+
return
|
|
872
|
+
try:
|
|
873
|
+
container.remove_class("hidden")
|
|
874
|
+
except Exception:
|
|
875
|
+
try:
|
|
876
|
+
styles = getattr(container, "styles", None)
|
|
877
|
+
if styles and hasattr(styles, "display"):
|
|
878
|
+
styles.display = "block"
|
|
879
|
+
except Exception:
|
|
880
|
+
pass
|
|
881
|
+
|
|
882
|
+
def _hide_bottom_panel(self) -> None:
|
|
883
|
+
try:
|
|
884
|
+
container = self.query_one("#bottom")
|
|
885
|
+
except Exception:
|
|
886
|
+
return
|
|
887
|
+
try:
|
|
888
|
+
container.add_class("hidden")
|
|
889
|
+
except Exception:
|
|
890
|
+
try:
|
|
891
|
+
styles = getattr(container, "styles", None)
|
|
892
|
+
if styles and hasattr(styles, "display"):
|
|
893
|
+
styles.display = "none"
|
|
894
|
+
except Exception:
|
|
895
|
+
pass
|
|
896
|
+
|
|
897
|
+
def _refresh_bottom_visibility(self) -> None:
|
|
898
|
+
"""Hide entire bottom area if no output and summary is hidden and status is idle."""
|
|
899
|
+
try:
|
|
900
|
+
summary_panel = self.query_one("#summary-panel")
|
|
901
|
+
help_panel = self.query_one("#help-panel")
|
|
902
|
+
output_panel = self.query_one("#output-panel")
|
|
903
|
+
status_widget = self.query_one("#run-status")
|
|
904
|
+
bottom_container = self.query_one("#bottom")
|
|
905
|
+
except Exception:
|
|
906
|
+
return
|
|
907
|
+
try:
|
|
908
|
+
s_classes: set[str] = set(getattr(summary_panel, "classes", []) or [])
|
|
909
|
+
h_classes: set[str] = set(getattr(help_panel, "classes", []) or [])
|
|
910
|
+
o_classes: set[str] = set(getattr(output_panel, "classes", []) or [])
|
|
911
|
+
is_summary_hidden = "hidden" in s_classes
|
|
912
|
+
is_help_hidden = "hidden" in h_classes
|
|
913
|
+
is_output_hidden = "hidden" in o_classes
|
|
914
|
+
except Exception:
|
|
915
|
+
is_summary_hidden = False
|
|
916
|
+
is_help_hidden = False
|
|
917
|
+
is_output_hidden = False
|
|
918
|
+
try:
|
|
919
|
+
status_text = getattr(status_widget, "renderable", "") or getattr(
|
|
920
|
+
status_widget, "text", ""
|
|
921
|
+
)
|
|
922
|
+
status_str = (
|
|
923
|
+
str(status_text)
|
|
924
|
+
if not isinstance(status_text, str)
|
|
925
|
+
else status_text
|
|
926
|
+
)
|
|
927
|
+
# Show bottom if status has more than the plain idle marker
|
|
928
|
+
has_status_info = status_str.strip() not in {"", "Status: idle"}
|
|
929
|
+
except Exception:
|
|
930
|
+
has_status_info = False
|
|
931
|
+
if (
|
|
932
|
+
is_summary_hidden
|
|
933
|
+
and is_help_hidden
|
|
934
|
+
and is_output_hidden
|
|
935
|
+
and not has_status_info
|
|
936
|
+
):
|
|
937
|
+
self._hide_bottom_panel()
|
|
938
|
+
else:
|
|
939
|
+
self._show_bottom_panel()
|
|
940
|
+
# Expand bottom when any panel is visible; otherwise compact to auto height
|
|
941
|
+
try:
|
|
942
|
+
any_panel_visible = not (
|
|
943
|
+
is_summary_hidden and is_help_hidden and is_output_hidden
|
|
944
|
+
)
|
|
945
|
+
if any_panel_visible:
|
|
946
|
+
bottom_container.add_class("expanded")
|
|
947
|
+
else:
|
|
948
|
+
bottom_container.remove_class("expanded")
|
|
949
|
+
except Exception:
|
|
950
|
+
# Best-effort; ignore if styles not supported
|
|
951
|
+
pass
|
|
952
|
+
|
|
953
|
+
def _hide_summary_panel(self) -> None:
|
|
954
|
+
try:
|
|
955
|
+
panel = self.query_one("#summary-panel")
|
|
956
|
+
except Exception:
|
|
957
|
+
return
|
|
958
|
+
try:
|
|
959
|
+
panel.add_class("hidden")
|
|
960
|
+
except Exception:
|
|
961
|
+
try:
|
|
962
|
+
styles = getattr(panel, "styles", None)
|
|
963
|
+
if styles and hasattr(styles, "display"):
|
|
964
|
+
styles.display = "none"
|
|
965
|
+
except Exception:
|
|
966
|
+
pass
|
|
967
|
+
|
|
968
|
+
def action_focus_filter(self) -> None:
|
|
969
|
+
"""Focus the most relevant filter/input for the active pane."""
|
|
970
|
+
target_input_id: str | None = None
|
|
971
|
+
focus = None
|
|
972
|
+
# Try to get currently focused widget
|
|
973
|
+
for attr in ("focused",):
|
|
974
|
+
try:
|
|
975
|
+
focus = getattr(self, attr, None) or getattr(
|
|
976
|
+
self.screen, attr, None
|
|
977
|
+
)
|
|
978
|
+
except Exception as exc:
|
|
979
|
+
logging.debug(f"Focus attribute retrieval failed: {exc}")
|
|
980
|
+
continue
|
|
981
|
+
if focus is not None:
|
|
982
|
+
break
|
|
983
|
+
|
|
984
|
+
# Helper to check ancestry membership
|
|
985
|
+
def within(container_id: str) -> bool:
|
|
986
|
+
try:
|
|
987
|
+
container = self.query_one(f"#{container_id}")
|
|
988
|
+
except Exception:
|
|
989
|
+
return False
|
|
990
|
+
node = focus
|
|
991
|
+
seen = 0
|
|
992
|
+
while node is not None and seen < 100:
|
|
993
|
+
if node is container:
|
|
994
|
+
return True
|
|
995
|
+
try:
|
|
996
|
+
node = getattr(node, "parent", None)
|
|
997
|
+
except Exception as exc:
|
|
998
|
+
logging.debug(f"Focus ancestry traversal failed: {exc}")
|
|
999
|
+
node = None
|
|
1000
|
+
seen += 1
|
|
1001
|
+
return False
|
|
1002
|
+
|
|
1003
|
+
# Prefer bottom panels when focus is inside them
|
|
1004
|
+
try:
|
|
1005
|
+
if within("output-panel"):
|
|
1006
|
+
target_input_id = "filter-output"
|
|
1007
|
+
elif within("summary-panel"):
|
|
1008
|
+
target_input_id = "filter-summary"
|
|
1009
|
+
except Exception as exc:
|
|
1010
|
+
logging.debug(f"Bottom panel detection failed: {exc}")
|
|
1011
|
+
|
|
1012
|
+
# If not bottom, detect active top tab via focus location
|
|
1013
|
+
if target_input_id is None:
|
|
1014
|
+
if within("tab-devices"):
|
|
1015
|
+
target_input_id = "filter-devices"
|
|
1016
|
+
elif within("tab-groups"):
|
|
1017
|
+
target_input_id = "filter-groups"
|
|
1018
|
+
elif within("tab-sequences"):
|
|
1019
|
+
target_input_id = "filter-sequences"
|
|
1020
|
+
elif within("tab-commands"):
|
|
1021
|
+
# Not a filter but sensible default
|
|
1022
|
+
target_input_id = "input-commands"
|
|
1023
|
+
|
|
1024
|
+
# If still unknown, look up active tabs by container state
|
|
1025
|
+
if target_input_id is None:
|
|
1026
|
+
# Check targets-tabs active
|
|
1027
|
+
try:
|
|
1028
|
+
tabs = self.query_one("#targets-tabs")
|
|
1029
|
+
active = getattr(tabs, "active", None)
|
|
1030
|
+
active_id = getattr(active, "id", None) or str(active or "")
|
|
1031
|
+
if "devices" in str(active_id):
|
|
1032
|
+
target_input_id = "filter-devices"
|
|
1033
|
+
elif "groups" in str(active_id):
|
|
1034
|
+
target_input_id = "filter-groups"
|
|
1035
|
+
except Exception:
|
|
1036
|
+
pass
|
|
1037
|
+
if target_input_id is None:
|
|
1038
|
+
try:
|
|
1039
|
+
tabs = self.query_one("#actions-tabs")
|
|
1040
|
+
active = getattr(tabs, "active", None)
|
|
1041
|
+
active_id = getattr(active, "id", None) or str(active or "")
|
|
1042
|
+
if "sequences" in str(active_id):
|
|
1043
|
+
target_input_id = "filter-sequences"
|
|
1044
|
+
elif "commands" in str(active_id):
|
|
1045
|
+
target_input_id = "input-commands"
|
|
1046
|
+
except Exception:
|
|
1047
|
+
pass
|
|
1048
|
+
|
|
1049
|
+
# Final fallback: prefer output filter if panel visible, else summary, else devices
|
|
1050
|
+
if target_input_id is None:
|
|
1051
|
+
try:
|
|
1052
|
+
out_panel = self.query_one("#output-panel")
|
|
1053
|
+
out_hidden = "hidden" in (getattr(out_panel, "classes", []) or [])
|
|
1054
|
+
if not out_hidden:
|
|
1055
|
+
target_input_id = "filter-output"
|
|
1056
|
+
except Exception:
|
|
1057
|
+
pass
|
|
1058
|
+
if target_input_id is None:
|
|
1059
|
+
try:
|
|
1060
|
+
sum_panel = self.query_one("#summary-panel")
|
|
1061
|
+
sum_hidden = "hidden" in (getattr(sum_panel, "classes", []) or [])
|
|
1062
|
+
if not sum_hidden:
|
|
1063
|
+
target_input_id = "filter-summary"
|
|
1064
|
+
except Exception:
|
|
1065
|
+
pass
|
|
1066
|
+
if target_input_id is None:
|
|
1067
|
+
target_input_id = "filter-devices"
|
|
1068
|
+
|
|
1069
|
+
self._focus_input_by_id(target_input_id)
|
|
1070
|
+
|
|
1071
|
+
def _focus_input_by_id(self, element_id: str) -> None:
|
|
1072
|
+
try:
|
|
1073
|
+
w = self.query_one(f"#{element_id}")
|
|
1074
|
+
except Exception:
|
|
1075
|
+
return
|
|
1076
|
+
# Try widget's focus method first
|
|
1077
|
+
try:
|
|
1078
|
+
if hasattr(w, "focus"):
|
|
1079
|
+
w.focus()
|
|
1080
|
+
return
|
|
1081
|
+
except Exception:
|
|
1082
|
+
pass
|
|
1083
|
+
# Fallback to focusing via app
|
|
1084
|
+
try:
|
|
1085
|
+
if hasattr(self, "set_focus"):
|
|
1086
|
+
self.set_focus(w)
|
|
1087
|
+
except Exception:
|
|
1088
|
+
pass
|
|
1089
|
+
|
|
1090
|
+
def action_toggle_theme(self) -> None:
|
|
1091
|
+
"""Toggle between light and dark theme, with compatibility fallbacks."""
|
|
1092
|
+
try:
|
|
1093
|
+
current_dark = bool(self.dark)
|
|
1094
|
+
except Exception:
|
|
1095
|
+
current_dark = getattr(self, "_dark_mode", True)
|
|
1096
|
+
new_dark = not current_dark
|
|
1097
|
+
self._dark_mode = new_dark
|
|
1098
|
+
self._apply_theme(new_dark)
|
|
1099
|
+
|
|
1100
|
+
def action_show_help(self) -> None:
|
|
1101
|
+
"""Display a streamlined help view in the Help panel."""
|
|
1102
|
+
# Build content for help
|
|
1103
|
+
try:
|
|
1104
|
+
from rich.text import Text
|
|
1105
|
+
|
|
1106
|
+
rich_text: Any = Text()
|
|
1107
|
+
rich_text.append("Help", style="bold")
|
|
1108
|
+
rich_text.append("\nPress Esc to close this help.")
|
|
1109
|
+
rich_text.append("\n\nKeys and actions:")
|
|
1110
|
+
for kb in KEYMAP:
|
|
1111
|
+
key_label = kb.key_display or kb.key
|
|
1112
|
+
rich_text.append("\n • ")
|
|
1113
|
+
rich_text.append(f"{key_label} — ")
|
|
1114
|
+
rich_text.append(kb.description)
|
|
1115
|
+
content: Any = rich_text
|
|
1116
|
+
except Exception:
|
|
1117
|
+
lines = [
|
|
1118
|
+
"Help",
|
|
1119
|
+
"Press Esc to close this help.",
|
|
1120
|
+
"",
|
|
1121
|
+
"Keys and actions:",
|
|
1122
|
+
]
|
|
1123
|
+
for kb in KEYMAP:
|
|
1124
|
+
key_label = kb.key_display or kb.key
|
|
1125
|
+
lines.append(f" • {key_label} — {kb.description}")
|
|
1126
|
+
content = "\n".join(lines)
|
|
1127
|
+
|
|
1128
|
+
# Update help widget
|
|
1129
|
+
try:
|
|
1130
|
+
help_log = self.query_one("#help-log")
|
|
1131
|
+
if hasattr(help_log, "clear"):
|
|
1132
|
+
try:
|
|
1133
|
+
help_log.clear()
|
|
1134
|
+
except Exception:
|
|
1135
|
+
pass
|
|
1136
|
+
if hasattr(help_log, "update"):
|
|
1137
|
+
help_log.update(content)
|
|
1138
|
+
elif hasattr(help_log, "write"):
|
|
1139
|
+
text = str(content)
|
|
1140
|
+
for line in text.splitlines():
|
|
1141
|
+
help_log.write(line)
|
|
1142
|
+
except Exception:
|
|
1143
|
+
pass
|
|
1144
|
+
|
|
1145
|
+
# Show help panel, hide summary to avoid confusion
|
|
1146
|
+
try:
|
|
1147
|
+
self._hide_summary_panel()
|
|
1148
|
+
except Exception:
|
|
1149
|
+
pass
|
|
1150
|
+
try:
|
|
1151
|
+
self._show_help_panel()
|
|
1152
|
+
except Exception:
|
|
1153
|
+
pass
|
|
1154
|
+
try:
|
|
1155
|
+
status_msg = (
|
|
1156
|
+
"Status: idle — Help (press h to refresh, s/o to toggle panels)"
|
|
1157
|
+
)
|
|
1158
|
+
self.query_one("#run-status").update(status_msg)
|
|
1159
|
+
self._refresh_bottom_visibility()
|
|
1160
|
+
except Exception:
|
|
1161
|
+
pass
|
|
1162
|
+
|
|
1163
|
+
def _apply_theme(self, dark: bool) -> None:
|
|
1164
|
+
# Preferred: property 'dark' on App
|
|
1165
|
+
try:
|
|
1166
|
+
if hasattr(self, "dark"):
|
|
1167
|
+
self.dark = dark
|
|
1168
|
+
return
|
|
1169
|
+
except Exception as exc:
|
|
1170
|
+
logging.debug(f"Setting App.dark failed: {exc}")
|
|
1171
|
+
# Legacy: set_theme("dark"|"light")
|
|
1172
|
+
try:
|
|
1173
|
+
if hasattr(self, "set_theme"):
|
|
1174
|
+
theme_name = "dark" if dark else "light"
|
|
1175
|
+
self.set_theme(theme_name)
|
|
1176
|
+
# Continue to refresh UI below
|
|
1177
|
+
except Exception as exc:
|
|
1178
|
+
logging.debug(f"set_theme failed: {exc}")
|
|
1179
|
+
# Fallback: use built-in action if available
|
|
1180
|
+
try:
|
|
1181
|
+
# If App has an action to toggle, ensure it matches desired state
|
|
1182
|
+
has_dark_attr = hasattr(self, "dark")
|
|
1183
|
+
current = bool(self.dark) if has_dark_attr else None
|
|
1184
|
+
if hasattr(self, "action_toggle_dark"):
|
|
1185
|
+
if current is None or current != dark:
|
|
1186
|
+
self.action_toggle_dark()
|
|
1187
|
+
except Exception as exc:
|
|
1188
|
+
logging.debug(f"action_toggle_dark failed: {exc}")
|
|
1189
|
+
# Refresh CSS/Screen to reflect theme changes
|
|
1190
|
+
for method in ("refresh_css", "reload_css"):
|
|
1191
|
+
try:
|
|
1192
|
+
if hasattr(self, method):
|
|
1193
|
+
getattr(self, method)()
|
|
1194
|
+
break
|
|
1195
|
+
except Exception as exc:
|
|
1196
|
+
logging.debug(f"{method} failed: {exc}")
|
|
1197
|
+
try:
|
|
1198
|
+
if hasattr(self, "refresh"):
|
|
1199
|
+
self.refresh()
|
|
1200
|
+
except Exception:
|
|
1201
|
+
pass
|
|
1202
|
+
|
|
1203
|
+
def action_toggle_help(self) -> None:
|
|
1204
|
+
"""Toggle the visibility of the Help panel.
|
|
1205
|
+
|
|
1206
|
+
If the Help panel is hidden, render and show it; if visible, hide it.
|
|
1207
|
+
"""
|
|
1208
|
+
try:
|
|
1209
|
+
panel = self.query_one("#help-panel")
|
|
1210
|
+
classes: set[str] = set(getattr(panel, "classes", []) or [])
|
|
1211
|
+
is_hidden = "hidden" in classes
|
|
1212
|
+
except Exception:
|
|
1213
|
+
is_hidden = True
|
|
1214
|
+
if is_hidden:
|
|
1215
|
+
self.action_show_help()
|
|
1216
|
+
else:
|
|
1217
|
+
self._hide_help_panel()
|
|
1218
|
+
try:
|
|
1219
|
+
self.query_one("#run-status").update("Status: idle")
|
|
1220
|
+
self._refresh_bottom_visibility()
|
|
1221
|
+
except Exception:
|
|
1222
|
+
pass
|
|
1223
|
+
|
|
1224
|
+
def action_close_overlays(self) -> None:
|
|
1225
|
+
"""Close any open bottom overlays (Help, Summary, Output)."""
|
|
1226
|
+
try:
|
|
1227
|
+
self._hide_help_panel()
|
|
1228
|
+
except Exception:
|
|
1229
|
+
pass
|
|
1230
|
+
try:
|
|
1231
|
+
self._hide_summary_panel()
|
|
1232
|
+
except Exception:
|
|
1233
|
+
pass
|
|
1234
|
+
try:
|
|
1235
|
+
self._hide_output_panel()
|
|
1236
|
+
except Exception:
|
|
1237
|
+
pass
|
|
1238
|
+
try:
|
|
1239
|
+
self.query_one("#run-status").update("Status: idle")
|
|
1240
|
+
self._refresh_bottom_visibility()
|
|
1241
|
+
except Exception:
|
|
1242
|
+
pass
|
|
1243
|
+
# Fallback: no-op; CSS uses theme vars so best effort only
|
|
1244
|
+
|
|
1245
|
+
# No modal emulation; cancellation uses toast prompts only
|
|
1246
|
+
|
|
1247
|
+
# Dynamic action guards to reflect availability in Footer and prevent invalid runs
|
|
1248
|
+
def check_action(
|
|
1249
|
+
self, action: str, _parameters: tuple[object, ...]
|
|
1250
|
+
) -> bool | None:
|
|
1251
|
+
try:
|
|
1252
|
+
running = bool(getattr(self, "_run_active", False))
|
|
1253
|
+
except Exception:
|
|
1254
|
+
running = False
|
|
1255
|
+
if action == "confirm" and running:
|
|
1256
|
+
# Allow confirm to surface the cancel prompt while a run is active
|
|
1257
|
+
return True
|
|
1258
|
+
if action == "cancel" and not running:
|
|
1259
|
+
# Disable cancel when idle
|
|
1260
|
+
return None
|
|
1261
|
+
return True
|
|
1262
|
+
|
|
1263
|
+
# Ensure Ctrl+C default (help_quit) triggers cancel rather than quitting
|
|
1264
|
+
async def action_help_quit(self) -> None:
|
|
1265
|
+
try:
|
|
1266
|
+
await self.action_cancel()
|
|
1267
|
+
except Exception:
|
|
1268
|
+
pass
|
|
1269
|
+
|
|
1270
|
+
# Helpers are provided by network_toolkit.tui.helpers
|
|
1271
|
+
|
|
1272
|
+
# Note: We rely on DeviceSession raising NetworkToolkitError for failures
|
|
1273
|
+
|
|
1274
|
+
# Launch the app
|
|
1275
|
+
_App().run()
|