comparison sat/bridge/bridge_constructor/constructors/pb/pb_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 3c7a64d6f49f
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
53 ) 53 )
54 ) 54 )
55 setattr(self, method_name, handler) 55 setattr(self, method_name, handler)
56 56
57 57
58 class Bridge(object): 58 class bridge(object):
59 59
60 def __init__(self): 60 def __init__(self):
61 self.signals_handler = SignalsHandler() 61 self.signals_handler = SignalsHandler()
62 62
63 def __getattr__(self, name): 63 def __getattr__(self, name):
73 name=failure_.type.decode('utf-8'), 73 name=failure_.type.decode('utf-8'),
74 message=str(failure_.value) 74 message=str(failure_.value)
75 ) 75 )
76 ) 76 )
77 77
78 def remoteCallback(self, result, callback): 78 def remote_callback(self, result, callback):
79 """call callback with argument or None 79 """call callback with argument or None
80 80
81 if result is not None not argument is used, 81 if result is not None not argument is used,
82 else result is used as argument 82 else result is used as argument
83 @param result: remote call result 83 @param result: remote call result
110 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]): 110 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]):
111 errback = args.pop() 111 errback = args.pop()
112 callback = args.pop() 112 callback = args.pop()
113 d = self.root.callRemote(name, *args, **kwargs) 113 d = self.root.callRemote(name, *args, **kwargs)
114 if callback is not None: 114 if callback is not None:
115 d.addCallback(self.remoteCallback, callback) 115 d.addCallback(self.remote_callback, callback)
116 if errback is not None: 116 if errback is not None:
117 d.addErrback(errback) 117 d.addErrback(errback)
118 118
119 def _initBridgeEb(self, failure_): 119 def _init_bridge_eb(self, failure_):
120 log.error("Can't init bridge: {msg}".format(msg=failure_)) 120 log.error("Can't init bridge: {msg}".format(msg=failure_))
121 return failure_ 121 return failure_
122 122
123 def _set_root(self, root): 123 def _set_root(self, root):
124 """set remote root object 124 """set remote root object
125 125
126 bridge will then be initialised 126 bridge will then be initialised
127 """ 127 """
128 self.root = root 128 self.root = root
129 d = root.callRemote("initBridge", self.signals_handler) 129 d = root.callRemote("initBridge", self.signals_handler)
130 d.addErrback(self._initBridgeEb) 130 d.addErrback(self._init_bridge_eb)
131 return d 131 return d
132 132
133 def getRootObjectEb(self, failure_): 133 def get_root_object_eb(self, failure_):
134 """Call errback with appropriate bridge error""" 134 """Call errback with appropriate bridge error"""
135 if failure_.check(ConnectionRefusedError, ConnectError): 135 if failure_.check(ConnectionRefusedError, ConnectError):
136 raise exceptions.BridgeExceptionNoService 136 raise exceptions.BridgeExceptionNoService
137 else: 137 else:
138 raise failure_ 138 raise failure_
139 139
140 def bridgeConnect(self, callback, errback): 140 def bridge_connect(self, callback, errback):
141 factory = pb.PBClientFactory() 141 factory = pb.PBClientFactory()
142 conf = config.parseMainConf() 142 conf = config.parse_main_conf()
143 getConf = partial(config.getConf, conf, "bridge_pb", "") 143 get_conf = partial(config.get_conf, conf, "bridge_pb", "")
144 conn_type = getConf("connection_type", "unix_socket") 144 conn_type = get_conf("connection_type", "unix_socket")
145 if conn_type == "unix_socket": 145 if conn_type == "unix_socket":
146 local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() 146 local_dir = Path(config.config_get(conf, "", "local_dir")).resolve()
147 socket_path = local_dir / "bridge_pb" 147 socket_path = local_dir / "bridge_pb"
148 reactor.connectUNIX(str(socket_path), factory) 148 reactor.connectUNIX(str(socket_path), factory)
149 elif conn_type == "socket": 149 elif conn_type == "socket":
150 host = getConf("host", "localhost") 150 host = get_conf("host", "localhost")
151 port = int(getConf("port", 8789)) 151 port = int(get_conf("port", 8789))
152 reactor.connectTCP(host, port, factory) 152 reactor.connectTCP(host, port, factory)
153 else: 153 else:
154 raise ValueError(f"Unknown pb connection type: {conn_type!r}") 154 raise ValueError(f"Unknown pb connection type: {conn_type!r}")
155 d = factory.getRootObject() 155 d = factory.getRootObject()
156 d.addCallback(self._set_root) 156 d.addCallback(self._set_root)
157 if callback is not None: 157 if callback is not None:
158 d.addCallback(lambda __: callback()) 158 d.addCallback(lambda __: callback())
159 d.addErrback(self.getRootObjectEb) 159 d.addErrback(self.get_root_object_eb)
160 if errback is not None: 160 if errback is not None:
161 d.addErrback(lambda failure_: errback(failure_.value)) 161 d.addErrback(lambda failure_: errback(failure_.value))
162 return d 162 return d
163 163
164 def register_signal(self, functionName, handler, iface="core"): 164 def register_signal(self, functionName, handler, iface="core"):
173 async_handler = lambda *args, **kwargs: defer.Deferred.fromFuture( 173 async_handler = lambda *args, **kwargs: defer.Deferred.fromFuture(
174 asyncio.ensure_future(handler(*args, **kwargs))) 174 asyncio.ensure_future(handler(*args, **kwargs)))
175 return super().register_signal(name, async_handler, iface) 175 return super().register_signal(name, async_handler, iface)
176 176
177 177
178 class AIOBridge(Bridge): 178 class AIOBridge(bridge):
179 179
180 def __init__(self): 180 def __init__(self):
181 self.signals_handler = AIOSignalsHandler() 181 self.signals_handler = AIOSignalsHandler()
182 182
183 def _errback(self, failure_): 183 def _errback(self, failure_):
190 def call(self, name, *args, **kwargs): 190 def call(self, name, *args, **kwargs):
191 d = self.root.callRemote(name, *args, *kwargs) 191 d = self.root.callRemote(name, *args, *kwargs)
192 d.addErrback(self._errback) 192 d.addErrback(self._errback)
193 return d.asFuture(asyncio.get_event_loop()) 193 return d.asFuture(asyncio.get_event_loop())
194 194
195 async def bridgeConnect(self): 195 async def bridge_connect(self):
196 d = super().bridgeConnect(callback=None, errback=None) 196 d = super().bridge_connect(callback=None, errback=None)
197 return await d.asFuture(asyncio.get_event_loop()) 197 return await d.asFuture(asyncio.get_event_loop())
198 198
199 ##ASYNC_METHODS_PART## 199 ##ASYNC_METHODS_PART##