diff sat/plugins/plugin_misc_ip.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 be6d91572633
children
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_ip.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_misc_ip.py	Sat Apr 08 13:54:42 2023 +0200
@@ -99,7 +99,7 @@
     def __init__(self, host):
         log.info(_("plugin IP discovery initialization"))
         self.host = host
-        host.memory.updateParams(PARAMS)
+        host.memory.update_params(PARAMS)
 
         # NAT-Port
         try:
@@ -109,50 +109,50 @@
             self._nat = None
 
         # XXX: cache is kept until SàT is restarted
-        #      if IP may have changed, use self.refreshIP
+        #      if IP may have changed, use self.refresh_ip
         self._external_ip_cache = None
         self._local_ip_cache = None
 
-    def getHandler(self, client):
+    def get_handler(self, client):
         return IPPlugin_handler()
 
-    def refreshIP(self):
+    def refresh_ip(self):
         # FIXME: use a trigger instead ?
         self._external_ip_cache = None
         self._local_ip_cache = None
 
-    def _externalAllowed(self, client):
+    def _external_allowed(self, client):
         """Return value of parameter with autorisation of user to do external requests
 
         if parameter is not set, a dialog is shown to use to get its confirmation, and parameted is set according to answer
         @return (defer.Deferred[bool]): True if external request is autorised
         """
-        allow_get_ip = self.host.memory.params.getParamA(
+        allow_get_ip = self.host.memory.params.param_get_a(
             GET_IP_NAME, GET_IP_CATEGORY, use_default=False
         )
 
         if allow_get_ip is None:
             # we don't have autorisation from user yet to use get_ip, we ask him
-            def setParam(allowed):
-                # FIXME: we need to use boolConst as setParam only manage str/unicode
+            def param_set(allowed):
+                # FIXME: we need to use bool_const as param_set only manage str/unicode
                 #        need to be fixed when params will be refactored
-                self.host.memory.setParam(
-                    GET_IP_NAME, C.boolConst(allowed), GET_IP_CATEGORY
+                self.host.memory.param_set(
+                    GET_IP_NAME, C.bool_const(allowed), GET_IP_CATEGORY
                 )
                 return allowed
 
-            d = xml_tools.deferConfirm(
+            d = xml_tools.defer_confirm(
                 self.host,
                 _(GET_IP_CONFIRM),
                 _(GET_IP_CONFIRM_TITLE),
                 profile=client.profile,
             )
-            d.addCallback(setParam)
+            d.addCallback(param_set)
             return d
 
         return defer.succeed(allow_get_ip)
 
-    def _filterAddresse(self, ip_addr):
+    def _filter_addresse(self, ip_addr):
         """Filter acceptable addresses
 
         For now, just remove IPv4 local addresses
@@ -161,7 +161,7 @@
         """
         return not ip_addr.startswith("127.")
 
-    def _insertFirst(self, addresses, ip_addr):
+    def _insert_first(self, addresses, ip_addr):
         """Insert ip_addr as first item in addresses
 
         @param addresses(list): list of IP addresses
@@ -174,7 +174,7 @@
         else:
             addresses.insert(0, ip_addr)
 
-    async def _getIPFromExternal(self, ext_url):
+    async def _get_ip_from_external(self, ext_url):
         """Get local IP by doing a connection on an external url
 
         @param ext_utl(str): url to connect to
@@ -201,7 +201,7 @@
         return local_ip
 
     @defer.inlineCallbacks
-    def getLocalIPs(self, client):
+    def get_local_i_ps(self, client):
         """Try do discover local area network IPs
 
         @return (deferred): list of lan IP addresses
@@ -225,43 +225,43 @@
                     continue
                 for data in inet_list:
                     addresse = data["addr"]
-                    if self._filterAddresse(addresse):
+                    if self._filter_addresse(addresse):
                         addresses.append(addresse)
 
         # then we use our connection to server
         ip = client.xmlstream.transport.getHost().host
-        if self._filterAddresse(ip):
-            self._insertFirst(addresses, ip)
+        if self._filter_addresse(ip):
+            self._insert_first(addresses, ip)
             defer.returnValue(addresses)
 
         # if server is local, we try with NAT-Port
         if self._nat is not None:
-            nat_ip = yield self._nat.getIP(local=True)
+            nat_ip = yield self._nat.get_ip(local=True)
             if nat_ip is not None:
-                self._insertFirst(addresses, nat_ip)
+                self._insert_first(addresses, nat_ip)
                 defer.returnValue(addresses)
 
             if addresses:
                 defer.returnValue(addresses)
 
         # still not luck, we need to contact external website
-        allow_get_ip = yield self._externalAllowed(client)
+        allow_get_ip = yield self._external_allowed(client)
 
         if not allow_get_ip:
             defer.returnValue(addresses or localhost)
 
         try:
-            local_ip = yield defer.ensureDeferred(self._getIPFromExternal(GET_IP_PAGE))
+            local_ip = yield defer.ensureDeferred(self._get_ip_from_external(GET_IP_PAGE))
         except (internet_error.DNSLookupError, internet_error.TimeoutError):
             log.warning("Can't access Domain Name System")
         else:
             if local_ip is not None:
-                self._insertFirst(addresses, local_ip)
+                self._insert_first(addresses, local_ip)
 
         defer.returnValue(addresses or localhost)
 
     @defer.inlineCallbacks
-    def getExternalIP(self, client):
+    def get_external_ip(self, client):
         """Try to discover external IP
 
         @return (deferred): external IP address or None if it can't be discovered
@@ -295,13 +295,13 @@
 
         # then with NAT-Port
         if self._nat is not None:
-            nat_ip = yield self._nat.getIP()
+            nat_ip = yield self._nat.get_ip()
             if nat_ip is not None:
                 self._external_ip_cache = nat_ip
                 defer.returnValue(nat_ip)
 
         # and finally by requesting external website
-        allow_get_ip = yield self._externalAllowed(client)
+        allow_get_ip = yield self._external_allowed(client)
         try:
             ip = ((yield webclient.getPage(GET_IP_PAGE.encode('utf-8')))
                   if allow_get_ip else None)