From dba5ddc60cf46747909f175ec505f5475b59b2ce Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 31 Jan 2018 12:14:32 +1000 Subject: [PATCH] kete: drop the non-interactive bits from tuhi-kete Fixes #51 --- tools/tuhi-kete.py | 85 ++++++++-------------------------------------- 1 file changed, 14 insertions(+), 71 deletions(-) diff --git a/tools/tuhi-kete.py b/tools/tuhi-kete.py index 39bcde3..3bb60cb 100755 --- a/tools/tuhi-kete.py +++ b/tools/tuhi-kete.py @@ -246,9 +246,14 @@ class TuhiKeteManager(_DBusObject): None, self._on_name_vanished) - self.mainloop = None + # we can not call GLib.MainLoop() here or it will install a unix signal + # handler for SIGINT, and we will not be able to catch + # KeyboardInterrupt in cmdloop() + self.mainloop = GLib.MainLoop.new(None, False) + self._devices = {} self._pairable_devices = {} + for objpath in self.property('Devices'): device = TuhiKeteDevice(self, objpath) self._devices[device.address] = device @@ -274,18 +279,13 @@ class TuhiKeteManager(_DBusObject): self._pairable_devices = {} def run(self): - if self.mainloop is None: - self.mainloop = GObject.MainLoop() - try: self.mainloop.run() except KeyboardInterrupt: - print('\r', end='') # to remove the ^C self.mainloop.quit() def quit(self): - if self.mainloop is not None: - self.mainloop.quit() + self.mainloop.quit() def _on_properties_changed(self, proxy, changed_props, invalidated_props): if changed_props is None: @@ -318,10 +318,8 @@ class TuhiKeteManager(_DBusObject): def _on_name_vanished(self, connection, name): logger.error('Tuhi daemon went away') - try: - self.mainloop.quit() - except AttributeError: - pass + self.mainloop.quit() + # FIXME: need to bubble this up to the Worker def __getitem__(self, btaddr): return self._devices[btaddr] @@ -330,7 +328,7 @@ class TuhiKeteManager(_DBusObject): return self def __exit__(self, exc_type, exc_val, exc_tb): - pass + self.mainloop.quit() class Worker(GObject.Object): @@ -956,16 +954,11 @@ class TuhiKeteShell(cmd.Cmd): class TuhiKeteShellWorker(Worker): - def __init__(self, manager, args): + def __init__(self, manager): super(TuhiKeteShellWorker, self).__init__(manager) def start_mainloop(self): - # we can not call GLib.MainLoop() here or it will install a unix signal - # handler for SIGINT, and we will not be able to catch - # KeyboardInterrupt in cmdloop() - mainloop = GLib.MainLoop.new(None, False) - - mainloop.run() + self.manager.run() def start(self): self._glib_thread = threading.Thread(target=self.start_mainloop) @@ -981,61 +974,14 @@ class TuhiKeteShellWorker(Worker): self._shell.run() -def parse_list(parser): - sub = parser.add_parser('list', help='list known devices') - sub.set_defaults(worker=Printer) - - -def parse_pair(parser): - sub = parser.add_parser('pair', help='pair a new device') - sub.add_argument('address', metavar='12:34:56:AB:CD:EF', - type=TuhiKeteDevice.is_device_address, - nargs='?', default=None, - help='the address of the device to pair') - sub.set_defaults(worker=Searcher) - - -def parse_listen(parser): - sub = parser.add_parser('listen', help='listen to events from a device') - sub.add_argument('address', metavar='12:34:56:AB:CD:EF', - type=TuhiKeteDevice.is_device_address, - default=None, - help='the address of the device to listen to') - sub.set_defaults(worker=Listener) - - -def parse_fetch(parser): - sub = parser.add_parser('fetch', help='download a drawing from a device and save as svg in $PWD') - sub.add_argument('address', metavar='12:34:56:AB:CD:EF', - type=TuhiKeteDevice.is_device_address, - default=None, - help='the address of the device to fetch from') - sub.add_argument('index', metavar='[|all]', type=str, - default=None, - help='the index of the drawing to fetch or a literal "all"') - sub.set_defaults(worker=Fetcher) - - -def parse_shell(parser): - sub = parser.add_parser('shell', help='run a bash-like shell') - sub.set_defaults(worker=TuhiKeteShellWorker) - - def parse(args): - desc = 'Commandline client to the Tuhi DBus daemon' + desc = 'Interactive commandline client to the Tuhi DBus daemon' parser = argparse.ArgumentParser(description=desc) parser.add_argument('-v', '--verbose', help='Show some debugging informations', action='store_true', default=False) - subparser = parser.add_subparsers(help='Available commands') - parse_list(subparser) - parse_pair(subparser) - parse_listen(subparser) - parse_fetch(subparser) - parse_shell(subparser) - return parser.parse_args(args[1:]) @@ -1044,12 +990,9 @@ def main(args): if args.verbose: logger.setLevel(logging.DEBUG) - if not hasattr(args, 'worker'): - args.worker = TuhiKeteShellWorker - try: with TuhiKeteManager() as mgr: - worker = args.worker(mgr, args) + worker = TuhiKeteShellWorker(mgr) worker.start() except DBusError as e: