monitor-ead (2859B)
1 #!/usr/bin/python3 2 3 from gi.repository import GLib 4 5 import dbus 6 import dbus.mainloop.glib 7 8 _dbus2py = { 9 dbus.String : str, 10 dbus.UInt32 : int, 11 dbus.Int32 : int, 12 dbus.Int16 : int, 13 dbus.UInt16 : int, 14 dbus.UInt64 : int, 15 dbus.Int64 : int, 16 dbus.Byte : int, 17 dbus.Boolean : bool, 18 dbus.ByteArray : str, 19 dbus.ObjectPath : str 20 } 21 22 def dbus2py(d): 23 t = type(d) 24 if t in _dbus2py: 25 return _dbus2py[t](d) 26 if t is dbus.Dictionary: 27 return dict([(dbus2py(k), dbus2py(v)) for k, v in d.items()]) 28 if t is dbus.Array and d.signature == "y": 29 return "".join([chr(b) for b in d]) 30 if t is dbus.Array or t is list: 31 return [dbus2py(v) for v in d] 32 if t is dbus.Struct or t is tuple: 33 return tuple([dbus2py(v) for v in d]) 34 return d 35 36 def pretty(d): 37 d = dbus2py(d) 38 t = type(d) 39 40 if t in (dict, tuple, list) and len(d) > 0: 41 if t is dict: 42 d = ", ".join(["%s = %s" % (k, pretty(v)) for k, v in d.items()]) 43 return "{ %s }" % d 44 45 d = " ".join([pretty(e) for e in d]) 46 47 if t is tuple: 48 return "( %s )" % d 49 50 if t is str: 51 return "%s" % d 52 53 return str(d) 54 55 def properties_changed(interface, changed, invalidated, path): 56 iface = interface[interface.rfind(".") + 1:] 57 for name, value in changed.items(): 58 print("{%s} [%s] %s = %s" % (iface, path, name, pretty(value))) 59 60 relevant_ifaces = [ "net.connman.ead.Adapter" ] 61 62 def interfaces_added(path, interfaces): 63 for iface, props in interfaces.items(): 64 if not(iface in relevant_ifaces): 65 continue 66 67 print("{Added %s} [%s]" % (iface, path)) 68 69 for name, value in props.items(): 70 print(" %s = %s" % (name, pretty(value))) 71 72 def interfaces_removed(path, interfaces): 73 for iface in interfaces: 74 if not(iface in relevant_ifaces): 75 continue 76 77 print("{Removed %s} [%s]" % (iface, path)) 78 79 if __name__ == '__main__': 80 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 81 82 bus = dbus.SystemBus() 83 84 bus.add_signal_receiver(properties_changed, 85 bus_name="net.connman.ead", 86 dbus_interface="org.freedesktop.DBus.Properties", 87 signal_name="PropertiesChanged", 88 path_keyword="path") 89 90 bus.add_signal_receiver(interfaces_added, bus_name="net.connman.ead", 91 dbus_interface="org.freedesktop.DBus.ObjectManager", 92 signal_name="InterfacesAdded") 93 94 bus.add_signal_receiver(interfaces_removed, bus_name="net.connman.ead", 95 dbus_interface="org.freedesktop.DBus.ObjectManager", 96 signal_name="InterfacesRemoved") 97 98 mainloop = GLib.MainLoop() 99 mainloop.run()