class MSADbusService(service.Object): """ Clase Base para generar un servicio via DBUS. """ def __init__(self, set_as_default=True): DBusGMainLoop(set_as_default=set_as_default) self.session_bus = SessionBus() # WARNING, NO BORRAR la asignación a la variable name aunque ésta no se # use, sino Dbus restartea el servicio N veces una por cada reintento # del cliente. name = service.BusName(self.bus_name, self.session_bus) self._service_init() service.Object.__init__(self, self.session_bus, self.object_path) self._loop = MainLoop() try: self._loop.run() except KeyboardInterrupt: pass def quit(self): """ Cierra el servicio DBUS, útil para casos de reinicio. DEBE ser implementado por los hijos de esta clase. """ pass
def main(): print('starting D-Bus service at %s' % BUS_NAME) DBusGMainLoop(set_as_default=True) bus = dbus.SessionBus() name = dbus.service.BusName(BUS_NAME, bus) service = DBusService(bus=bus) print('call with: "dbus-send --session --print-reply --dest=%(busname)s %(object_path)s ' '%(busname)s.crash int32:123 string:foo boolean:true"' % {'busname': BUS_NAME, 'object_path': OBJECT_PATH}) loop = MainLoop() loop.run()
def pair(args): """ Pair to the specified device Args: args (dict): args parsed on the command line Returns: results (dict): return message and code of the operation """ def success(): try: device_manager.trust_device() device_manager.connect_device() format_results({'result': 'Success', 'code': ''}) finally: mainloop.quit() def error(err): try: if err == 'org.freedesktop.DBus.Error.NoReply' and self.dev: code = 'Timeout' device_manager.cancel_device() if err in ('org.bluez.Error.AuthenticationCanceled', 'org.bluez.Error.AuthenticationFailed', 'org.bluez.Error.AuthenticationRejected', 'org.bluez.Error.AuthenticationTimeout'): code = 'AuthenticationError' else: code = 'CreatingDeviceFailed' format_results({'result': 'Error', 'code': code}) finally: mainloop.quit() mainloop = MainLoop() device_manager = DeviceManager(args.device) device_manager.pair_device(success, error) mainloop.run()
def pair(args): """ Pair to the specified device Args: args (dict): args parsed on the command line Returns: results (dict): return message and code of the operation """ def success(): try: device_manager.trust_device() device_manager.connect_device() format_results({'result': 'Success', 'code': ''}) finally: mainloop.quit() def error(err): try: if err == 'org.freedesktop.DBus.Error.NoReply' and self.dev: code = 'Timeout' device_manager.cancel_device() if err in ('org.bluez.Error.AuthenticationCanceled', 'org.bluez.Error.AuthenticationFailed', 'org.bluez.Error.AuthenticationRejected', 'org.bluez.Error.AuthenticationTimeout'): code = 'AuthenticationError' else: code = 'CreatingDeviceFailed' format_results({'result': 'Error', 'code': code}) finally: mainloop.quit() mainloop = MainLoop() device_manager = DeviceManager(args.device) device_manager.pair_device(success, error) mainloop.run()
loop = MainLoop() # Declare a name where our service can be reached try: bus_name = dbus.service.BusName("com.keystrokes.notifs", bus=dbus.SessionBus(), do_not_queue=True) DesktopNotification(bus_name) except dbus.exceptions.NameExistsException: # print("service is already running") logging.info("service is already running") sys.exit(1) # Run the loop try: loop.run() except KeyboardInterrupt: # print("keyboard interrupt received") logging.info("keyboard interrupt received") except Exception as e: # print("Unexpected exception occurred: '{}'".format(str(e))) logging.info("Unexpected exception occurred: %s", str(e)) finally: loop.quit() # dbus.set_default_main_loop(dbus.mainloop.NativeMainLoop()) # from dbus.mainloop import NativeMainLoop # n = notify2.Notification("Heading","Body with all the details") # def click_callback(a,b,c): # print(a)
self._connection = dbus.ObjectPath(value) else: raise ValueError('Read-only or nonexistent property') props[prop] = self._account_props()[prop] self.AccountPropertyChanged(props) elif iface == ACCOUNT_IFACE_AVATAR_IFACE: if prop == 'Avatar': self._avatar = dbus.Struct( (dbus.ByteArray(value[0]), unicode(value[1])), signature='ays') self.AvatarChanged() else: raise ValueError('Nonexistent property') else: raise ValueError('No such interface') if __name__ == '__main__': DBusGMainLoop(set_as_default=True) try: am = AccountManager() except dbus.NameExistsException: print >> sys.stderr, 'AccountManager already running' sys.exit(1) print "AccountManager running..." mainloop = MainLoop() mainloop.run()
elif prop == 'Connection': self._connection = dbus.ObjectPath(value) else: raise ValueError('Read-only or nonexistent property') props[prop] = self._account_props()[prop] self.AccountPropertyChanged(props) elif iface == ACCOUNT_IFACE_AVATAR_IFACE: if prop == 'Avatar': self._avatar = dbus.Struct( (dbus.ByteArray(value[0]), unicode(value[1])), signature='ays') self.AvatarChanged() else: raise ValueError('Nonexistent property') else: raise ValueError('No such interface') if __name__ == '__main__': DBusGMainLoop(set_as_default=True) try: am = AccountManager() except dbus.NameExistsException: print >> sys.stderr, 'AccountManager already running' sys.exit(1) print "AccountManager running..." mainloop = MainLoop() mainloop.run()