thzdaq-cli 0.0.6b3__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.
- thzdaq_cli/__init__.py +1 -0
- thzdaq_cli/__main__.py +5 -0
- thzdaq_cli/agents.py +259 -0
- thzdaq_cli/app.py +100 -0
- thzdaq_cli/approvals.py +150 -0
- thzdaq_cli/client.py +140 -0
- thzdaq_cli/daemon.py +266 -0
- thzdaq_cli/devices.py +262 -0
- thzdaq_cli/measurements.py +383 -0
- thzdaq_cli/scripts.py +167 -0
- thzdaq_cli/show.py +71 -0
- thzdaq_cli/station.py +72 -0
- thzdaq_cli/workspaces.py +177 -0
- thzdaq_cli-0.0.6b3.dist-info/METADATA +27 -0
- thzdaq_cli-0.0.6b3.dist-info/RECORD +18 -0
- thzdaq_cli-0.0.6b3.dist-info/WHEEL +4 -0
- thzdaq_cli-0.0.6b3.dist-info/entry_points.txt +2 -0
- thzdaq_cli-0.0.6b3.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
"""thzdaq procedures, describe, run, status, measurements, cancel, watch, plot:
|
|
2
|
+
the procedures of the bench and the measurements in its daemon. Parameters
|
|
3
|
+
are typed as describe shows them, in their display units: --set
|
|
4
|
+
voltage=0:7:351 is a sweep from 0 to 7 mV in 351 points. A run is followed
|
|
5
|
+
until it ends; Ctrl+C asks whether to cancel it, and without a terminal
|
|
6
|
+
leaves it running. plot opens the plots of a measurement in a window of
|
|
7
|
+
their own, apart from this terminal."""
|
|
8
|
+
|
|
9
|
+
import queue
|
|
10
|
+
import sys
|
|
11
|
+
from typing import Any, Callable, Dict, Iterator
|
|
12
|
+
|
|
13
|
+
from thzdaq_api import events, windows
|
|
14
|
+
from thzdaq_api.devices import ListDevices
|
|
15
|
+
from thzdaq_api.lab import Lab
|
|
16
|
+
from thzdaq_api.measurement_file import (
|
|
17
|
+
COMPLETED,
|
|
18
|
+
RUNNING,
|
|
19
|
+
Measurement,
|
|
20
|
+
read_measurement,
|
|
21
|
+
)
|
|
22
|
+
from thzdaq_api.measurements import (
|
|
23
|
+
AbortMeasurement,
|
|
24
|
+
CancelMeasurement,
|
|
25
|
+
DescribeProcedure,
|
|
26
|
+
ListProcedures,
|
|
27
|
+
ListRuns,
|
|
28
|
+
ProcedureInfo,
|
|
29
|
+
RunInfo,
|
|
30
|
+
StartMeasurement,
|
|
31
|
+
fill_slots,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
from thzdaq_cli import show
|
|
35
|
+
from thzdaq_cli.client import FAILED, HERE, OK, Client, Mistyped
|
|
36
|
+
from thzdaq_cli.devices import assignments
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def add(commands) -> None:
|
|
40
|
+
commands.add_parser("procedures", help="what the bench measures").set_defaults(
|
|
41
|
+
run=list_procedures
|
|
42
|
+
)
|
|
43
|
+
describe = commands.add_parser(
|
|
44
|
+
"describe", help="a procedure: parameters, devices, plots"
|
|
45
|
+
)
|
|
46
|
+
describe.add_argument("procedure")
|
|
47
|
+
given(describe)
|
|
48
|
+
describe.set_defaults(run=describe_procedure)
|
|
49
|
+
run = commands.add_parser("run", help="run a procedure and follow it")
|
|
50
|
+
run.add_argument("procedure")
|
|
51
|
+
given(run)
|
|
52
|
+
run.add_argument("--comment", default="")
|
|
53
|
+
run.add_argument(
|
|
54
|
+
"--share", action="store_true", help="also on devices others sweep"
|
|
55
|
+
)
|
|
56
|
+
run.add_argument("--workspace", help=f"its file goes to data/ there ({HERE})")
|
|
57
|
+
run.add_argument("--dry-run", action="store_true", help="check, start nothing")
|
|
58
|
+
run.add_argument("--detach", action="store_true", help="start it, do not follow")
|
|
59
|
+
run.set_defaults(run=run_procedure)
|
|
60
|
+
commands.add_parser("status", help="measurements running now").set_defaults(
|
|
61
|
+
run=running
|
|
62
|
+
)
|
|
63
|
+
commands.add_parser(
|
|
64
|
+
"measurements", help="measurements of the daemon, running and lately ended"
|
|
65
|
+
).set_defaults(run=list_runs)
|
|
66
|
+
cancel = commands.add_parser("cancel", help="stop a measurement (it restores)")
|
|
67
|
+
cancel.add_argument("id", type=int)
|
|
68
|
+
cancel.add_argument("--abort", action="store_true", help="break reads that hang")
|
|
69
|
+
cancel.set_defaults(run=cancel_run)
|
|
70
|
+
watch = commands.add_parser("watch", help="follow a measurement, or every event")
|
|
71
|
+
watch.add_argument("id", type=int, nargs="?")
|
|
72
|
+
watch.set_defaults(run=watch_events)
|
|
73
|
+
plot = commands.add_parser(
|
|
74
|
+
"plot", help="the plots of a measurement in a window of their own"
|
|
75
|
+
)
|
|
76
|
+
plot.add_argument(
|
|
77
|
+
"measurement", type=windows.which, metavar=windows.WHICH,
|
|
78
|
+
help=windows.WHICH_HELP,
|
|
79
|
+
) # fmt: skip
|
|
80
|
+
windows.add_options(plot)
|
|
81
|
+
plot.set_defaults(run=plot_measurement)
|
|
82
|
+
file = commands.add_parser("show", help="what a measurement file holds")
|
|
83
|
+
file.add_argument("path")
|
|
84
|
+
file.set_defaults(run=show_file)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def given(command) -> None:
|
|
88
|
+
command.add_argument(
|
|
89
|
+
"--set", action="append", default=[], metavar="NAME=VALUE",
|
|
90
|
+
help="a parameter as describe shows it (display units; a sweep start:stop:points)",
|
|
91
|
+
) # fmt: skip
|
|
92
|
+
command.add_argument(
|
|
93
|
+
"--device", action="append", default=[], metavar="SLOT=DEVICE",
|
|
94
|
+
help="the device of a slot (default: the device named as the slot)",
|
|
95
|
+
) # fmt: skip
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def broken(error: str) -> str:
|
|
99
|
+
"""Why a procedure cannot run, in a line: the last of its error."""
|
|
100
|
+
lines = error.strip().splitlines()
|
|
101
|
+
return f"not described: {lines[-1]}" if lines else ""
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def list_procedures(args, client: Client) -> int:
|
|
105
|
+
found = client.call(ListProcedures())
|
|
106
|
+
client.out(
|
|
107
|
+
found,
|
|
108
|
+
lambda p: show.table(
|
|
109
|
+
("procedure", "title", ""), [(i.name, i.title, broken(i.error)) for i in p]
|
|
110
|
+
),
|
|
111
|
+
)
|
|
112
|
+
return OK
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def procedure_text(info: ProcedureInfo) -> str:
|
|
116
|
+
steps = f"{info.steps} steps" if info.steps else "no step count"
|
|
117
|
+
lines = [f"{info.title} ({info.name}): {steps}", info.description, ""]
|
|
118
|
+
lines.append(
|
|
119
|
+
show.table(
|
|
120
|
+
("parameter", "label", "default", "unit", "used when"),
|
|
121
|
+
[(p.name, p.label, p.typed(p.default), p.display_unit, p.used_when())
|
|
122
|
+
for p in info.parameters],
|
|
123
|
+
)
|
|
124
|
+
) # fmt: skip
|
|
125
|
+
slots = show.table(
|
|
126
|
+
("slot", "device of", "kind"),
|
|
127
|
+
[(s.name, " + ".join(s.capabilities) or "any", s.kind or "")
|
|
128
|
+
for s in info.slots],
|
|
129
|
+
) # fmt: skip
|
|
130
|
+
if slots:
|
|
131
|
+
lines += ["", slots]
|
|
132
|
+
if info.plots:
|
|
133
|
+
lines += ["", "plots: " + ", ".join(p.title for p in info.plots)]
|
|
134
|
+
return "\n".join(lines)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def describe_procedure(args, client: Client) -> int:
|
|
138
|
+
info = client.call(
|
|
139
|
+
DescribeProcedure(
|
|
140
|
+
procedure=args.procedure,
|
|
141
|
+
typed=assignments(args.set),
|
|
142
|
+
devices=assignments(args.device),
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
client.out(info, procedure_text)
|
|
146
|
+
return OK
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def run_text(run: RunInfo) -> str:
|
|
150
|
+
"""How a run stands, or how it ended."""
|
|
151
|
+
done = f"{run.done}/{run.total}" if run.total else f"{run.done}"
|
|
152
|
+
text = f"#{run.id} {run.title}: {run.status} ({done} steps)"
|
|
153
|
+
if run.error:
|
|
154
|
+
text += f"\n{run.error}"
|
|
155
|
+
if run.path:
|
|
156
|
+
text += f"\nfile {run.path}"
|
|
157
|
+
return text
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def chosen_devices(args, client: Client) -> Dict[str, str]:
|
|
161
|
+
"""The devices given, and for a slot without one the device that alone
|
|
162
|
+
suits it (said on stderr); several that suit are named for --device."""
|
|
163
|
+
typed, given = assignments(args.set), assignments(args.device)
|
|
164
|
+
info = client.call(DescribeProcedure(procedure=args.procedure, typed=typed))
|
|
165
|
+
devices, choices = fill_slots(info.slots, client.call(ListDevices()), given)
|
|
166
|
+
if choices:
|
|
167
|
+
slot, refs = next(iter(choices.items()))
|
|
168
|
+
raise Mistyped(f"--device {slot}=… one of {', '.join(refs)}")
|
|
169
|
+
for slot in devices.keys() - given.keys():
|
|
170
|
+
print(f"{slot}: {devices[slot]}", file=sys.stderr)
|
|
171
|
+
return devices
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def run_procedure(args, client: Client) -> int:
|
|
175
|
+
workspace = client.folder()
|
|
176
|
+
devices = chosen_devices(args, client)
|
|
177
|
+
|
|
178
|
+
def start(share: bool) -> RunInfo:
|
|
179
|
+
return client.call(
|
|
180
|
+
StartMeasurement(
|
|
181
|
+
procedure=args.procedure,
|
|
182
|
+
typed=assignments(args.set),
|
|
183
|
+
devices=devices,
|
|
184
|
+
comment=args.comment,
|
|
185
|
+
share=share or args.share,
|
|
186
|
+
workspace=workspace,
|
|
187
|
+
dry_run=args.dry_run,
|
|
188
|
+
)
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
if args.dry_run or args.detach:
|
|
192
|
+
planned = client.shared(start)
|
|
193
|
+
if planned is None:
|
|
194
|
+
return FAILED
|
|
195
|
+
client.out(planned, run_text)
|
|
196
|
+
return OK
|
|
197
|
+
heard = Heard(client.lab) # before the start: none of its events is missed
|
|
198
|
+
try:
|
|
199
|
+
started = client.shared(start)
|
|
200
|
+
if started is None:
|
|
201
|
+
return FAILED
|
|
202
|
+
if not client.json:
|
|
203
|
+
print(f"#{started.id} {started.title} started", file=sys.stderr)
|
|
204
|
+
return follow(client, heard, started.id)
|
|
205
|
+
finally:
|
|
206
|
+
heard.stop()
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class Heard:
|
|
210
|
+
"""Events of the lab: the stream's thread hands them over, a command takes
|
|
211
|
+
them in its own."""
|
|
212
|
+
|
|
213
|
+
def __init__(self, lab: Lab):
|
|
214
|
+
self.queue: "queue.Queue[events.Event]" = queue.Queue()
|
|
215
|
+
self.stop = lab.subscribe(self.queue.put)
|
|
216
|
+
|
|
217
|
+
def bodies(self) -> Iterator[Any]:
|
|
218
|
+
while True:
|
|
219
|
+
try:
|
|
220
|
+
yield self.queue.get(timeout=0.2).body
|
|
221
|
+
except queue.Empty:
|
|
222
|
+
continue # a moment to hear Ctrl+C
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def follow(client: Client, heard: Heard, run_id: int) -> int:
|
|
226
|
+
"""Until the measurement ends: its progress, its log, how it ended."""
|
|
227
|
+
while True:
|
|
228
|
+
try:
|
|
229
|
+
for body in heard.bodies():
|
|
230
|
+
if events.run_id(body) != run_id or isinstance(
|
|
231
|
+
body, events.MeasurementRecords
|
|
232
|
+
):
|
|
233
|
+
continue
|
|
234
|
+
if client.json:
|
|
235
|
+
print(show.as_json(body), flush=True)
|
|
236
|
+
elif isinstance(body, events.MeasurementProgress):
|
|
237
|
+
progress(body)
|
|
238
|
+
elif isinstance(body, events.MeasurementLog):
|
|
239
|
+
print(body.message)
|
|
240
|
+
if isinstance(body, events.MeasurementFinished):
|
|
241
|
+
if not client.json:
|
|
242
|
+
print(run_text(body.run))
|
|
243
|
+
return OK if body.run.status == COMPLETED else FAILED
|
|
244
|
+
except KeyboardInterrupt:
|
|
245
|
+
print(file=sys.stderr)
|
|
246
|
+
if not client.ask(f"Cancel #{run_id}?"):
|
|
247
|
+
print(f"#{run_id} goes on: thzdaq watch {run_id}", file=sys.stderr)
|
|
248
|
+
return OK
|
|
249
|
+
client.call(CancelMeasurement(id=run_id))
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def progress(body: events.MeasurementProgress) -> None:
|
|
253
|
+
"""On one line of a terminal; a log file gets no progress lines."""
|
|
254
|
+
if not sys.stderr.isatty():
|
|
255
|
+
return
|
|
256
|
+
total = (
|
|
257
|
+
f"/{body.total} ({round(100 * body.done / body.total)} %)" if body.total else ""
|
|
258
|
+
)
|
|
259
|
+
sys.stderr.write(f"\r#{body.id} {body.done}{total} ")
|
|
260
|
+
sys.stderr.flush()
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def running(args, client: Client) -> int:
|
|
264
|
+
found = [run for run in client.call(ListRuns()) if run.status == RUNNING]
|
|
265
|
+
client.out(found, lambda r: runs_table(r) or "no measurement runs")
|
|
266
|
+
return OK
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def runs_table(runs) -> str:
|
|
270
|
+
return show.table(
|
|
271
|
+
("id", "title", "status", "done", "origin", "file"),
|
|
272
|
+
[(r.id, r.title, r.status, f"{r.done}/{r.total or '?'}", r.origin, r.path or "")
|
|
273
|
+
for r in runs],
|
|
274
|
+
) # fmt: skip
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def list_runs(args, client: Client) -> int:
|
|
278
|
+
found = client.call(ListRuns())
|
|
279
|
+
client.out(found, lambda r: runs_table(r) or "no measurement")
|
|
280
|
+
return OK
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def cancel_run(args, client: Client) -> int:
|
|
284
|
+
operation = AbortMeasurement if args.abort else CancelMeasurement
|
|
285
|
+
client.out(client.call(operation(id=args.id)), run_text)
|
|
286
|
+
return OK
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
# One line for each kind of event when every event is watched
|
|
290
|
+
LINES: Dict[type, Callable[[Any], str]] = {
|
|
291
|
+
events.QuantitySet: lambda b: f"{b.ref} {b.key} = {show.value(b.value)} ({b.origin})",
|
|
292
|
+
events.ActionDone: lambda b: f"{b.ref} {b.action} {b.arguments or ''} ({b.origin})",
|
|
293
|
+
events.Values: lambda b: f"{b.ref} "
|
|
294
|
+
+ " ".join(f"{k}={show.value(v)}" for k, v in b.values.items()),
|
|
295
|
+
events.ReadFailed: lambda b: f"{b.ref} failed: {b.message}",
|
|
296
|
+
events.MeasurementStarted: lambda b: f"#{b.run.id} {b.run.title} started ({b.run.origin})",
|
|
297
|
+
events.MeasurementProgress: lambda b: f"#{b.id} {b.done}/{b.total or '?'}",
|
|
298
|
+
events.MeasurementLog: lambda b: f"#{b.id} {b.message}",
|
|
299
|
+
events.MeasurementPlotted: lambda b: f"#{b.id} plots {b.plot.title}",
|
|
300
|
+
events.MeasurementFinished: lambda b: run_text(b.run),
|
|
301
|
+
events.RawSent: lambda b: f"{b.instrument} {b.command!r}"
|
|
302
|
+
+ ("" if b.answer is None else f" -> {b.answer!r}")
|
|
303
|
+
+ f" ({b.origin})",
|
|
304
|
+
events.ScriptStarted: lambda b: f"{show.script(b.script)} ({b.script.by})",
|
|
305
|
+
events.ScriptOutput: lambda b: show.output(b.id, b.text),
|
|
306
|
+
events.ScriptEnded: lambda b: show.script(b.script),
|
|
307
|
+
events.ProceduresChanged: lambda b: f"procedures: {len(b.procedures)}",
|
|
308
|
+
events.StationChanged: lambda b: f"profile {show.profile(b.name, b.path)}",
|
|
309
|
+
events.LeasesChanged: lambda b: "in use: "
|
|
310
|
+
+ (", ".join(f"{l.device} {l.mode} by #{l.run}" for l in b.leases) or "nothing"),
|
|
311
|
+
events.ApprovalRequested: lambda b: show.approval(b.approval),
|
|
312
|
+
events.ApprovalAnswered: lambda b: show.answer(b.id, b.answer),
|
|
313
|
+
events.UiRequested: lambda b: f"window {b.window}: {b.operation} "
|
|
314
|
+
+ " ".join(f"{k}={v}" for k, v in b.arguments.items() if v not in (None, {}, ""))
|
|
315
|
+
+ f" ({b.origin})",
|
|
316
|
+
events.Resync: lambda b: "events were lost (the daemon restarted)",
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def watch_events(args, client: Client) -> int:
|
|
321
|
+
heard = Heard(client.lab)
|
|
322
|
+
try:
|
|
323
|
+
if args.id is not None:
|
|
324
|
+
found = next((r for r in client.call(ListRuns()) if r.id == args.id), None)
|
|
325
|
+
if found is None or found.status != RUNNING:
|
|
326
|
+
client.out(
|
|
327
|
+
found, lambda r: run_text(r) if r else f"no measurement #{args.id}"
|
|
328
|
+
)
|
|
329
|
+
return OK if found is not None else FAILED
|
|
330
|
+
return follow(client, heard, args.id)
|
|
331
|
+
try:
|
|
332
|
+
for body in heard.bodies():
|
|
333
|
+
if isinstance(body, events.MeasurementRecords):
|
|
334
|
+
continue
|
|
335
|
+
print(
|
|
336
|
+
show.as_json(body) if client.json else LINES[type(body)](body),
|
|
337
|
+
flush=True,
|
|
338
|
+
)
|
|
339
|
+
except KeyboardInterrupt:
|
|
340
|
+
return OK
|
|
341
|
+
finally:
|
|
342
|
+
heard.stop()
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def plot_measurement(args, client: Client) -> int:
|
|
346
|
+
which = args.measurement
|
|
347
|
+
command = windows.open_plots(
|
|
348
|
+
client.call, which, client.simulate, windows.OwnPlot.of(args)
|
|
349
|
+
)
|
|
350
|
+
shown = f"#{which}" if isinstance(which, int) else which
|
|
351
|
+
client.out(
|
|
352
|
+
{"measurement": which, "command": command},
|
|
353
|
+
lambda told: f"the plots of {shown} open in a window",
|
|
354
|
+
)
|
|
355
|
+
return OK
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def file_text(measurement: Measurement) -> str:
|
|
359
|
+
lines = [
|
|
360
|
+
f"#{measurement.id} {measurement.title or measurement.procedure}: "
|
|
361
|
+
f"{measurement.status}",
|
|
362
|
+
f"{measurement.started} .. {measurement.finished or ''}".rstrip(" ."),
|
|
363
|
+
]
|
|
364
|
+
if measurement.comment:
|
|
365
|
+
lines.append(measurement.comment)
|
|
366
|
+
if measurement.error:
|
|
367
|
+
lines.append(measurement.error)
|
|
368
|
+
lines.append(
|
|
369
|
+
show.table(
|
|
370
|
+
("table", "records", "columns"),
|
|
371
|
+
[(name, len(measurement.records(name)), ", ".join(table.data))
|
|
372
|
+
for name, table in measurement.tables.items()],
|
|
373
|
+
)
|
|
374
|
+
or "no records"
|
|
375
|
+
) # fmt: skip
|
|
376
|
+
return "\n".join(lines)
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def show_file(args, client: Client) -> int:
|
|
380
|
+
"""No daemon needed: the file is read here."""
|
|
381
|
+
found = read_measurement(args.path)
|
|
382
|
+
client.out(found.to_dict() if client.json else found, file_text)
|
|
383
|
+
return OK
|
thzdaq_cli/scripts.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""thzdaq script start|list|logs|stop, thzdaq run-script: scripts the daemon
|
|
2
|
+
runs apart from any terminal, so closing this one leaves them running; their
|
|
3
|
+
output goes to data/scripts/<id>.log of their workspace. A script runs with
|
|
4
|
+
--python, else with the Python its workspace names (thzdaq.toml), else with
|
|
5
|
+
this command's (a build's thzdaq-cli.exe run-script); what
|
|
6
|
+
it may do without asking a person is given with --allow (set:<device>,
|
|
7
|
+
call:<device>, run:<procedure>, raw:<instrument>; an agent's allows
|
|
8
|
+
nothing: a script has the rights of whoever started it). stop cancels its
|
|
9
|
+
measurements, and ends it when it has not ended a few seconds later."""
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import runpy
|
|
13
|
+
import sys
|
|
14
|
+
import time
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from typing import List, Optional, Tuple
|
|
17
|
+
|
|
18
|
+
from thzdaq_api import host, interpreters
|
|
19
|
+
from thzdaq_api.scripts import (
|
|
20
|
+
Grant,
|
|
21
|
+
ListScripts,
|
|
22
|
+
ReadScriptLog,
|
|
23
|
+
ScriptInfo,
|
|
24
|
+
StartScript,
|
|
25
|
+
StopScript,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
from thzdaq_cli import show
|
|
29
|
+
from thzdaq_cli.client import HERE, OK, Client, Mistyped
|
|
30
|
+
|
|
31
|
+
FOLLOW_S = 0.5 # how often logs -f looks for more output, as tail -f does
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def add(commands) -> None:
|
|
35
|
+
group = commands.add_parser(
|
|
36
|
+
"script", help="scripts the daemon runs apart from this terminal"
|
|
37
|
+
)
|
|
38
|
+
actions = group.add_subparsers(dest="action", metavar="ACTION", required=True)
|
|
39
|
+
start = actions.add_parser(
|
|
40
|
+
"start", help="run a Python script in the daemon: it outlives this terminal"
|
|
41
|
+
)
|
|
42
|
+
start.add_argument("file", help="then -- and the script's arguments")
|
|
43
|
+
start.add_argument(
|
|
44
|
+
"--python",
|
|
45
|
+
help="an environment's folder or its interpreter (default: the "
|
|
46
|
+
"workspace's, else this command's)",
|
|
47
|
+
)
|
|
48
|
+
start.add_argument(
|
|
49
|
+
"--allow", action="append", default=[], metavar="KIND:TARGET",
|
|
50
|
+
help="done without asking: set:<device>, call:<device>, run:<procedure>,"
|
|
51
|
+
" raw:<instrument> (raw writes, which no limit checks); a person's only",
|
|
52
|
+
) # fmt: skip
|
|
53
|
+
start.add_argument(
|
|
54
|
+
"--share", action="append", default=[], metavar="DEVICE",
|
|
55
|
+
help="its measurements share it while another one sweeps it",
|
|
56
|
+
) # fmt: skip
|
|
57
|
+
start.add_argument(
|
|
58
|
+
"--workspace", help=f"its working folder, its log in data/scripts ({HERE})"
|
|
59
|
+
)
|
|
60
|
+
start.set_defaults(run=start_script)
|
|
61
|
+
actions.add_parser("list", help="scripts running and lately ended").set_defaults(
|
|
62
|
+
run=list_scripts
|
|
63
|
+
)
|
|
64
|
+
logs = actions.add_parser("logs", help="what a script printed")
|
|
65
|
+
logs.add_argument("id", type=int)
|
|
66
|
+
logs.add_argument("-f", "--follow", action="store_true", help="until it ends")
|
|
67
|
+
logs.set_defaults(run=script_logs)
|
|
68
|
+
stop = actions.add_parser(
|
|
69
|
+
"stop", help="cancel its measurements, then end it if it goes on"
|
|
70
|
+
)
|
|
71
|
+
stop.add_argument("id", type=int)
|
|
72
|
+
stop.set_defaults(run=stop_script)
|
|
73
|
+
run = commands.add_parser(
|
|
74
|
+
host.RUN_SCRIPT, help="run a Python file with this program's Python"
|
|
75
|
+
)
|
|
76
|
+
ran = run.add_mutually_exclusive_group(required=True)
|
|
77
|
+
ran.add_argument("file", nargs="?", help="then the script's arguments, as python")
|
|
78
|
+
ran.add_argument(
|
|
79
|
+
host.MODULE, dest="module", help="a module as a program (python -m), then --"
|
|
80
|
+
)
|
|
81
|
+
# Everything after the file is the script's, as python takes it: the daemon
|
|
82
|
+
# starts a script as <its python> <file> <arguments>
|
|
83
|
+
run.add_argument("arguments", nargs=argparse.REMAINDER, help=argparse.SUPPRESS)
|
|
84
|
+
run.set_defaults(run=run_script)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def interpreter(given: str, workspace: Optional[Path]) -> Tuple[str, ...]:
|
|
88
|
+
"""How the daemon runs a file: --python, else the Python of its
|
|
89
|
+
workspace."""
|
|
90
|
+
if given:
|
|
91
|
+
return (str(interpreters.python_in(interpreters.located(given))),)
|
|
92
|
+
return interpreters.scripts_python(workspace)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def grants(given: List[str]) -> Tuple[Grant, ...]:
|
|
96
|
+
try:
|
|
97
|
+
return tuple(Grant.parse(text) for text in given)
|
|
98
|
+
except ValueError as e:
|
|
99
|
+
raise Mistyped(str(e)) from None
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def script_text(info: ScriptInfo) -> str:
|
|
103
|
+
lines = [show.script(info), f"log {info.log}"]
|
|
104
|
+
if info.allow:
|
|
105
|
+
lines.append("may " + ", ".join(str(grant) for grant in info.allow))
|
|
106
|
+
return "\n".join(lines)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def start_script(args, client: Client) -> int:
|
|
110
|
+
started = client.call(
|
|
111
|
+
StartScript(
|
|
112
|
+
path=str(Path(args.file).resolve()),
|
|
113
|
+
python=interpreter(args.python, client.workspace),
|
|
114
|
+
arguments=tuple(args.passed),
|
|
115
|
+
workspace=client.folder(),
|
|
116
|
+
allow=grants(args.allow),
|
|
117
|
+
share=tuple(args.share),
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
client.out(started, script_text)
|
|
121
|
+
return OK
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def list_scripts(args, client: Client) -> int:
|
|
125
|
+
found = client.call(ListScripts())
|
|
126
|
+
client.out(
|
|
127
|
+
found,
|
|
128
|
+
lambda s: show.table(
|
|
129
|
+
("id", "script", "status", "pid", "started", "log"),
|
|
130
|
+
[(i.id, i.name, i.status, i.pid or "", i.started, i.log) for i in s],
|
|
131
|
+
)
|
|
132
|
+
or "no script",
|
|
133
|
+
)
|
|
134
|
+
return OK
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def script_logs(args, client: Client) -> int:
|
|
138
|
+
since = 0
|
|
139
|
+
while True:
|
|
140
|
+
part = client.call(ReadScriptLog(id=args.id, since=since))
|
|
141
|
+
sys.stdout.write(part.text)
|
|
142
|
+
sys.stdout.flush()
|
|
143
|
+
since = part.next
|
|
144
|
+
if part.text:
|
|
145
|
+
continue # more may wait already
|
|
146
|
+
if not (args.follow and part.running):
|
|
147
|
+
return OK
|
|
148
|
+
time.sleep(FOLLOW_S)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def stop_script(args, client: Client) -> int:
|
|
152
|
+
client.out(client.call(StopScript(id=args.id)), script_text)
|
|
153
|
+
return OK
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def run_script(args, client: Client) -> int:
|
|
157
|
+
"""The file as Python runs it: __main__, its folder first on the path; a
|
|
158
|
+
module as python -m runs it (the kernel of a window's console)."""
|
|
159
|
+
if args.module:
|
|
160
|
+
sys.argv = [args.module, *args.passed]
|
|
161
|
+
runpy.run_module(args.module, run_name="__main__", alter_sys=True)
|
|
162
|
+
return OK
|
|
163
|
+
path = Path(args.file).resolve()
|
|
164
|
+
sys.argv = [str(path), *args.arguments, *args.passed]
|
|
165
|
+
sys.path.insert(0, str(path.parent))
|
|
166
|
+
runpy.run_path(str(path), run_name="__main__")
|
|
167
|
+
return OK
|
thzdaq_cli/show.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""How answers are printed: aligned text for people, JSON for scripts."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any, Iterable, Optional, Sequence
|
|
5
|
+
|
|
6
|
+
import msgspec
|
|
7
|
+
from thzdaq_api.approvals import Answer, Approval
|
|
8
|
+
from thzdaq_api.scripts import RUNNING, ScriptInfo
|
|
9
|
+
|
|
10
|
+
NO_FILE = "no file" # a profile, or a run, not kept in a file
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def profile(name: str, path: Optional[str]) -> str:
|
|
14
|
+
return f"{name} ({path or NO_FILE})"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def as_json(value: Any) -> str:
|
|
18
|
+
# JSON escapes preserve Unicode even through a Windows legacy code page.
|
|
19
|
+
return json.dumps(json.loads(msgspec.json.encode(value)), ensure_ascii=True)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def table(headers: Sequence[str], rows: Iterable[Sequence[Any]]) -> str:
|
|
23
|
+
"""Columns as wide as their widest text; empty when there are no rows."""
|
|
24
|
+
rows = [[f"{cell}" for cell in row] for row in rows]
|
|
25
|
+
if not rows:
|
|
26
|
+
return ""
|
|
27
|
+
widths = [max(len(text) for text in column) for column in zip(headers, *rows)]
|
|
28
|
+
lines = [headers, *rows]
|
|
29
|
+
return "\n".join(
|
|
30
|
+
" ".join(text.ljust(width) for text, width in zip(line, widths)).rstrip()
|
|
31
|
+
for line in lines
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def value(found: Any, unit: str = "") -> str:
|
|
36
|
+
"""A value as the daemon gives it (SI, power in dBm), with its unit."""
|
|
37
|
+
if found is None:
|
|
38
|
+
return "--"
|
|
39
|
+
if isinstance(found, bool):
|
|
40
|
+
return "on" if found else "off"
|
|
41
|
+
if isinstance(found, float):
|
|
42
|
+
return f"{found:.6g} {unit}".strip()
|
|
43
|
+
return f"{found} {unit}".strip()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def approval(asked: Approval) -> str:
|
|
47
|
+
"""What waits for a person, as the window's dialog shows it."""
|
|
48
|
+
lines = [f"#{asked.id} {asked.origin}: {asked.title}", asked.text]
|
|
49
|
+
if asked.warning:
|
|
50
|
+
lines.append(f"warning: {asked.warning}")
|
|
51
|
+
if asked.options:
|
|
52
|
+
lines.append(f"options: {', '.join(asked.options)}")
|
|
53
|
+
return "\n".join(lines)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def answer(id: int, given: Answer) -> str:
|
|
57
|
+
"""How what waited was answered."""
|
|
58
|
+
verdict = "allowed" if given.allowed else "denied"
|
|
59
|
+
words = f": {given.text}" if given.text else ""
|
|
60
|
+
return f"#{id} {verdict} by {given.by}{words}"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def script(info: ScriptInfo) -> str:
|
|
64
|
+
"""A script the daemon runs or ran, in a line: how it stands."""
|
|
65
|
+
ended = "" if info.status == RUNNING else f" (exit code {info.exit_code})"
|
|
66
|
+
return f"script {info.id} {info.name}: {info.status}{ended}"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def output(id: int, text: str) -> str:
|
|
70
|
+
"""What a script printed, each line marked with its number."""
|
|
71
|
+
return "\n".join(f"script {id} | {line}" for line in text.splitlines())
|
thzdaq_cli/station.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""thzdaq station show|load|new: the profile the daemon works with. A profile
|
|
2
|
+
is a YAML file, or one of the program's stations/ by its name (lab)."""
|
|
3
|
+
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from thzdaq_api.station import (
|
|
7
|
+
DEVICES,
|
|
8
|
+
DRIVER,
|
|
9
|
+
INSTRUMENTS,
|
|
10
|
+
KIND,
|
|
11
|
+
TITLE,
|
|
12
|
+
GetStation,
|
|
13
|
+
LoadStation,
|
|
14
|
+
NewStation,
|
|
15
|
+
StationProfile,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from thzdaq_api.workspace import is_profile_name
|
|
19
|
+
|
|
20
|
+
from thzdaq_cli import show
|
|
21
|
+
from thzdaq_cli.client import OK, Client
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def add(commands) -> None:
|
|
25
|
+
group = commands.add_parser("station", help="the profile of the bench")
|
|
26
|
+
actions = group.add_subparsers(dest="action", required=True, metavar="ACTION")
|
|
27
|
+
actions.add_parser("show", help="its instruments and devices").set_defaults(
|
|
28
|
+
run=show_profile
|
|
29
|
+
)
|
|
30
|
+
load = actions.add_parser(
|
|
31
|
+
"load", help="work with another profile: a YAML file, or a name in stations/"
|
|
32
|
+
)
|
|
33
|
+
load.add_argument("path")
|
|
34
|
+
load.set_defaults(run=load_profile)
|
|
35
|
+
new = actions.add_parser("new", help="start an empty profile in a new file")
|
|
36
|
+
new.add_argument("path")
|
|
37
|
+
new.add_argument("--title", default="bench")
|
|
38
|
+
new.set_defaults(run=new_profile)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def profile_text(station: StationProfile) -> str:
|
|
42
|
+
lines = [
|
|
43
|
+
show.profile(station.name, station.path)
|
|
44
|
+
+ (" simulated" if station.simulate else "")
|
|
45
|
+
]
|
|
46
|
+
for instrument_id, instrument in (station.profile.get(INSTRUMENTS) or {}).items():
|
|
47
|
+
title, driver = instrument.get(TITLE, ""), instrument[DRIVER]
|
|
48
|
+
lines.append(f" {instrument_id}: {title} ({driver})")
|
|
49
|
+
for device_id, device in (instrument.get(DEVICES) or {}).items():
|
|
50
|
+
lines.append(f" {device_id}: {device.get(TITLE, '')} ({device[KIND]})")
|
|
51
|
+
return "\n".join(lines)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def show_profile(args, client: Client) -> int:
|
|
55
|
+
client.out(client.call(GetStation()), profile_text)
|
|
56
|
+
return OK
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def given(path: str) -> str:
|
|
60
|
+
"""A name stays a name (the daemon finds it); a path is this terminal's."""
|
|
61
|
+
return path if is_profile_name(path) else str(Path(path).resolve())
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def load_profile(args, client: Client) -> int:
|
|
65
|
+
client.out(client.call(LoadStation(path=given(args.path))), profile_text)
|
|
66
|
+
return OK
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def new_profile(args, client: Client) -> int:
|
|
70
|
+
started = NewStation(path=given(args.path), title=args.title)
|
|
71
|
+
client.out(client.call(started), profile_text)
|
|
72
|
+
return OK
|