mirror of https://github.com/tuhiproject/tuhi.git
parent
2a3b32a4fe
commit
dba5ddc60c
|
@ -246,9 +246,14 @@ class TuhiKeteManager(_DBusObject):
|
||||||
None,
|
None,
|
||||||
self._on_name_vanished)
|
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._devices = {}
|
||||||
self._pairable_devices = {}
|
self._pairable_devices = {}
|
||||||
|
|
||||||
for objpath in self.property('Devices'):
|
for objpath in self.property('Devices'):
|
||||||
device = TuhiKeteDevice(self, objpath)
|
device = TuhiKeteDevice(self, objpath)
|
||||||
self._devices[device.address] = device
|
self._devices[device.address] = device
|
||||||
|
@ -274,18 +279,13 @@ class TuhiKeteManager(_DBusObject):
|
||||||
self._pairable_devices = {}
|
self._pairable_devices = {}
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.mainloop is None:
|
|
||||||
self.mainloop = GObject.MainLoop()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.mainloop.run()
|
self.mainloop.run()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print('\r', end='') # to remove the ^C
|
|
||||||
self.mainloop.quit()
|
self.mainloop.quit()
|
||||||
|
|
||||||
def quit(self):
|
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):
|
def _on_properties_changed(self, proxy, changed_props, invalidated_props):
|
||||||
if changed_props is None:
|
if changed_props is None:
|
||||||
|
@ -318,10 +318,8 @@ class TuhiKeteManager(_DBusObject):
|
||||||
|
|
||||||
def _on_name_vanished(self, connection, name):
|
def _on_name_vanished(self, connection, name):
|
||||||
logger.error('Tuhi daemon went away')
|
logger.error('Tuhi daemon went away')
|
||||||
try:
|
self.mainloop.quit()
|
||||||
self.mainloop.quit()
|
# FIXME: need to bubble this up to the Worker
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __getitem__(self, btaddr):
|
def __getitem__(self, btaddr):
|
||||||
return self._devices[btaddr]
|
return self._devices[btaddr]
|
||||||
|
@ -330,7 +328,7 @@ class TuhiKeteManager(_DBusObject):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
pass
|
self.mainloop.quit()
|
||||||
|
|
||||||
|
|
||||||
class Worker(GObject.Object):
|
class Worker(GObject.Object):
|
||||||
|
@ -956,16 +954,11 @@ class TuhiKeteShell(cmd.Cmd):
|
||||||
|
|
||||||
|
|
||||||
class TuhiKeteShellWorker(Worker):
|
class TuhiKeteShellWorker(Worker):
|
||||||
def __init__(self, manager, args):
|
def __init__(self, manager):
|
||||||
super(TuhiKeteShellWorker, self).__init__(manager)
|
super(TuhiKeteShellWorker, self).__init__(manager)
|
||||||
|
|
||||||
def start_mainloop(self):
|
def start_mainloop(self):
|
||||||
# we can not call GLib.MainLoop() here or it will install a unix signal
|
self.manager.run()
|
||||||
# handler for SIGINT, and we will not be able to catch
|
|
||||||
# KeyboardInterrupt in cmdloop()
|
|
||||||
mainloop = GLib.MainLoop.new(None, False)
|
|
||||||
|
|
||||||
mainloop.run()
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self._glib_thread = threading.Thread(target=self.start_mainloop)
|
self._glib_thread = threading.Thread(target=self.start_mainloop)
|
||||||
|
@ -981,61 +974,14 @@ class TuhiKeteShellWorker(Worker):
|
||||||
self._shell.run()
|
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='[<index>|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):
|
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 = argparse.ArgumentParser(description=desc)
|
||||||
parser.add_argument('-v', '--verbose',
|
parser.add_argument('-v', '--verbose',
|
||||||
help='Show some debugging informations',
|
help='Show some debugging informations',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
default=False)
|
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:])
|
return parser.parse_args(args[1:])
|
||||||
|
|
||||||
|
|
||||||
|
@ -1044,12 +990,9 @@ def main(args):
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
if not hasattr(args, 'worker'):
|
|
||||||
args.worker = TuhiKeteShellWorker
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with TuhiKeteManager() as mgr:
|
with TuhiKeteManager() as mgr:
|
||||||
worker = args.worker(mgr, args)
|
worker = TuhiKeteShellWorker(mgr)
|
||||||
worker.start()
|
worker.start()
|
||||||
|
|
||||||
except DBusError as e:
|
except DBusError as e:
|
||||||
|
|
Loading…
Reference in New Issue