forked from M-Labs/artiq
1
0
Fork 0

artiq_client: remove custom input validation for built-in argparse (#1185)

This commit is contained in:
Drew 2019-01-09 23:58:11 -05:00 committed by Sébastien Bourdeauducq
parent 721c6f3bcc
commit 99a0f61b35
1 changed files with 28 additions and 30 deletions

View File

@ -1,4 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""
Client to send commands to :mod:`artiq_master` and display results locally.
The client can perform actions such as accessing/setting datasets,
scanning devices, scheduling experiments, and looking for experiments/devices.
"""
import argparse import argparse
import logging import logging
@ -15,7 +21,7 @@ from artiq.protocols.pc_rpc import Client
from artiq.protocols.sync_struct import Subscriber from artiq.protocols.sync_struct import Subscriber
from artiq.protocols.broadcast import Receiver from artiq.protocols.broadcast import Receiver
from artiq.protocols import pyon from artiq.protocols import pyon
from artiq.tools import short_format from artiq.tools import short_format, parse_arguments
def clear_screen(): def clear_screen():
@ -46,7 +52,8 @@ def get_argparser():
"scheduling, default: %(default)s)") "scheduling, default: %(default)s)")
parser_add.add_argument("-t", "--timed", default=None, type=str, parser_add.add_argument("-t", "--timed", default=None, type=str,
help="set a due date for the experiment") help="set a due date for the experiment")
parser_add.add_argument("-f", "--flush", default=False, action="store_true", parser_add.add_argument("-f", "--flush", default=False,
action="store_true",
help="flush the pipeline before preparing " help="flush the pipeline before preparing "
"the experiment") "the experiment")
parser_add.add_argument("-R", "--repository", default=False, parser_add.add_argument("-R", "--repository", default=False,
@ -80,10 +87,12 @@ def get_argparser():
help="name of the dataset") help="name of the dataset")
parser_set_dataset.add_argument("value", metavar="VALUE", parser_set_dataset.add_argument("value", metavar="VALUE",
help="value in PYON format") help="value in PYON format")
parser_set_dataset.add_argument("-p", "--persist", action="store_true",
help="make the dataset persistent") persist_group = parser_set_dataset.add_mutually_exclusive_group()
parser_set_dataset.add_argument("-n", "--no-persist", action="store_true", persist_group.add_argument("-p", "--persist", action="store_true",
help="make the dataset non-persistent") help="make the dataset persistent")
persist_group.add_argument("-n", "--no-persist", action="store_true",
help="make the dataset non-persistent")
parser_del_dataset = subparsers.add_parser( parser_del_dataset = subparsers.add_parser(
"del-dataset", help="delete a dataset") "del-dataset", help="delete a dataset")
@ -93,7 +102,8 @@ def get_argparser():
"show", help="show schedule, log, devices or datasets") "show", help="show schedule, log, devices or datasets")
parser_show.add_argument( parser_show.add_argument(
"what", metavar="WHAT", "what", metavar="WHAT",
help="select object to show: schedule/log/devices/datasets") choices=["schedule", "log", "ccb", "devices", "datasets"],
help="select object to show: %(choices)s")
subparsers.add_parser( subparsers.add_parser(
"scan-devices", help="trigger a device database (re)scan") "scan-devices", help="trigger a device database (re)scan")
@ -114,20 +124,11 @@ def get_argparser():
return parser return parser
def _parse_arguments(arguments):
d = {}
for argument in arguments:
name, value = argument.split("=")
d[name] = pyon.decode(value)
return d
def _action_submit(remote, args): def _action_submit(remote, args):
try: try:
arguments = _parse_arguments(args.arguments) arguments = parse_arguments(args.arguments)
except: except Exception as err:
print("Failed to parse run arguments") raise ValueError("Failed to parse run arguments") from err
sys.exit(1)
expid = { expid = {
"log_level": logging.WARNING + args.quiet*10 - args.verbose*10, "log_level": logging.WARNING + args.quiet*10 - args.verbose*10,
@ -154,10 +155,6 @@ def _action_delete(remote, args):
def _action_set_dataset(remote, args): def _action_set_dataset(remote, args):
if args.persist and args.no_persist:
print("Options --persist and --no-persist cannot be specified "
"at the same time")
sys.exit(1)
persist = None persist = None
if args.persist: if args.persist:
persist = True persist = True
@ -190,13 +187,13 @@ def _action_ls(remote, args):
def _show_schedule(schedule): def _show_schedule(schedule):
clear_screen() clear_screen()
if schedule: if schedule:
l = sorted(schedule.items(), sorted_schedule = sorted(schedule.items(),
key=lambda x: (-x[1]["priority"], key=lambda x: (-x[1]["priority"],
x[1]["due_date"] or 0, x[1]["due_date"] or 0,
x[0])) x[0]))
table = PrettyTable(["RID", "Pipeline", " Status ", "Prio", table = PrettyTable(["RID", "Pipeline", " Status ", "Prio",
"Due date", "Revision", "File", "Class name"]) "Due date", "Revision", "File", "Class name"])
for rid, v in l: for rid, v in sorted_schedule:
row = [rid, v["pipeline"], v["status"], v["priority"]] row = [rid, v["pipeline"], v["status"], v["priority"]]
if v["due_date"] is None: if v["due_date"] is None:
row.append("") row.append("")
@ -252,6 +249,7 @@ def _run_subscriber(host, port, subscriber):
def _show_dict(args, notifier_name, display_fun): def _show_dict(args, notifier_name, display_fun):
d = dict() d = dict()
def init_d(x): def init_d(x):
d.clear() d.clear()
d.update(x) d.update(x)
@ -299,8 +297,7 @@ def main():
elif args.what == "datasets": elif args.what == "datasets":
_show_dict(args, "datasets", _show_datasets) _show_dict(args, "datasets", _show_datasets)
else: else:
print("Unknown object to show, use -h to list valid names.") raise ValueError
sys.exit(1)
else: else:
port = 3251 if args.port is None else args.port port = 3251 if args.port is None else args.port
target_name = { target_name = {
@ -318,5 +315,6 @@ def main():
finally: finally:
remote.close_rpc() remote.close_rpc()
if __name__ == "__main__": if __name__ == "__main__":
main() main()