Browse Source

fix significant whitespace issues in namespace generation; make python3-ready

Julius Pfrommer 8 years ago
parent
commit
1f6c816802

+ 8 - 5
tools/pyUANamespace/generate_open62541CCode.py

@@ -67,14 +67,17 @@ parser.add_argument('-v','--verbose', action='count', help='Make the script more
 if __name__ == '__main__':
   args = parser.parse_args()
 
-  level= logging.CRITICAL
-  if (args.verbose==1):
+  level = logging.CRITICAL
+  verbosity = 0
+  if args.verbose:
+    verbosity = int(args.verbose)
+  if (verbosity==1):
     level = logging.ERROR
-  elif (args.verbose==2):
+  elif (verbosity==2):
     level = logging.WARNING
-  elif (args.verbose==3):
+  elif (verbosity==3):
     level = logging.INFO
-  elif (args.verbose>=4):
+  elif (verbosity>=4):
     level = logging.DEBUG
   logging.basicConfig(level=level)
   logger.setLevel(logging.INFO)

+ 58 - 64
tools/pyUANamespace/open62541_MacroHelper.py

@@ -54,17 +54,11 @@ class open62541_MacroHelper():
         returns: C-printable string representation of input
     '''
     # No punctuation characters <>!$
-    illegal_chars = list(string.punctuation)
-    # underscore is allowed
-    illegal_chars.remove('_')
-
-    illegal = "".join(illegal_chars)
-    substitution = ""
-    # Map all punctuation characters to underscore
-    for illegal_char in illegal_chars:
-        substitution = substitution + '_'
-
-    return input.translate(string.maketrans(illegal, substitution), illegal)
+    for illegal_char in list(string.punctuation):
+        if illegal_char == '_': # underscore is allowed
+            continue
+        input = input.replace(illegal_char, "_") # map to underscore
+    return input
 
   def getNodeIdDefineString(self, node):
     code = []
@@ -147,57 +141,57 @@ class open62541_MacroHelper():
 
     # Node ordering should have made sure that arguments, if they exist, have not been printed yet
     if node.nodeClass() == NODE_CLASS_METHOD:
-      inArgVal = []
-      outArgVal = []
-      code.append("UA_Argument *inputArguments = NULL;")
-      code.append("UA_Argument *outputArguments = NULL;")
-      for r in node.getReferences():
-	if r.target() != None and r.target().nodeClass() == NODE_CLASS_VARIABLE and r.target().browseName() == 'InputArguments':
-          while r.target() in unprintedNodes:
-            unprintedNodes.remove(r.target())
-	  if r.target().value() != None:
-	    inArgVal = r.target().value().value
-	elif r.target() != None and r.target().nodeClass() == NODE_CLASS_VARIABLE and r.target().browseName() == 'OutputArguments':
-	  while r.target() in unprintedNodes:
-            unprintedNodes.remove(r.target())
-	  if r.target().value() != None:
-	    outArgVal = r.target().value().value
-      if len(inArgVal)>0:
-	code.append("")
-	code.append("inputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(inArgVal)) + ");")
-	code.append("int inputArgumentCnt;")
-	code.append("for (inputArgumentCnt=0; inputArgumentCnt<" + str(len(inArgVal)) + "; inputArgumentCnt++) UA_Argument_init(&inputArguments[inputArgumentCnt]); ")
-	argumentCnt = 0
-	for inArg in inArgVal:
-	  if inArg.getValueFieldByAlias("Description") != None:
-	    code.append("inputArguments[" + str(argumentCnt) + "].description = UA_LOCALIZEDTEXT(\"" + str(inArg.getValueFieldByAlias("Description")[0]) + "\",\"" + str(inArg.getValueFieldByAlias("Description")[1]) + "\");")
-	  if inArg.getValueFieldByAlias("Name") != None:
-	    code.append("inputArguments[" + str(argumentCnt) + "].name = UA_STRING(\"" + str(inArg.getValueFieldByAlias("Name")) + "\");")
-	  if inArg.getValueFieldByAlias("ValueRank") != None:
-	    code.append("inputArguments[" + str(argumentCnt) + "].valueRank = " + str(inArg.getValueFieldByAlias("ValueRank")) + ";")
-	  if inArg.getValueFieldByAlias("DataType") != None:
-	    code.append("inputArguments[" + str(argumentCnt) + "].dataType = " + str(self.getCreateNodeIDMacro(inArg.getValueFieldByAlias("DataType"))) + ";")
-	  #if inArg.getValueFieldByAlias("ArrayDimensions") != None:
-	  #  code.append("inputArguments[" + str(argumentCnt) + "].arrayDimensions = " + str(inArg.getValueFieldByAlias("ArrayDimensions")) + ";")
-	  argumentCnt += 1
-      if len(outArgVal)>0:
-	code.append("")
-	code.append("outputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(outArgVal)) + ");")
-	code.append("int outputArgumentCnt;")
-	code.append("for (outputArgumentCnt=0; outputArgumentCnt<" + str(len(outArgVal)) + "; outputArgumentCnt++) UA_Argument_init(&outputArguments[outputArgumentCnt]); ")
-	argumentCnt = 0
-	for outArg in outArgVal:
-	  if outArg.getValueFieldByAlias("Description") != None:
-	    code.append("outputArguments[" + str(argumentCnt) + "].description = UA_LOCALIZEDTEXT(\"" + str(outArg.getValueFieldByAlias("Description")[0]) + "\",\"" + str(outArg.getValueFieldByAlias("Description")[1]) + "\");")
-	  if outArg.getValueFieldByAlias("Name") != None:
-	    code.append("outputArguments[" + str(argumentCnt) + "].name = UA_STRING(\"" + str(outArg.getValueFieldByAlias("Name")) + "\");")
-	  if outArg.getValueFieldByAlias("ValueRank") != None:
-	    code.append("outputArguments[" + str(argumentCnt) + "].valueRank = " + str(outArg.getValueFieldByAlias("ValueRank")) + ";")
-	  if outArg.getValueFieldByAlias("DataType") != None:
-	    code.append("outputArguments[" + str(argumentCnt) + "].dataType = " + str(self.getCreateNodeIDMacro(outArg.getValueFieldByAlias("DataType"))) + ";")
-	  #if outArg.getValueFieldByAlias("ArrayDimensions") != None:
-	  #  code.append("outputArguments[" + str(argumentCnt) + "].arrayDimensions = " + str(outArg.getValueFieldByAlias("ArrayDimensions")) + ";")
-	  argumentCnt += 1
+        inArgVal = []
+        outArgVal = []
+        code.append("UA_Argument *inputArguments = NULL;")
+        code.append("UA_Argument *outputArguments = NULL;")
+        for r in node.getReferences():
+            if r.target() != None and r.target().nodeClass() == NODE_CLASS_VARIABLE and r.target().browseName() == 'InputArguments':
+                while r.target() in unprintedNodes:
+                    unprintedNodes.remove(r.target())
+            if r.target().value() != None:
+                inArgVal = r.target().value().value
+            elif r.target() != None and r.target().nodeClass() == NODE_CLASS_VARIABLE and r.target().browseName() == 'OutputArguments':
+                while r.target() in unprintedNodes:
+                    unprintedNodes.remove(r.target())
+                if r.target().value() != None:
+                    outArgVal = r.target().value().value
+        if len(inArgVal)>0:
+            code.append("")
+            code.append("inputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(inArgVal)) + ");")
+            code.append("int inputArgumentCnt;")
+            code.append("for (inputArgumentCnt=0; inputArgumentCnt<" + str(len(inArgVal)) + "; inputArgumentCnt++) UA_Argument_init(&inputArguments[inputArgumentCnt]); ")
+            argumentCnt = 0
+            for inArg in inArgVal:
+                if inArg.getValueFieldByAlias("Description") != None:
+                    code.append("inputArguments[" + str(argumentCnt) + "].description = UA_LOCALIZEDTEXT(\"" + str(inArg.getValueFieldByAlias("Description")[0]) + "\",\"" + str(inArg.getValueFieldByAlias("Description")[1]) + "\");")
+                if inArg.getValueFieldByAlias("Name") != None:
+                    code.append("inputArguments[" + str(argumentCnt) + "].name = UA_STRING(\"" + str(inArg.getValueFieldByAlias("Name")) + "\");")
+                if inArg.getValueFieldByAlias("ValueRank") != None:
+                    code.append("inputArguments[" + str(argumentCnt) + "].valueRank = " + str(inArg.getValueFieldByAlias("ValueRank")) + ";")
+                if inArg.getValueFieldByAlias("DataType") != None:
+                    code.append("inputArguments[" + str(argumentCnt) + "].dataType = " + str(self.getCreateNodeIDMacro(inArg.getValueFieldByAlias("DataType"))) + ";")
+                #if inArg.getValueFieldByAlias("ArrayDimensions") != None:
+                #  code.append("inputArguments[" + str(argumentCnt) + "].arrayDimensions = " + str(inArg.getValueFieldByAlias("ArrayDimensions")) + ";")
+                argumentCnt += 1
+        if len(outArgVal)>0:
+            code.append("")
+            code.append("outputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(outArgVal)) + ");")
+            code.append("int outputArgumentCnt;")
+            code.append("for (outputArgumentCnt=0; outputArgumentCnt<" + str(len(outArgVal)) + "; outputArgumentCnt++) UA_Argument_init(&outputArguments[outputArgumentCnt]); ")
+            argumentCnt = 0
+            for outArg in outArgVal:
+                if outArg.getValueFieldByAlias("Description") != None:
+                    code.append("outputArguments[" + str(argumentCnt) + "].description = UA_LOCALIZEDTEXT(\"" + str(outArg.getValueFieldByAlias("Description")[0]) + "\",\"" + str(outArg.getValueFieldByAlias("Description")[1]) + "\");")
+                if outArg.getValueFieldByAlias("Name") != None:
+                    code.append("outputArguments[" + str(argumentCnt) + "].name = UA_STRING(\"" + str(outArg.getValueFieldByAlias("Name")) + "\");")
+                if outArg.getValueFieldByAlias("ValueRank") != None:
+                    code.append("outputArguments[" + str(argumentCnt) + "].valueRank = " + str(outArg.getValueFieldByAlias("ValueRank")) + ";")
+                if outArg.getValueFieldByAlias("DataType") != None:
+                    code.append("outputArguments[" + str(argumentCnt) + "].dataType = " + str(self.getCreateNodeIDMacro(outArg.getValueFieldByAlias("DataType"))) + ";")
+                #if outArg.getValueFieldByAlias("ArrayDimensions") != None:
+                #  code.append("outputArguments[" + str(argumentCnt) + "].arrayDimensions = " + str(outArg.getValueFieldByAlias("ArrayDimensions")) + ";")
+                argumentCnt += 1
 
     # print the attributes struct
     code.append("UA_%sAttributes attr;" % nodetype)
@@ -209,9 +203,9 @@ class open62541_MacroHelper():
       code = code + node.printOpen62541CCode_SubtypeEarly(bootstrapping = False)
     elif nodetype == "Method":
       if node.executable():
-	code.append("attr.executable = true;")
+        code.append("attr.executable = true;")
       if node.userExecutable():
-	code.append("attr.userExecutable = true;")
+        code.append("attr.userExecutable = true;")
 
     code.append("UA_NodeId nodeId = " + str(self.getCreateNodeIDMacro(node)) + ";")
     if nodetype in ["Object", "Variable"]:

+ 5 - 5
tools/pyUANamespace/open62541_XMLPreprocessor.py

@@ -146,7 +146,7 @@ class preProcessDocument:
     idDict = {}
 
     for ndid in self.containedNodes:
-      if not idDict.has_key(ndid[0].ns):
+      if not ndid[0].ns in idDict.keys():
         idDict[ndid[0].ns] = 1
       else:
         idDict[ndid[0].ns] = idDict[ndid[0].ns] + 1
@@ -191,7 +191,7 @@ class preProcessDocument:
     outline = self.nodeset.toxml()
     for qualifier in self.namespaceQualifiers:
       rq = qualifier+":"
-      outline = outline.replace(rq.decode('UTF-8'), "")
+      outline = outline.replace(rq, "")
     os.write(outfile, outline.encode('UTF-8'))
     os.close(outfile)
 
@@ -264,7 +264,7 @@ class open62541_XMLPreprocessor:
     for ref in refs:
       for n in doc.containedNodes:
         if str(ref) == str(n[0]):
-          print ref, n[0]
+          print(ref, n[0])
           found = found + 1
           break
     return float(found)/float(sspace)
@@ -348,12 +348,12 @@ class open62541_XMLPreprocessor:
           r.toString()
         # ... how many of them would be found!?
         c = self.testModelCongruencyAgainstReferences(tDoc, refs)
-        print c
+        print(c)
         if c>0:
           matches.append(c, tDoc)
       best = (0, None)
       for m in matches:
-        print m[0]
+        print(m[0])
         if m[0] > best[0]:
           best = m
       if best[1] != None:

+ 1 - 1
tools/pyUANamespace/ua_namespace.py

@@ -664,7 +664,7 @@ class opcua_namespace():
       else:
         name =  self.namespaceIdentifiers[nsid]
         name = name.replace("\"","\\\"")
-        code.append("UA_Server_addNamespace(server, \"" + name.encode('UTF-8') + "\");")
+        code.append("UA_Server_addNamespace(server, \"" + name + "\");")
 
     # Find all references necessary to create the namespace and
     # "Bootstrap" them so all other nodes can safely use these referencetypes whenever

+ 2 - 0
tools/pyUANamespace/ua_node_types.py

@@ -219,6 +219,8 @@ class opcua_node_id_t():
     return self.__mystrname__
 
   def __eq__(self, nodeId2):
+    if not nodeId2:
+        return False
     return (self.toString() == nodeId2.toString())
 
   def __repr__(self):