diff sat/bridge/pb.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
line wrap: on
line diff
--- a/sat/bridge/pb.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/bridge/pb.py	Sat Apr 08 13:54:42 2023 +0200
@@ -55,17 +55,17 @@
     def __init__(self):
         self.signals_handlers = []
 
-    def remote_initBridge(self, signals_handler):
+    def remote_init_bridge(self, signals_handler):
         self.signals_handlers.append(HandlerWrapper(signals_handler))
         log.info("registered signal handler")
 
-    def sendSignalEb(self, failure_, signal_name):
+    def send_signal_eb(self, failure_, signal_name):
         if not failure_.check(pb.PBConnectionLost):
             log.error(
                 f"Error while sending signal {signal_name}: {failure_}",
             )
 
-    def sendSignal(self, name, args, kwargs):
+    def send_signal(self, name, args, kwargs):
         to_remove = []
         for wrapper in self.signals_handlers:
             handler = wrapper.handler
@@ -74,13 +74,13 @@
             except pb.DeadReferenceError:
                 to_remove.append(wrapper)
             else:
-                d.addErrback(self.sendSignalEb, name)
+                d.addErrback(self.send_signal_eb, name)
         if to_remove:
             for wrapper in to_remove:
                 log.debug("Removing signal handler for dead frontend")
                 self.signals_handlers.remove(wrapper)
 
-    def _bridgeDeactivateSignals(self):
+    def _bridge_deactivate_signals(self):
         if hasattr(self, "signals_paused"):
             log.warning("bridge signals already deactivated")
             if self.signals_handler:
@@ -90,7 +90,7 @@
         self.signals_handlers = []
         log.debug("bridge signals have been deactivated")
 
-    def _bridgeReactivateSignals(self):
+    def _bridge_reactivate_signals(self):
         try:
             self.signals_handlers = self.signals_paused
         except AttributeError:
@@ -102,31 +102,31 @@
 ##METHODS_PART##
 
 
-class Bridge(object):
+class bridge(object):
     def __init__(self):
         log.info("Init Perspective Broker...")
         self.root = PBRoot()
-        conf = config.parseMainConf()
-        getConf = partial(config.getConf, conf, "bridge_pb", "")
-        conn_type = getConf("connection_type", "unix_socket")
+        conf = config.parse_main_conf()
+        get_conf = partial(config.get_conf, conf, "bridge_pb", "")
+        conn_type = get_conf("connection_type", "unix_socket")
         if conn_type == "unix_socket":
-            local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
+            local_dir = Path(config.config_get(conf, "", "local_dir")).resolve()
             socket_path = local_dir / "bridge_pb"
             log.info(f"using UNIX Socket at {socket_path}")
             reactor.listenUNIX(
                 str(socket_path), pb.PBServerFactory(self.root), mode=0o600
             )
         elif conn_type == "socket":
-            port = int(getConf("port", 8789))
+            port = int(get_conf("port", 8789))
             log.info(f"using TCP Socket at port {port}")
             reactor.listenTCP(port, pb.PBServerFactory(self.root))
         else:
             raise ValueError(f"Unknown pb connection type: {conn_type!r}")
 
-    def sendSignal(self, name, *args, **kwargs):
-        self.root.sendSignal(name, args, kwargs)
+    def send_signal(self, name, *args, **kwargs):
+        self.root.send_signal(name, args, kwargs)
 
-    def remote_initBridge(self, signals_handler):
+    def remote_init_bridge(self, signals_handler):
         self.signals_handlers.append(signals_handler)
         log.info("registered signal handler")
 
@@ -135,78 +135,78 @@
         setattr(self.root, "remote_" + name, callback)
         #  self.root.register_method(name, callback)
 
