diff sat/plugins/plugin_adhoc_dbus.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 7550ae9cfbac
children
line wrap: on
line diff
--- a/sat/plugins/plugin_adhoc_dbus.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_adhoc_dbus.py	Sat Apr 08 13:54:42 2023 +0200
@@ -98,39 +98,39 @@
         log.info(_("plugin Ad-Hoc D-Bus initialization"))
         self.host = host
         if etree is not None:
-            host.bridge.addMethod(
-                "adHocDBusAddAuto",
+            host.bridge.add_method(
+                "ad_hoc_dbus_add_auto",
                 ".plugin",
                 in_sign="sasasasasasass",
                 out_sign="(sa(sss))",
-                method=self._adHocDBusAddAuto,
+                method=self._ad_hoc_dbus_add_auto,
                 async_=True,
             )
-        host.bridge.addMethod(
-            "adHocRemotesGet",
+        host.bridge.add_method(
+            "ad_hoc_remotes_get",
             ".plugin",
             in_sign="s",
             out_sign="a(sss)",
-            method=self._adHocRemotesGet,
+            method=self._ad_hoc_remotes_get,
             async_=True,
         )
         self._c = host.plugins["XEP-0050"]
-        host.registerNamespace("mediaplayer", NS_MEDIA_PLAYER)
+        host.register_namespace("mediaplayer", NS_MEDIA_PLAYER)
         if dbus is not None:
             self.session_bus = dbus.SessionBus()
             self.fd_object = self.session_bus.get_object(
                 FD_NAME, FD_PATH, introspect=False)
 
-    def profileConnected(self, client):
+    def profile_connected(self, client):
         if dbus is not None:
-            self._c.addAdHocCommand(
-                client, self.localMediaCb, D_("Media Players"),
+            self._c.add_ad_hoc_command(
+                client, self.local_media_cb, D_("Media Players"),
                 node=NS_MEDIA_PLAYER,
                 timeout=60*60*6  # 6 hours timeout, to avoid breaking remote
                                  # in the middle of a movie
             )
 
-    def _DBusAsyncCall(self, proxy, method, *args, **kwargs):
+    def _dbus_async_call(self, proxy, method, *args, **kwargs):
         """ Call a DBus method asynchronously and return a deferred
 
         @param proxy: DBus object proxy, as returner by get_object
@@ -149,18 +149,18 @@
         proxy.get_dbus_method(method, dbus_interface=interface)(*args, **kwargs)
         return d
 
-    def _DBusGetProperty(self, proxy, interface, name):
-        return self._DBusAsyncCall(
+    def _dbus_get_property(self, proxy, interface, name):
+        return self._dbus_async_call(
             proxy, "Get", interface, name, interface="org.freedesktop.DBus.Properties")
 
 
-    def _DBusListNames(self):
-        return self._DBusAsyncCall(self.fd_object, "ListNames")
+    def _dbus_list_names(self):
+        return self._dbus_async_call(self.fd_object, "ListNames")
 
-    def _DBusIntrospect(self, proxy):
-        return self._DBusAsyncCall(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE)
+    def _dbus_introspect(self, proxy):
+        return self._dbus_async_call(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE)
 
-    def _acceptMethod(self, method):
+    def _accept_method(self, method):
         """ Return True if we accept the method for a command
         @param method: etree.Element
         @return: True if the method is acceptable
@@ -175,7 +175,7 @@
     @defer.inlineCallbacks
     def _introspect(self, methods, bus_name, proxy):
         log.debug("introspecting path [%s]" % proxy.object_path)
-        introspect_xml = yield self._DBusIntrospect(proxy)
+        introspect_xml = yield self._dbus_introspect(proxy)
         el = etree.fromstring(introspect_xml)
         for node in el.iterchildren("node", "interface"):
             if node.tag == "node":
@@ -191,23 +191,23 @@
                     continue
                 log.debug("introspecting interface [%s]" % name)
                 for method in node.iterchildren("method"):
-                    if self._acceptMethod(method):
+                    if self._accept_method(method):
                         method_name = method.get("name")
                         log.debug("method accepted: [%s]" % method_name)
                         methods.add((proxy.object_path, name, method_name))
 
-    def _adHocDBusAddAuto(self, prog_name, allowed_jids, allowed_groups, allowed_magics,
+    def _ad_hoc_dbus_add_auto(self, prog_name, allowed_jids, allowed_groups, allowed_magics,
                           forbidden_jids, forbidden_groups, flags, profile_key):
-        client = self.host.getClient(profile_key)
-        return self.adHocDBusAddAuto(
+        client = self.host.get_client(profile_key)
+        return self.ad_hoc_dbus_add_auto(
             client, prog_name, allowed_jids, allowed_groups, allowed_magics,
             forbidden_jids, forbidden_groups, flags)
 
     @defer.inlineCallbacks
-    def adHocDBusAddAuto(self, client, prog_name, allowed_jids=None, allowed_groups=None,
+    def ad_hoc_dbus_add_auto(self, client, prog_name, allowed_jids=None, allowed_groups=None,
                          allowed_magics=None, forbidden_jids=None, forbidden_groups=None,
                          flags=None):
-        bus_names = yield self._DBusListNames()
+        bus_names = yield self._dbus_list_names()
         bus_names = [bus_name for bus_name in bus_names if "." + prog_name in bus_name]
         if not bus_names:
             log.info("Can't find any bus for [%s]" % prog_name)
@@ -223,7 +223,7 @@
         yield self._introspect(methods, bus_name, proxy)
 
         if methods:
-            self._addCommand(
+            self._add_command(
                 client,
                 prog_name,
                 bus_name,
@@ -238,13 +238,13 @@
 
         defer.returnValue((str(bus_name), methods))
 
-    def _addCommand(self, client, adhoc_name, bus_name, methods, allowed_jids=None,
+    def _add_command(self, client, adhoc_name, bus_name, methods, allowed_jids=None,
                     allowed_groups=None, allowed_magics=None, forbidden_jids=None,
                     forbidden_groups=None, flags=None):
         if flags is None:
             flags = set()
 
-        def DBusCallback(client, command_elt, session_data, action, node):
+        def d_bus_callback(client, command_elt, session_data, action, node):
             actions = session_data.setdefault("actions", [])
             names_map = session_data.setdefault("names_map", {})
             actions.append(action)
@@ -283,7 +283,7 @@
                 path, iface, command = names_map[command]
                 proxy = self.session_bus.get_object(bus_name, path)
 
-                self._DBusAsyncCall(proxy, command, interface=iface)
+                self._dbus_async_call(proxy, command, interface=iface)
 
                 # job done, we can end the session, except if we have FLAG_LOOP
                 if FLAG_LOOP in flags:
@@ -292,7 +292,7 @@
                     # is OK)
                     del actions[:]
                     names_map.clear()
-                    return DBusCallback(
+                    return d_bus_callback(
                         client, None, session_data, self._c.ACTION.EXECUTE, node
                     )
                 form = data_form.Form("form", title=_("Updated"))
@@ -305,9 +305,9 @@
 
             return (payload, status, None, note)
 
-        self._c.addAdHocCommand(
+        self._c.add_ad_hoc_command(
             client,
-            DBusCallback,
+            d_bus_callback,
             adhoc_name,
             allowed_jids=allowed_jids,
             allowed_groups=allowed_groups,
@@ -318,18 +318,18 @@
 
     ## Local media ##
 
-    def _adHocRemotesGet(self, profile):
-        return self.adHocRemotesGet(self.host.getClient(profile))
+    def _ad_hoc_remotes_get(self, profile):
+        return self.ad_hoc_remotes_get(self.host.get_client(profile))
 
     @defer.inlineCallbacks
-    def adHocRemotesGet(self, client):
+    def ad_hoc_remotes_get(self, client):
         """Retrieve available remote media controlers in our devices
         @return (list[tuple[unicode, unicode, unicode]]): list of devices with:
             - entity full jid
             - device name
             - device label
         """
-        found_data = yield defer.ensureDeferred(self.host.findByFeatures(
+        found_data = yield defer.ensureDeferred(self.host.find_by_features(
             client, [self.host.ns_map['commands']], service=False, roster=False,
             own_jid=True, local_device=True))
 
@@ -344,7 +344,7 @@
                         try:
                             result_elt = yield self._c.do(client, device_jid,
                                                           NS_MEDIA_PLAYER, timeout=5)
-                            command_elt = self._c.getCommandElt(result_elt)
+                            command_elt = self._c.get_command_elt(result_elt)
                             form = data_form.findForm(command_elt, NS_MEDIA_PLAYER)
                             if form is None:
                                 continue
@@ -368,7 +368,7 @@
                         break
         defer.returnValue(remotes)
 
-    def doMPRISCommand(self, proxy, command):
+    def do_mpris_command(self, proxy, command):
         iface, command = command.rsplit(".", 1)
         if command == CMD_GO_BACK:
             command = 'Seek'
@@ -378,9 +378,9 @@
             args = [SEEK_OFFSET]
         else:
             args = []
-        return self._DBusAsyncCall(proxy, command, *args, interface=iface)
+        return self._dbus_async_call(proxy, command, *args, interface=iface)
 
-    def addMPRISMetadata(self, form, metadata):
+    def add_mpris_metadata(self, form, metadata):
         """Serialise MRPIS Metadata according to MPRIS_METADATA_MAP"""
         for mpris_key, name in MPRIS_METADATA_MAP.items():
             if mpris_key in metadata:
@@ -390,7 +390,7 @@
                                               value=value))
 
     @defer.inlineCallbacks
-    def localMediaCb(self, client, command_elt, session_data, action, node):
+    def local_media_cb(self, client, command_elt, session_data, action, node):
         try:
             x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
             command_form = data_form.Form.fromElement(x_elt)
@@ -399,7 +399,7 @@
 
         if command_form is None or len(command_form.fields) == 0:
             # root request, we looks for media players
-            bus_names = yield self._DBusListNames()
+            bus_names = yield self._dbus_list_names()
             bus_names = [b for b in bus_names if b.startswith(MPRIS_PREFIX)]
             if len(bus_names) == 0:
                 note = (self._c.NOTE.INFO, D_("No media player found."))
@@ -445,7 +445,7 @@
             except KeyError:
                 pass
             else:
-                yield self.doMPRISCommand(proxy, command)
+                yield self.do_mpris_command(proxy, command)
 
             # we construct the remote control form
             form = data_form.Form("form", title=D_("Media Player Selection"))
@@ -455,13 +455,13 @@
             for iface, properties_names in MPRIS_PROPERTIES.items():
                 for name in properties_names:
                     try:
-                        value = yield self._DBusGetProperty(proxy, iface, name)
+                        value = yield self._dbus_get_property(proxy, iface, name)
                     except Exception as e:
                         log.warning(_("Can't retrieve attribute {name}: {reason}")
                                     .format(name=name, reason=e))
                         continue
                     if name == MPRIS_METADATA_KEY:
-                        self.addMPRISMetadata(form, value)
+                        self.add_mpris_metadata(form, value)
                     else:
                         form.addField(data_form.Field(fieldType="fixed",
                                                       var=name,