From 1744912ccd4356ab3c57429a06a7807f88298919 Mon Sep 17 00:00:00 2001
From: Susa Giogoli <su.giogoli@icloud.com>
Date: Fri, 14 Jul 2023 15:30:01 +0200
Subject: [PATCH] instantiateComponents and buildComponents are cleaned and
 commented too

---
 newmodeljsonparse.py | 73 ++++++++++++++++++++++++++------------------
 writefile.py         |  6 +---
 2 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/newmodeljsonparse.py b/newmodeljsonparse.py
index a1e35fd..d63336f 100644
--- a/newmodeljsonparse.py
+++ b/newmodeljsonparse.py
@@ -367,9 +367,9 @@ def getComponentVarList(searched, list1, component, jsoncomponents):
         return list1
 
 
-def buildComponents(interfacesreturn,  jsoninterfaces, jsoncomponents, jsonmeasurements, jsonparameters, jsonfunctions,  enumnodedict, enumvalues, enumnames):
+def buildComponents(interfacesreturn,  jsoninterfaces, jsoncomponents, jsonmeasurements, jsonparameters, jsonfunctions,  enumnodedict, enumvalues):
     """objcomponentsclone,
-    Iterates recursively throught the rootcomponent of the interface and its components etc..
+    Iterates recursively throught the rootcomponent of the interface and its components etc.
     With every iteration it collects all variables and functions from each component and their basecomponents,
     creates their respective objects and writesthem with the writefunctions from wf.
     :param interfacesreturn: a dict in which all created objecttype node names get collect in the Format {uuid : objecttype}
@@ -379,9 +379,8 @@ def buildComponents(interfacesreturn,  jsoninterfaces, jsoncomponents, jsonmeasu
     :param jsonparameters: a list with all elements of type parameter
     :param jsonfunctions: a list of all elements with type function
     :param enumnodedict: a dict with all the enumtypes from the format {uuid : enumtypenodename}
-    :param enumvalues:
-    :param enumnames:
-    :return:
+    :param enumvalues: a dict with the changed values from enums from the format {uuid: [0: value1, 1: value2....],...}
+    :return: Interfacesreturn
     """
     for obj in jsoninterfaces:
         rootobj = {}