-    def addMethod(
+    def add_method(
             self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={}
     ):
-        """Dynamically add a method to PB Bridge"""
+        """Dynamically add a method to PB bridge"""
         # FIXME: doc parameter is kept only temporary, the time to remove it from calls
         log.debug("Adding method {name} to PB bridge".format(name=name))
         self.register_method(name, method)
 
-    def addSignal(self, name, int_suffix, signature, doc={}):
+    def add_signal(self, name, int_suffix, signature, doc={}):
         log.debug("Adding signal {name} to PB bridge".format(name=name))
         setattr(
-            self, name, lambda *args, **kwargs: self.sendSignal(name, *args, **kwargs)
+            self, name, lambda *args, **kwargs: self.send_signal(name, *args, **kwargs)
         )
 
-    def bridgeDeactivateSignals(self):
+    def bridge_deactivate_signals(self):
         """Stop sending signals to bridge
 
         Mainly used for mobile frontends, when the frontend is paused
         """
-        self.root._bridgeDeactivateSignals()
+        self.root._bridge_deactivate_signals()
 
-    def bridgeReactivateSignals(self):
+    def bridge_reactivate_signals(self):
         """Send again signals to bridge
 
-        Should only be used after bridgeDeactivateSignals has been called
+        Should only be used after bridge_deactivate_signals has been called
         """
-        self.root._bridgeReactivateSignals()
+        self.root._bridge_reactivate_signals()
 
     def _debug(self, action, params, profile):
-        self.sendSignal("_debug", action, params, profile)
+        self.send_signal("_debug", action, params, profile)
 
-    def actionNew(self, action_data, id, security_limit, profile):
-        self.sendSignal("actionNew", action_data, id, security_limit, profile)
+    def action_new(self, action_data, id, security_limit, profile):
+        self.send_signal("action_new", action_data, id, security_limit, profile)
 
     def connected(self, jid_s, profile):
-        self.sendSignal("connected", jid_s, profile)
+        self.send_signal("connected", jid_s, profile)
 
-    def contactDeleted(self, entity_jid, profile):
-        self.sendSignal("contactDeleted", entity_jid, profile)
+    def contact_deleted(self, entity_jid, profile):
+        self.send_signal("contact_deleted", entity_jid, profile)
+
+    def contact_new(self, contact_jid, attributes, groups, profile):
+        self.send_signal("contact_new", contact_jid, attributes, groups, profile)
 
     def disconnected(self, profile):
-        self.sendSignal("disconnected", profile)
+        self.send_signal("disconnected", profile)
 
-    def entityDataUpdated(self, jid, name, value, profile):
-        self.sendSignal("entityDataUpdated", jid, name, value, profile)
+    def entity_data_updated(self, jid, name, value, profile):
+        self.send_signal("entity_data_updated", jid, name, value, profile)
 
-    def messageEncryptionStarted(self, to_jid, encryption_data, profile_key):
-        self.sendSignal("messageEncryptionStarted", to_jid, encryption_data, profile_key)
+    def message_encryption_started(self, to_jid, encryption_data, profile_key):
+        self.send_signal("message_encryption_started", to_jid, encryption_data, profile_key)
 
-    def messageEncryptionStopped(self, to_jid, encryption_data, profile_key):
-        self.sendSignal("messageEncryptionStopped", to_jid, encryption_data, profile_key)
+    def message_encryption_stopped(self, to_jid, encryption_data, profile_key):
+        self.send_signal("message_encryption_stopped", to_jid, encryption_data, profile_key)
 
-    def messageNew(self, uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile):
-        self.sendSignal("messageNew", uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile)
+    def message_new(self, uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile):
+        self.send_signal("message_new", uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile)
 
-    def newContact(self, contact_jid, attributes, groups, profile):
-        self.sendSignal("newContact", contact_jid, attributes, groups, profile)
+    def param_update(self, name, value, category, profile):
+        self.send_signal("param_update", name, value, category, profile)
 
-    def paramUpdate(self, name, value, category, profile):
-        self.sendSignal("paramUpdate", name, value, category, profile)
-
-    def presenceUpdate(self, entity_jid, show, priority, statuses, profile):
-        self.sendSignal("presenceUpdate", entity_jid, show, priority, statuses, profile)
+    def presence_update(self, entity_jid, show, priority, statuses, profile):
+        self.send_signal("presence_update", entity_jid, show, priority, statuses, profile)
 
-    def progressError(self, id, error, profile):
-        self.sendSignal("progressError", id, error, profile)
+    def progress_error(self, id, error, profile):
+        self.send_signal("progress_error", id, error, profile)
 
-    def progressFinished(self, id, metadata, profile):
-        self.sendSignal("progressFinished", id, metadata, profile)
+    def progress_finished(self, id, metadata, profile):
+        self.send_signal("progress_finished", id, metadata, profile)
 
-    def progressStarted(self, id, metadata, profile):
-        self.sendSignal("progressStarted", id, metadata, profile)
+    def progress_started(self, id, metadata, profile):
+        self.send_signal("progress_started", id, metadata, profile)
 
     def subscribe(self, sub_type, entity_jid, profile):
-        self.sendSignal("subscribe", sub_type, entity_jid, profile)
+        self.send_signal("subscribe", sub_type, entity_jid, profile)