Bladeren bron

make generator-tools work with python3

Julius Pfrommer 6 jaren geleden
bovenliggende
commit
57eef24151
3 gewijzigde bestanden met toevoegingen van 24 en 24 verwijderingen
  1. 1 1
      tools/amalgamate.py
  2. 19 20
      tools/generate_statuscode_descriptions.py
  3. 4 3
      tools/hex2bin.py

+ 1 - 1
tools/amalgamate.py

@@ -29,7 +29,7 @@ guard_re = re.compile("^#(?:(?:ifndef|define)\s*[A-Z_]+_H_|endif /\* [A-Z_]+_H_
 
 print ("Starting amalgamating file "+ args.outfile)
 
-file = io.open(args.outfile, 'w', encoding='utf8', errors='replace')
+file = io.open(args.outfile, 'wt', encoding='utf8', errors='replace')
 file.write(u"""/* THIS IS A SINGLE-FILE DISTRIBUTION CONCATENATED FROM THE OPEN62541 SOURCES
  * visit http://open62541.org/ for information about this software
  * Git-Revision: %s

+ 19 - 20
tools/generate_statuscode_descriptions.py

@@ -10,35 +10,33 @@ import platform
 import getpass
 import time
 import argparse
+from io import open
 
 parser = argparse.ArgumentParser()
 parser.add_argument('statuscodes', help='path/to/Opc.Ua.StatusCodes.csv')
 parser.add_argument('outfile', help='outfile w/o extension')
 args = parser.parse_args()
 
-f = open(args.statuscodes)
-input_str = f.read()
-f.close()
-input_str = input_str.replace('\r','')
-rows = list(map(lambda x:tuple(x.split(',')), input_str.split('\n')))
+rows = []
+with open(args.statuscodes, mode="rt") as f:
+    lines = f.readlines()
+    for l in lines:
+        rows.append(tuple(l.strip().split(',')))
 
-fc = open(args.outfile + ".c",'w')
+fc = open(args.outfile + ".c", "wt", encoding='utf8')
 def printc(string):
-    print(string, end='\n', file=fc)
+    print(string, end=u'\n', file=fc)
 
-printc('''/**********************************************************
- * '''+args.outfile+'''.hgen -- do not modify
- **********************************************************
- * Generated from '''+args.statuscodes+''' with script '''+sys.argv[0]+'''
- * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+
-       time.strftime("%Y-%m-%d %I:%M:%S")+'''
- **********************************************************/\n
+printc(u'''/**********************************************************
+ * Autogenerated -- do not modify
+ * Generated from %s with script %s
+ *********************************************************/
 
-#include "ua_types.h"''')
+#include "ua_types.h"''' % (args.statuscodes, sys.argv[0]))
 
 count = 2 + len(rows)
 
-printc('''
+printc(u'''
 
 /* Definition for the deprecated StatusCode description API */
 const UA_StatusCodeDescription statusCodeExplanation_default = {0xffffffff, "", ""};
@@ -55,12 +53,13 @@ const char * UA_StatusCode_name(UA_StatusCode code) {
 }
 #else
 static const size_t statusCodeDescriptionsSize = %s;
-static const UA_StatusCodeName statusCodeDescriptions[%i] = {''' % (count, count))
+static const UA_StatusCodeName statusCodeDescriptions[%i] = {
+    {UA_STATUSCODE_GOOD, \"Good\"},
+''' % (count, count))
 
-printc("    {UA_STATUSCODE_GOOD, \"Good\"},")
 for row in rows:
-    printc("    {UA_STATUSCODE_%s, \"%s\",}," % (row[0].upper(), row[0]))
-printc('''    {0xffffffff, "Unknown StatusCode"}
+    printc(u"    {UA_STATUSCODE_%s, \"%s\",}," % (row[0].upper(), row[0]))
+printc(u'''    {0xffffffff, "Unknown StatusCode"}
 };
 
 const char * UA_StatusCode_name(UA_StatusCode code) {

+ 4 - 3
tools/hex2bin.py

@@ -6,12 +6,13 @@
 
 import sys
 import os
+from io import open
 import binascii
 import re
 
 def clean_line(string):
     comment_re = re.compile("/\*.*?\*/") # matches C-style comments /* */ at the end of a line
-    return re.sub(comment_re, "" ,string).replace(' ','').replace('\n','')
+    return re.sub(comment_re, "" ,string).replace(" ","").replace("\n","")
 
 if len(sys.argv) < 2:
     print("Usage: python hex2bin.py file1.hex file2.hex ...")
@@ -20,8 +21,8 @@ if len(sys.argv) < 2:
 filenames = sys.argv[1:]
 for f in filenames:
     bn = os.path.basename(f)
-    with open(f) as ff:
-        with open(bn[:-4] + ".bin", 'w') as out:
+    with open(f, mode="rt") as ff:
+        with open(bn[:-4] + ".bin", "wb") as out:
             lines = ff.readlines()
             for l in lines:
                 c = clean_line(l)