@@ -408,33 +407,50 @@ def buildComponents(interfacesreturn,  jsoninterfaces, jsoncomponents, jsonmeasu
             functionobj = getFunctions(objfunctionlist, jsonparameters, jsonmeasurements)
             functionvars = getComponentVarList("functions", [], rootobj, jsoncomponents)
             wf.writefunction(functionobj, interfacesreturn[str(obj["uuid"])], functionvars.copy())
-            buildComponents(interfacesreturn,  objcomponentslist, jsoncomponents, jsonmeasurements, jsonparameters, jsonfunctions,  enumnodedict, enumvalues, enumnames)
+            buildComponents(interfacesreturn,  objcomponentslist, jsoncomponents, jsonmeasurements, jsonparameters, jsonfunctions,  enumnodedict, enumvalues)
     return interfacesreturn
 
 
 
 def instantiateComponents(typesdict, interfaces, jsoncomponents, instantiatedict, eventsdict, streamsdict, componentlist, headnodeindex, variabledict):
-        for interface in interfaces:
-            root = interface
-            interfacename = ""
-            newcomponentlist = getComponentVarList("components", [], root, jsoncomponents)
-            for cobj in componentlist:
-                if cobj["value"] == root["uuid"]:
-                    interfacename = cobj["name"]
-                    componentlist.remove(cobj)
-                    break
-            events = getComponentVarList("events", [], root, jsoncomponents)
-            streams = getComponentVarList("streams", [], root, jsoncomponents)
-            variables = getComponentVarList("measurements", [], root, jsoncomponents) + getComponentVarList("parameters", [], root, jsoncomponents)
-            variabledict[interfacename] = variables
-            components = getInterfaceparam([], root, "components", jsoncomponents, jsoncomponents)
-            instantiatedict[str(interfacename)] = wf.writeobjects(typesdict[str(interface["uuid"])], interfacename, instantiatedict[str(headnodeindex)])
-            eventsdict[str(interfacename)] = events
-            streamsdict[str(interfacename)] = streams
-            if len(components) > 0:
-                instantiateComponents(typesdict, components, jsoncomponents, instantiatedict, eventsdict, streamsdict, newcomponentlist, interfacename, variabledict)
-
-        return instantiatedict, eventsdict, streamsdict, variabledict
+    """
+    Instantiates all components and collects events, streams and variable names on the side.
+    :param typesdict: Dict for objecttype node names from the format {uuid : objecttypename}
+    :param interfaces: A list of components
+    :param jsoncomponents: all elements of the type component
+    :param instantiatedict: A dict for the node names of the already instantiated objects
+                                from Format {objectname : object node name}
+    :param eventsdict: Dict where the events of each instantiated component get collected
+                            from Format {objectname : eventlist}
+    :param streamsdict: Dict where the streams of each instantiated component get collected
+                            from Format {objectname : streamlist}
+    :param componentlist: a list for all component objects of the parentnaode to retrieve the individual object name
+    :param headnodeindex: the individual name of the headnode
+    :param variabledict: A retrieved dict for all variables of each component
+                            from Format {objectname : variablelist}
+    :return: instantiateddict, eventsdict, streamsdict, variabledict
+    """
+    for interface in interfaces:
+        root = interface
+        interfacename = ""
+        newcomponentlist = getComponentVarList("components", [], root, jsoncomponents)
+        for cobj in componentlist:
+            if cobj["value"] == root["uuid"]:
+                interfacename = cobj["name"]
+                componentlist.remove(cobj)
+                break
+        events = getComponentVarList("events", [], root, jsoncomponents)
+        streams = getComponentVarList("streams", [], root, jsoncomponents)
+        variables = getComponentVarList("measurements", [], root, jsoncomponents) + getComponentVarList("parameters", [], root, jsoncomponents)
+        variabledict[interfacename] = variables
+        components = getInterfaceparam([], root, "components", jsoncomponents, jsoncomponents)
+        instantiatedict[str(interfacename)] = wf.writeobjects(typesdict[str(interface["uuid"])], interfacename, instantiatedict[str(headnodeindex)])
+        eventsdict[str(interfacename)] = events
+        streamsdict[str(interfacename)] = streams
+        if len(components) > 0:
+            instantiateComponents(typesdict, components, jsoncomponents, instantiatedict, eventsdict, streamsdict, newcomponentlist, interfacename, variabledict)
+
+    return instantiatedict, eventsdict, streamsdict, variabledict
 
 
 def instantiate(typesdict, interfaces, jsoncomponents):
@@ -497,8 +513,7 @@ def main():
         returnvalenums = wf.writeenums(jsonenums)
         enumnodedict = returnvalenums[0]
         enumvalues = returnvalenums[1]
-        enumnames = returnvalenums[2]
-        typesdict = buildComponents({}, jsoninterfaces, jsoncomponents, jsonmeasurements, jsonparameters, jsonfunctions, enumnodedict, enumvalues, enumnames)
+        typesdict = buildComponents({}, jsoninterfaces, jsoncomponents, jsonmeasurements, jsonparameters, jsonfunctions, enumnodedict, enumvalues)
         returnval = instantiate(typesdict, jsoninterfaces, jsoncomponents)
         instantiatedict = returnval[0]
         eventsdict = returnval[1]
diff --git a/writefile.py b/writefile.py
index 6cd1692..035f906 100644
--- a/writefile.py
+++ b/writefile.py
@@ -95,14 +95,11 @@ def writeenums(enumslist):
     :return enumnodenames: Dict with format: {uuid of enum: opc ua object node of the enum}
     :return returnvalues: Dict with format: {uuid of enum: valuelist with
                             added numbers to values showing their corresponding int Value}
-    :return enumvariantnames: Dict with format: {uuid of enum: official name of the enum}
-                                Dict helps so that you don't need to search the fitting enum later on.
     """
     server = open("opcua-server.py", "a")
     writeindent = "    "
     returnvalues = {}
     enumnodenames = {}
-    variantnames = {}
     for enum in enumslist:
         returnvallist = []
         enumname = (enum["name"].lower()).replace(" ", "")
@@ -114,8 +111,7 @@ def writeenums(enumslist):
         server.write(writeindent + enumname + "= await new_enum(server, idx, \""+ str(enum["name"]) + "\", values=" + str(enum["values"]) + ")\n\n")
         enumnodenames[enum["uuid"]] = enumname
         returnvalues[enum["uuid"]] = returnvallist
-        variantnames[enum["uuid"]] = enum["name"]
-    return enumnodenames, returnvalues, variantnames
+    return enumnodenames, returnvalues
 
 
 def openobject(objname, description):
-- 
GitLab