forked from FreeOpcUa/python-opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_statuscode.py
More file actions
62 lines (55 loc) · 2.32 KB
/
Copy pathgenerate_statuscode.py
File metadata and controls
62 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import datetime
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def status_codes():
"""
Load values from StatusCode.csv and then
add values from StatusCodes_add.csv, but only
if they are absent from StatusCode.csv
"""
with open(os.path.join(BASE_DIR, 'schemas', 'StatusCodes_add.csv')) as inputfile:
additional = {}
for line in inputfile:
name, val, doc = line.split(",", 2)
additional[int(val, 0)] = (name, val, doc)
with open(os.path.join(BASE_DIR, 'schemas', 'UA-Nodeset-master', 'Schema', 'StatusCode.csv')) as inputfile:
result = []
for line in inputfile:
name, val, doc = line.split(",", 2)
result.append((name, val, doc))
additional.pop(int(val, 0), None)
add = [additional[k] for k in sorted(additional.keys())]
return add + result
if __name__ == "__main__":
codes = status_codes()
with open(os.path.join(BASE_DIR, "opcua", "ua", "status_codes.py"), "w") as outputfile:
outputfile.write(f"#AUTOGENERATED!!! Date: {datetime.datetime.now()}\n")
outputfile.write("\n")
outputfile.write("from opcua.ua.uaerrors import UaStatusCodeError\n")
# outputfile.write("from enum import Enum\n")
outputfile.write("\n")
outputfile.write("class StatusCodes:\n")
for name, val, doc in codes:
doc = doc.strip()
outputfile.write(" {0} = {1}\n".format(name, val))
outputfile.write("\n")
outputfile.write("\n")
outputfile.write("code_to_name_doc = {\n")
for name, val, doc in codes:
doc = doc.strip()
doc = doc.replace("'", '"')
outputfile.write(" {0}: ('{1}', '{2}'),\n".format(val, name, doc))
outputfile.write("}\n")
outputfile.write("\n")
outputfile.write("\n")
outputfile.write("""def get_name_and_doc(val):
if val in code_to_name_doc:
return code_to_name_doc[val]
else:
if val & 1 << 31:
return 'Bad', 'Unknown StatusCode value: {}'.format(val)
elif val & 1 << 30:
return 'UncertainIn', 'Unknown StatusCode value: {}'.format(val)
else:
return 'Good', 'Unknown StatusCode value: {}'.format(val)
""")