forked from cylc/cylc-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.py
More file actions
588 lines (512 loc) · 16.3 KB
/
overlay.py
File metadata and controls
588 lines (512 loc) · 16.3 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python3
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# 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/>.
"""Overlay panels for Tui.
Panels are functions::
function(app) -> (widget, overlay_options)
Parameters:
app:
Tui application object.
widget (urwid.Widget):
A widget to place in the overlay.
overlay_options (dict):
A dictionary of keyword arguments to provide to the
urwid.Overlay constructor.
You will likely want to override the `width` and `height`
arguments.
See the `urwid` documentation for details.
"""
from functools import partial
import os
import re
import shlex
import stat
from subprocess import Popen
import sys
import tempfile
from time import sleep
import urwid
from cylc.flow.id import Tokens
from cylc.flow.task_state import (
TASK_STATUSES_ORDERED,
TASK_STATUS_WAITING
)
from cylc.flow.tui import (
JOB_COLOURS,
JOB_ICON,
TUI
)
from cylc.flow.tui.data import (
list_mutations,
mutate,
)
from cylc.flow.tui.util import (
ListBoxPlus,
format_flow_nums,
get_task_icon,
get_text_dimensions,
)
def _get_display_id(id_):
"""Return an ID for display in context menus.
* Display the full ID for users/workflows
* Display the relative ID for everything else
"""
tokens = Tokens(id_)
if tokens.is_task_like:
# if it's a cycle/task/job, then use the relative id
return tokens.relative_id
else:
# otherwise use the full id
return tokens.id
def _toggle_filter(app, filter_group, status, *_):
"""Toggle a filter state."""
app.filters[filter_group][status] = not app.filters[filter_group][status]
app.updater.update_filters(app.filters)
def _invert_filter(checkboxes, *_):
"""Invert the state of all filters."""
for checkbox in checkboxes:
checkbox.set_state(not checkbox.state)
def filter_workflow_state(app):
"""Return a widget for adjusting the workflow filter options."""
checkboxes = [
urwid.CheckBox(
[status],
state=is_on,
on_state_change=partial(_toggle_filter, app, 'workflows', status)
)
for status, is_on in app.filters['workflows'].items()
if status != 'id'
]
workflow_id_prompt = 'id (regex)'
def update_id_filter(widget, value):
nonlocal app
try:
# ensure the filter is value before updating the filter
re.compile(value)
except re.error:
# error in the regex -> inform the user
widget.set_caption(f'{workflow_id_prompt} - error: \n')
else:
# valid regex -> update the filter
widget.set_caption(f'{workflow_id_prompt}: \n')
app.filters['workflows']['id'] = value
app.updater.update_filters(app.filters)
id_filter_widget = urwid.Edit(
caption=f'{workflow_id_prompt}: \n',
edit_text=app.filters['workflows']['id'],
)
urwid.connect_signal(id_filter_widget, 'change', update_id_filter)
widget = urwid.ListBox(
urwid.SimpleFocusListWalker([
urwid.Text('Filter Workflow States'),
urwid.Divider(),
urwid.Padding(
urwid.Button(
'Invert',
on_press=partial(_invert_filter, checkboxes)
),
right=19
)
] + checkboxes + [
urwid.Divider(),
id_filter_widget,
])
)
return (
widget,
{'width': 35, 'height': 23}
)
def filter_task_state(app):
"""Return a widget for adjusting the task state filter."""
checkboxes = [
urwid.CheckBox(
get_task_icon(state, colour='overlay')
+ [' ' + state],
state=is_on,
on_state_change=partial(_toggle_filter, app, 'tasks', state)
)
for state, is_on in app.filters['tasks'].items()
]
widget = urwid.ListBox(
urwid.SimpleFocusListWalker([
urwid.Text('Filter Task States'),
urwid.Divider(),
urwid.Padding(
urwid.Button(
'Invert',
on_press=partial(_invert_filter, checkboxes)
),
right=19
)
] + checkboxes)
)
return (
widget,
{'width': 35, 'height': 23}
)
def help_info(app):
"""Return a widget displaying help information."""
# system title
items = [
urwid.Text(r'''
_ _ _
| | | | (_)
___ _ _| | ___ | |_ _ _ _
/ __| | | | |/ __| | __| | | | |
| (__| |_| | | (__ | |_| |_| | |
\___|\__, |_|\___| \__|\__,_|_|
__/ |
|___/
( scroll using arrow keys )
'''),
urwid.Text(TUI)
]
# list key bindings
for group, bindings in app.bindings.list_groups():
items.append(
urwid.Text([
f'{group["desc"]}:'
])
)
for binding in bindings:
keystr = ' '.join(binding['keys'])
items.append(
urwid.Text([
('key', keystr),
(' ' * (10 - len(keystr))),
binding['desc']
])
)
items.append(
urwid.Divider()
)
# mouse interaction
items.extend([
urwid.Text(
'fn + ⌥ & click to select text' if sys.platform == 'darwin' else
'Shift & click to select text'
),
urwid.Divider()
])
# list task states
items.append(urwid.Divider())
items.append(urwid.Text('Task Icons:'))
for state in TASK_STATUSES_ORDERED:
items.append(
urwid.Text(
get_task_icon(state, colour='overlay')
+ [' ', state]
)
)
items.append(urwid.Divider())
items.append(urwid.Text('Special States:'))
items.append(
urwid.Text(
get_task_icon(TASK_STATUS_WAITING, is_held=True, colour='overlay')
+ [' ', 'held']
)
)
items.append(
urwid.Text(
get_task_icon(
TASK_STATUS_WAITING,
is_queued=True,
colour='overlay',
)
+ [' ', 'queued']
)
)
items.append(
urwid.Text(
get_task_icon(
TASK_STATUS_WAITING,
is_runahead=True,
colour='overlay',
)
+ [' ', 'runahead']
)
)
# list job states
items.append(urwid.Divider())
items.append(urwid.Text('Job Icons:'))
for state in JOB_COLOURS:
items.append(
urwid.Text(
[
(f'overlay_job_{state}', JOB_ICON),
' ',
state
]
)
)
widget = urwid.ListBox(
urwid.SimpleFocusListWalker(items)
)
return (
widget,
{'width': 60, 'height': 40}
)
def context(app):
"""An overlay for context menus."""
value = app.tree_walker.get_focus()[0].get_node().get_value()
selection = [value['id_']] # single selection ATM
is_running = True
if (
value['type_'] == 'workflow'
and value['data']['status'] not in {'running', 'paused'}
):
# this is a stopped workflow
# => don't display mutations only valid for a running workflow
is_running = False
def _mutate(mutation, _):
nonlocal app, selection
app.open_overlay(partial(progress, text='Running Command'))
overlay_fcn = None
try:
overlay_fcn = mutate(mutation, selection)
except Exception as exc:
app.open_overlay(partial(error, text=str(exc)))
else:
app.close_topmost()
app.close_topmost()
if overlay_fcn:
app.open_overlay(overlay_fcn)
# determine the ID to display for the context menu
display_id = _get_display_id(value['id_'])
header = [f'id: {display_id}']
attrs = []
# workflow state info
if value['data'].get('status'):
attrs.append(value['data']['status'])
# task state info
if value['data'].get('state'):
attrs.append(
value['data']['state']
+ (
' (held)' if value['data'].get('isHeld')
else ' (queued)' if value['data'].get('isQueued')
else '(runahead)' if value['data'].get('isRunahead')
else ''
)
)
# task flow info
if value['data'].get('flowNums', '[1]') != '[1]':
attrs.append(f'flows={format_flow_nums(value["data"]["flowNums"])}')
if attrs:
header.append(', '.join(attrs))
widget = urwid.ListBox(
urwid.SimpleFocusListWalker(
[
urwid.Text('\n'.join(header)),
urwid.Divider(),
urwid.Text('Action'),
urwid.Button(
'(cancel)',
on_press=lambda *_: app.close_topmost()
),
urwid.Divider()
] + [
urwid.Button(
mutation,
on_press=partial(_mutate, mutation)
)
for mutation in list_mutations(
selection,
is_running,
)
]
)
)
return (
widget,
{'width': 50, 'height': 20}
)
def error(app, text=''):
"""An overlay for unexpected errors."""
return (
urwid.ListBox([
urwid.Text('Error'),
urwid.Divider(),
urwid.Text(text),
]),
{'width': 50, 'height': 40}
)
def progress(app, text='Working'):
"""An overlay for presenting a running action."""
return (
urwid.ListBox([
urwid.Text(text),
]),
{'width': 30, 'height': 10}
)
def log(app, id_=None, list_files=None, get_log=None):
"""An overlay for displaying log files."""
# display the host name where the file is coming from
host_widget = urwid.Text('loading...')
# display the log filepath
file_widget = urwid.Text('')
# display the actual log file itself
text_widget = urwid.Text('')
def open_menu(*_args, **_kwargs):
"""Open an overlay for selecting a log file."""
nonlocal app, id_
app.open_overlay(select_log)
def select_log(*_args, **_kwargs):
"""Create an overlay for selecting a log file."""
nonlocal list_files, id_
try:
files = list_files()
except Exception as exc:
return error(app, text=str(exc))
return (
urwid.ListBox([
*[
urwid.Text('Select File'),
urwid.Divider(),
],
*[
urwid.Button(
filename,
on_press=partial(
open_log,
filename=filename,
close=True,
),
)
for filename in files
],
]),
# NOTE: the "+6" avoids the need for scrolling
{'width': 40, 'height': len(files) + 6}
)
def open_log(*_, filename=None, close=False):
"""View the provided log file.
Args:
filename:
The name of the file to open (note name not path).
close:
If True, then the topmost overlay will be closed when a file is
selected. Use this to close the "select_log" overlay.
"""
nonlocal host_widget, file_widget, text_widget
try:
host, path, text = get_log(filename)
except Exception as exc:
host_widget.set_text(f'Error: {exc}')
file_widget.set_text('')
text_widget.set_text('')
else:
host_widget.set_text(f'Host: {host}')
file_widget.set_text(f'Path: {path}')
text_widget.set_text(text)
if close:
app.close_topmost()
def open_in_editor(*_, command):
"""Suspend Tui, open the file in an external utility, then restore Tui.
Args:
command:
The command to run as a list, e.g. 'gvim -f'.
This command must be blocking, the tui session will be
restored when the command exits.
"""
nonlocal text_widget
with tempfile.NamedTemporaryFile('w+') as temp_file:
# write the text into a temp file
temp_file.write(text_widget.text)
temp_file.seek(0, 0)
# make the file readonly to avoid confusion
os.chmod(temp_file.name, stat.S_IRUSR)
# suspend Tui
app.loop.screen.stop()
# open the file using the external tool (must be blocking)
print(
'Launching external tool, Tui will resume once it exits.',
file=sys.stderr,
)
try:
Popen(
[*shlex.split(command), temp_file.name]
).wait() # nosec B603
# (this is running a command the user has configured)
except OSError as exc:
# ensure any critical errors are visible to the user so
# that they have a chance to fix them
_sleep_time = 5
print(
(
f'Error running {command} {temp_file.name}'
f'\n{exc}'
f'\nTui will resume in {_sleep_time} seconds'
),
file=sys.stderr
)
sleep(_sleep_time)
# restore Tui
app.loop.screen.start()
# load the default log file
if id_:
# NOTE: the kwargs are not provided in the overlay unit tests
open_log()
return (
ListBoxPlus([
# NOTE: We use a ListBox here because it allows the file-select
# button to have focus whilst keeping the overlay scrollable at the
# same time.
host_widget,
file_widget,
urwid.Button(
'Select File',
on_press=open_menu,
),
urwid.Columns([
('pack', urwid.Text('Open in: ')),
*(
(
'pack',
urwid.Button(
command,
align='left',
on_press=partial(open_in_editor, command=command),
),
)
for command in [
opt for opt in (
os.environ.get('EDITOR', 'vim'),
os.environ.get('GEDITOR', 'gvim -f'),
os.environ.get('PAGER', 'less'),
)
]
),
]),
urwid.Text(
"(Configure apps to open logs via $EDITOR, $GEDITOR, $PAGER)"
),
urwid.Divider(),
text_widget,
]),
# open full screen
{'width': 9999, 'height': 9999}
)
def text_box(app, text=''):
"""A simple text box overlay."""
width, height = get_text_dimensions(text)
return (
urwid.ListBox([
urwid.Text(text),
]),
# NOTE: those fudge factors account for the overlay border & padding
{'width': width + 4, 'height': height + 6}
)