comparison libervia/backend/bridge/bridge_constructor/constructors/dbus/dbus_core_template.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
29 29
30 log = getLogger(__name__) 30 log = getLogger(__name__)
31 31
32 # Interface prefix 32 # Interface prefix
33 const_INT_PREFIX = config.config_get( 33 const_INT_PREFIX = config.config_get(
34 config.parse_main_conf(), 34 config.parse_main_conf(), "", "bridge_dbus_int_prefix", "org.libervia.Libervia"
35 "", 35 )
36 "bridge_dbus_int_prefix",
37 "org.libervia.Libervia")
38 const_ERROR_PREFIX = const_INT_PREFIX + ".error" 36 const_ERROR_PREFIX = const_INT_PREFIX + ".error"
39 const_OBJ_PATH = "/org/libervia/Libervia/bridge" 37 const_OBJ_PATH = "/org/libervia/Libervia/bridge"
40 const_CORE_SUFFIX = ".core" 38 const_CORE_SUFFIX = ".core"
41 const_PLUGIN_SUFFIX = ".plugin" 39 const_PLUGIN_SUFFIX = ".plugin"
42 40
86 84
87 class DBusObject(objects.DBusObject): 85 class DBusObject(objects.DBusObject):
88 86
89 core_iface = DBusInterface( 87 core_iface = DBusInterface(
90 const_INT_PREFIX + const_CORE_SUFFIX, 88 const_INT_PREFIX + const_CORE_SUFFIX,
91 ##METHODS_DECLARATIONS_PART## 89 ##METHODS_DECLARATIONS_PART##
92 ##SIGNALS_DECLARATIONS_PART## 90 ##SIGNALS_DECLARATIONS_PART##
93 ) 91 )
94 plugin_iface = DBusInterface( 92 plugin_iface = DBusInterface(const_INT_PREFIX + const_PLUGIN_SUFFIX)
95 const_INT_PREFIX + const_PLUGIN_SUFFIX
96 )
97 93
98 dbusInterfaces = [core_iface, plugin_iface] 94 dbusInterfaces = [core_iface, plugin_iface]
99 95
100 def __init__(self, path): 96 def __init__(self, path):
101 super().__init__(path) 97 super().__init__(path)
114 else: 110 else:
115 d = defer.maybeDeferred(cb, *args, **kwargs) 111 d = defer.maybeDeferred(cb, *args, **kwargs)
116 d.addErrback(GenericException.create_and_raise) 112 d.addErrback(GenericException.create_and_raise)
117 return d 113 return d
118 114
115
119 ##METHODS_PART## 116 ##METHODS_PART##
117
120 118
121 class bridge: 119 class bridge:
122 120
123 def __init__(self): 121 def __init__(self):
124 log.info("Init DBus...") 122 log.info("Init DBus...")
138 raise BridgeInitError(str(e)) 136 raise BridgeInitError(str(e))
139 137
140 conn.exportObject(self._obj) 138 conn.exportObject(self._obj)
141 await conn.requestBusName(const_INT_PREFIX) 139 await conn.requestBusName(const_INT_PREFIX)
142 140
143 ##SIGNALS_PART## 141 ##SIGNALS_PART##
144 def register_method(self, name, callback): 142 def register_method(self, name, callback):
145 log.debug(f"registering DBus bridge method [{name}]") 143 log.debug(f"registering DBus bridge method [{name}]")
146 self._obj.register_method(name, callback) 144 self._obj.register_method(name, callback)
147 145
148 def emit_signal(self, name, *args): 146 def emit_signal(self, name, *args):
149 self._obj.emitSignal(name, *args) 147 self._obj.emitSignal(name, *args)
150 148
151 def add_method( 149 def add_method(
152 self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={} 150 self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={}
153 ): 151 ):
154 """Dynamically add a method to D-Bus bridge""" 152 """Dynamically add a method to D-Bus bridge"""
155 # FIXME: doc parameter is kept only temporary, the time to remove it from calls 153 # FIXME: doc parameter is kept only temporary, the time to remove it from calls
156 log.debug(f"Adding method {name!r} to D-Bus bridge") 154 log.debug(f"Adding method {name!r} to D-Bus bridge")
157 self._obj.plugin_iface.addMethod( 155 self._obj.plugin_iface.addMethod(
158 Method(name, arguments=in_sign, returns=out_sign) 156 Method(name, arguments=in_sign, returns=out_sign)
159 ) 157 )
158
160 # we have to create a method here instead of using partialmethod, because txdbus 159 # we have to create a method here instead of using partialmethod, because txdbus
161 # uses __func__ which doesn't work with partialmethod 160 # uses __func__ which doesn't work with partialmethod
162 def caller(self_, *args, **kwargs): 161 def caller(self_, *args, **kwargs):
163 return self_._callback(name, *args, **kwargs) 162 return self_._callback(name, *args, **kwargs)
163
164 setattr(self._obj, f"dbus_{name}", MethodType(caller, self._obj)) 164 setattr(self._obj, f"dbus_{name}", MethodType(caller, self._obj))
165 self.register_method(name, method) 165 self.register_method(name, method)
166 166
167 def add_signal(self, name, int_suffix, signature, doc={}): 167 def add_signal(self, name, int_suffix, signature, doc={}):
168 """Dynamically add a signal to D-Bus bridge""" 168 """Dynamically add a signal to D-Bus bridge"""