WampClient : Simplified, removed dead code, use exception for handling failures and misconfiguration, check existence of mandatory parameters. nevow_service_rework
authorEdouard Tisserant
Thu, 05 Jul 2018 11:11:01 +0200
branchnevow_service_rework
changeset 2212 cf1718962567
parent 2211 46447d99e5f9
child 2213 73b7034693b9
WampClient : Simplified, removed dead code, use exception for handling failures and misconfiguration, check existence of mandatory parameters.
Beremiz_service.py
runtime/WampClient.py
--- a/Beremiz_service.py	Wed Jul 04 14:17:00 2018 +0200
+++ b/Beremiz_service.py	Thu Jul 05 11:11:01 2018 +0200
@@ -623,15 +623,8 @@
 
     if wampconf is not None:
         try:
-            WC.SetServer(pyroserver, wampconf, wampsecret)
-            _wampconf = WC.GetConfiguration()
-            if _wampconf:
-                if _wampconf.get("url", False) and _wampconf.get("active", False):  # TODO : test more ?
-                    WC.RegisterWampClient()
-                else:
-                    raise Exception(_("WAMP config is incomplete or active is false."))
-            else:
-                raise Exception(_("WAMP config is missing."))
+            WC.SetServer(pyroserver)
+            WC.RegisterWampClient(wampconf, wampsecret)
         except Exception:
             LogMessageAndException(_("WAMP client startup failed. "))
 
--- a/runtime/WampClient.py	Wed Jul 04 14:17:00 2018 +0200
+++ b/runtime/WampClient.py	Thu Jul 05 11:11:01 2018 +0200
@@ -36,6 +36,8 @@
 from twisted.internet.protocol import ReconnectingClientFactory
 
 
+mandatoryConfigItems = ["ID", "active", "realm", "url"]
+
 _transportFactory = None
 _WampSession = None
 _PySrv = None
@@ -74,15 +76,6 @@
         obj = getattr(obj, names.pop(0))
     return obj
 
-def getValidOptins(options, arguments):
-    validOptions = {}
-    for key in options:
-        if key in arguments:
-            validOptions[key] = options[key]
-    if len(validOptions) > 0:
-        return validOptions
-    else:
-        return None
 
 class WampSession(wamp.ApplicationSession):
     def onConnect(self):
@@ -164,45 +157,25 @@
             del connector
 
 
-def GetConfiguration(items=None):
+def GetConfiguration():
+    WSClientConf = json.load(open(_WampConf))
+    for itemName in mandatoryConfigItems:
+        if WSClientConf.get(itemName, None) is None :
+            raise Exception(_("WAMP configuration error : missing '{}' parameter.").format(itemName))
+
+    return WSClientConf
+
+
+def SetConfiguration(WSClientConf):
     try:
-        WSClientConf = json.load(open(_WampConf))
-        if items and isinstance(items, list):
-            WSClientConfItems = {}
-            for item in items:
-                wampconf_value = WSClientConf.get(item, None)
-                if wampconf_value is not None:
-                    WSClientConfItems[item] = wampconf_value
-            if WSClientConfItems:
-                return WSClientConfItems
-        return WSClientConf
-    except ValueError, ve:
-        print(_("WAMP load error: "), ve)
-        return None
-    except Exception, e:
-        print(_("WAMP load error: "), e)
-        return None
-
-def SetConfiguration(items):
-    try:
-        WSClientConf = json.load(open(_WampConf))
-        saveChanges = False
-        if items:
-            for itemKey in items.keys():
-                wampconf_value = WSClientConf.get(itemKey, None)
-                if (wampconf_value is not None) and (items[itemKey] is not None) and (wampconf_value != items[itemKey]):
-                    WSClientConf[itemKey] = items[itemKey]
-                    saveChanges = True
-
-        if saveChanges:
-            with open(os.path.realpath(_WampConf), 'w') as f:
-                json.dump(WSClientConf, f, sort_keys=True, indent=4)
-            if 'active' in WSClientConf and WSClientConf['active']:
-                if _transportFactory and _WampSession:
-                    StopReconnectWampClient()
-                StartReconnectWampClient()
-            else:
+        with open(os.path.realpath(_WampConf), 'w') as f:
+            json.dump(WSClientConf, f, sort_keys=True, indent=4)
+        if 'active' in WSClientConf and WSClientConf['active']:
+            if _transportFactory and _WampSession:
                 StopReconnectWampClient()
+            StartReconnectWampClient()
+        else:
+            StopReconnectWampClient()
 
         return WSClientConf
     except ValueError, ve:
@@ -212,6 +185,7 @@
         print(_("WAMP save error: "), e)
         return None
 
+
 def LoadWampSecret(secretfname):
     try:
         WSClientWampSecret = open(secretfname, 'rb').read()
@@ -230,26 +204,23 @@
         return False
 
 
-def RegisterWampClient(wampconf=None, secretfname=None):
-    global _WampConf
+def RegisterWampClient(wampconf=None, wampsecret=None):
+    global _WampConf, _WampSecret
+    if wampsecret:
+        _WampSecret = wampsecret
     if wampconf:
         _WampConf = wampconf
-        WSClientConf = GetConfiguration()
-    else:
-        WSClientConf = GetConfiguration()
-
-    if not WSClientConf:
-        print(_("WAMP client connection not established!"))
-        return False
+
+    WSClientConf = GetConfiguration()
 
     if not IsCorrectUri(WSClientConf["url"]):
-        print(_("WAMP url {} is not correct!".format(WSClientConf["url"])))
-        return False
-
-    if secretfname:
-        WampSecret = LoadWampSecret(secretfname)
-    else:
-        WampSecret = LoadWampSecret(_WampSecret)
+        raise Exception(_("WAMP url {} is not correct!").format(WSClientConf["url"]))
+
+    if not WSClientConf["active"]:
+        print(_("WAMP deactivated in configuration"))
+        return
+
+    WampSecret = LoadWampSecret(_WampSecret)
 
     if WampSecret is not None:
         WSClientConf["secret"] = WampSecret
@@ -303,8 +274,5 @@
     return _WampSession and _WampSession.is_attached()
 
 
-def SetServer(pysrv, wampconf=None, wampsecret=None):
-    global _PySrv, _WampConf, _WampSecret
+def SetServer(pysrv):
     _PySrv = pysrv
-    _WampConf = wampconf
-    _WampSecret = wampsecret