applets: avoid argparse subparser mess

This commit is contained in:
Sebastien Bourdeauducq 2016-02-08 22:25:02 +01:00
parent 1a2596de8e
commit 67327b1e61
2 changed files with 23 additions and 44 deletions

View File

@ -79,24 +79,20 @@ class SimpleApplet:
help="time to wait after a mod (buffering other mods) " help="time to wait after a mod (buffering other mods) "
"before updating (default: %(default).2f)") "before updating (default: %(default).2f)")
self._arggroup_datasets = self.argparser.add_argument_group("datasets") group = self.argparser.add_argument_group("standalone mode (default)")
group.add_argument(
subparsers = self.argparser.add_subparsers(dest="mode")
subparsers.required = True
parser_sa = subparsers.add_parser("standalone",
help="run standalone, connect to master directly")
parser_sa.add_argument(
"--server", default="::1", "--server", default="::1",
help="hostname or IP to connect to") help="hostname or IP of the master to connect to "
parser_sa.add_argument( "for dataset notifications "
"(ignored in embedded mode)")
group.add_argument(
"--port", default=3250, type=int, "--port", default=3250, type=int,
help="TCP port to connect to") help="TCP port to connect to")
parser_em = subparsers.add_parser("embedded", self.argparser.add_argument("--embed", default=None,
help="embed into GUI") help="embed into GUI", metavar="IPC_ADDRESS")
parser_em.add_argument("ipc_address",
help="address for pipe_ipc") self._arggroup_datasets = self.argparser.add_argument_group("datasets")
self.dataset_args = set() self.dataset_args = set()
@ -121,23 +117,13 @@ class SimpleApplet:
asyncio.set_event_loop(self.loop) asyncio.set_event_loop(self.loop)
def ipc_init(self): def ipc_init(self):
if self.args.mode == "standalone": if self.args.embed is not None:
# nothing to do self.ipc = AppletIPCClient(self.args.embed)
pass
elif self.args.mode == "embedded":
self.ipc = AppletIPCClient(self.args.ipc_address)
self.loop.run_until_complete(self.ipc.connect()) self.loop.run_until_complete(self.ipc.connect())
else:
raise NotImplementedError
def ipc_close(self): def ipc_close(self):
if self.args.mode == "standalone": if self.args.embed is not None:
# nothing to do
pass
elif self.args.mode == "embedded":
self.ipc.close() self.ipc.close()
else:
raise NotImplementedError
def create_main_widget(self): def create_main_widget(self):
self.main_widget = self.main_widget_class(self.args) self.main_widget = self.main_widget_class(self.args)
@ -149,7 +135,7 @@ class SimpleApplet:
# 4. applet shows the widget # 4. applet shows the widget
# Doing embedding the other way around (using QWindow.setParent in the # Doing embedding the other way around (using QWindow.setParent in the
# applet) breaks resizing. # applet) breaks resizing.
if self.args.mode == "embedded": if self.args.embed is not None:
self.ipc.set_close_cb(self.main_widget.close) self.ipc.set_close_cb(self.main_widget.close)
win_id = int(self.main_widget.winId()) win_id = int(self.main_widget.winId())
self.loop.run_until_complete(self.ipc.embed(win_id)) self.loop.run_until_complete(self.ipc.embed(win_id))
@ -160,7 +146,7 @@ class SimpleApplet:
return data return data
def filter_mod(self, mod): def filter_mod(self, mod):
if self.args.mode == "embedded": if self.args.embed is not None:
# the parent already filters for us # the parent already filters for us
return True return True
@ -192,24 +178,17 @@ class SimpleApplet:
self.main_widget.data_changed(self.data, [mod]) self.main_widget.data_changed(self.data, [mod])
def subscribe(self): def subscribe(self):
if self.args.mode == "standalone": if self.args.embed is None:
self.subscriber = Subscriber("datasets", self.subscriber = Subscriber("datasets",
self.sub_init, self.sub_mod) self.sub_init, self.sub_mod)
self.loop.run_until_complete(self.subscriber.connect( self.loop.run_until_complete(self.subscriber.connect(
self.args.server_notify, self.args.port_notify)) self.args.server, self.args.port))
elif self.args.mode == "embedded":
self.ipc.subscribe(self.datasets, self.sub_init, self.sub_mod)
else: else:
raise NotImplementedError self.ipc.subscribe(self.datasets, self.sub_init, self.sub_mod)
def unsubscribe(self): def unsubscribe(self):
if self.args.mode == "standalone": if self.args.embed is None:
self.loop.run_until_complete(self.subscriber.close()) self.loop.run_until_complete(self.subscriber.close())
elif self.args.mode == "embedded":
# nothing to do
pass
else:
raise NotImplementedError
def run(self): def run(self):
self.args_init() self.args_init()

View File

@ -137,15 +137,15 @@ class AppletDock(dockarea.Dock):
_templates = [ _templates = [
("Big number", "{python} -m artiq.applets.big_number " ("Big number", "{python} -m artiq.applets.big_number "
"embedded {ipc_address} NUMBER_DATASET"), "--embed {ipc_address} NUMBER_DATASET"),
("Histogram", "{python} -m artiq.applets.plot_hist " ("Histogram", "{python} -m artiq.applets.plot_hist "
"embedded {ipc_address} COUNTS_DATASET " "--embed {ipc_address} COUNTS_DATASET "
"--x BIN_BOUNDARIES_DATASET"), "--x BIN_BOUNDARIES_DATASET"),
("XY", "{python} -m artiq.applets.plot_xy " ("XY", "{python} -m artiq.applets.plot_xy "
"embedded {ipc_address} Y_DATASET --x X_DATASET " "--embed {ipc_address} Y_DATASET --x X_DATASET "
"--error ERROR_DATASET --fit FIT_DATASET"), "--error ERROR_DATASET --fit FIT_DATASET"),
("XY + Histogram", "{python} -m artiq.applets.plot_xy_hist " ("XY + Histogram", "{python} -m artiq.applets.plot_xy_hist "
"embedded {ipc_address} X_DATASET " "--embed {ipc_address} X_DATASET "
"HIST_BIN_BOUNDARIES_DATASET " "HIST_BIN_BOUNDARIES_DATASET "
"HISTS_COUNTS_DATASET"), "HISTS_COUNTS_DATASET"),
] ]