comparison sat/bridge/bridge_constructor/constructors/dbus/dbus_frontend_template.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 71cfe9334f73
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
30 DBusGMainLoop(set_as_default=True) 30 DBusGMainLoop(set_as_default=True)
31 log = getLogger(__name__) 31 log = getLogger(__name__)
32 32
33 33
34 # Interface prefix 34 # Interface prefix
35 const_INT_PREFIX = config.getConfig( 35 const_INT_PREFIX = config.config_get(
36 config.parseMainConf(), 36 config.parse_main_conf(),
37 "", 37 "",
38 "bridge_dbus_int_prefix", 38 "bridge_dbus_int_prefix",
39 "org.libervia.Libervia") 39 "org.libervia.Libervia")
40 const_ERROR_PREFIX = const_INT_PREFIX + ".error" 40 const_ERROR_PREFIX = const_INT_PREFIX + ".error"
41 const_OBJ_PATH = '/org/libervia/Libervia/bridge' 41 const_OBJ_PATH = '/org/libervia/Libervia/bridge'
64 except (SyntaxError, ValueError, TypeError): 64 except (SyntaxError, ValueError, TypeError):
65 condition = '' 65 condition = ''
66 return BridgeException(name, message, condition) 66 return BridgeException(name, message, condition)
67 67
68 68
69 class Bridge: 69 class bridge:
70 70
71 def bridgeConnect(self, callback, errback): 71 def bridge_connect(self, callback, errback):
72 try: 72 try:
73 self.sessions_bus = dbus.SessionBus() 73 self.sessions_bus = dbus.SessionBus()
74 self.db_object = self.sessions_bus.get_object(const_INT_PREFIX, 74 self.db_object = self.sessions_bus.get_object(const_INT_PREFIX,
75 const_OBJ_PATH) 75 const_OBJ_PATH)
76 self.db_core_iface = dbus.Interface(self.db_object, 76 self.db_core_iface = dbus.Interface(self.db_object,
103 try: 103 try:
104 return object.__getattribute__(self, name) 104 return object.__getattribute__(self, name)
105 except AttributeError: 105 except AttributeError:
106 # The attribute is not found, we try the plugin proxy to find the requested method 106 # The attribute is not found, we try the plugin proxy to find the requested method
107 107
108 def getPluginMethod(*args, **kwargs): 108 def get_plugin_method(*args, **kwargs):
109 # We first check if we have an async call. We detect this in two ways: 109 # We first check if we have an async call. We detect this in two ways:
110 # - if we have the 'callback' and 'errback' keyword arguments 110 # - if we have the 'callback' and 'errback' keyword arguments
111 # - or if the last two arguments are callable 111 # - or if the last two arguments are callable
112 112
113 async_ = False 113 async_ = False
154 proxy._introspect_state = IN_PROGRESS 154 proxy._introspect_state = IN_PROGRESS
155 proxy._Introspect() 155 proxy._Introspect()
156 return self.db_plugin_iface.get_dbus_method(name)(*args, **kwargs) 156 return self.db_plugin_iface.get_dbus_method(name)(*args, **kwargs)
157 raise e 157 raise e
158 158
159 return getPluginMethod 159 return get_plugin_method
160 160
161 ##METHODS_PART## 161 ##METHODS_PART##
162 162
163 class AIOBridge(Bridge): 163 class AIOBridge(bridge):
164 164
165 def register_signal(self, functionName, handler, iface="core"): 165 def register_signal(self, functionName, handler, iface="core"):
166 loop = asyncio.get_running_loop() 166 loop = asyncio.get_running_loop()
167 async_handler = lambda *args: asyncio.run_coroutine_threadsafe(handler(*args), loop) 167 async_handler = lambda *args: asyncio.run_coroutine_threadsafe(handler(*args), loop)
168 return super().register_signal(functionName, async_handler, iface) 168 return super().register_signal(functionName, async_handler, iface)
171 """ usual __getattribute__ if the method exists, else try to find a plugin method """ 171 """ usual __getattribute__ if the method exists, else try to find a plugin method """
172 try: 172 try:
173 return object.__getattribute__(self, name) 173 return object.__getattribute__(self, name)
174 except AttributeError: 174 except AttributeError:
175 # The attribute is not found, we try the plugin proxy to find the requested method 175 # The attribute is not found, we try the plugin proxy to find the requested method
176 def getPluginMethod(*args, **kwargs): 176 def get_plugin_method(*args, **kwargs):
177 loop = asyncio.get_running_loop() 177 loop = asyncio.get_running_loop()
178 fut = loop.create_future() 178 fut = loop.create_future()
179 method = getattr(self.db_plugin_iface, name) 179 method = getattr(self.db_plugin_iface, name)
180 reply_handler = lambda ret=None: loop.call_soon_threadsafe( 180 reply_handler = lambda ret=None: loop.call_soon_threadsafe(
181 fut.set_result, ret) 181 fut.set_result, ret)
189 reply_handler=reply_handler, 189 reply_handler=reply_handler,
190 error_handler=error_handler 190 error_handler=error_handler
191 ) 191 )
192 except ValueError as e: 192 except ValueError as e:
193 if e.args[0].startswith("Unable to guess signature"): 193 if e.args[0].startswith("Unable to guess signature"):
194 # same hack as for Bridge.__getattribute__ 194 # same hack as for bridge.__getattribute__
195 log.warning("using hack to work around inspection issue") 195 log.warning("using hack to work around inspection issue")
196 proxy = self.db_plugin_iface.proxy_object 196 proxy = self.db_plugin_iface.proxy_object
197 IN_PROGRESS = proxy.INTROSPECT_STATE_INTROSPECT_IN_PROGRESS 197 IN_PROGRESS = proxy.INTROSPECT_STATE_INTROSPECT_IN_PROGRESS
198 proxy._introspect_state = IN_PROGRESS 198 proxy._introspect_state = IN_PROGRESS
199 proxy._Introspect() 199 proxy._Introspect()
207 207
208 else: 208 else:
209 raise e 209 raise e
210 return fut 210 return fut
211 211
212 return getPluginMethod 212 return get_plugin_method
213 213
214 def bridgeConnect(self): 214 def bridge_connect(self):
215 loop = asyncio.get_running_loop() 215 loop = asyncio.get_running_loop()
216 fut = loop.create_future() 216 fut = loop.create_future()
217 super().bridgeConnect( 217 super().bridge_connect(
218 callback=lambda: loop.call_soon_threadsafe(fut.set_result, None), 218 callback=lambda: loop.call_soon_threadsafe(fut.set_result, None),
219 errback=lambda e: loop.call_soon_threadsafe(fut.set_exception, e) 219 errback=lambda e: loop.call_soon_threadsafe(fut.set_exception, e)
220 ) 220 )
221 return fut 221 return fut
222 222