generate_namespace.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import print_function
  2. import sys
  3. from collections import OrderedDict
  4. import re
  5. import csv
  6. from itertools import tee
  7. if len(sys.argv) != 3:
  8. print("Usage: python generate_namespace.py <path/to/NodeIds.csv> <outfile w/o extension>", file=sys.stdout)
  9. exit(0)
  10. # types that are to be excluded
  11. exclude_types = set(["Object","ObjectType","Variable","Method"])
  12. def skipType(name):
  13. if name in exclude_types:
  14. return True
  15. return False
  16. f = open(sys.argv[1])
  17. rows1, rows2, rows3 = tee(csv.reader(f), 3)
  18. fh = open(sys.argv[2] + ".h",'w');
  19. fc = open(sys.argv[2] + ".c",'w');
  20. print('''/* struktur vTable und enumerierung*/
  21. #ifndef OPCUA_NAMESPACE_0_H_
  22. #define OPCUA_NAMESPACE_0_H_
  23. #include "opcua.h" // definition of UA_VTable and basic UA_Types
  24. Int32 UA_namespace_zero_to_index(Int32 id);
  25. extern UA_VTable UA_namespace_zero[];
  26. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  27. print('''/* Mapping and vTable of Namespace Zero */
  28. #include "opcua.h"
  29. Int32 UA_namespace_zero_to_index(Int32 id) {
  30. Int32 retval = -1;
  31. switch (id) { ''', end='\n',file=fc)
  32. i = 0
  33. for row in rows1:
  34. if skipType(row[2]):
  35. continue
  36. name = "UA_" + row[0]
  37. print("\t"+name.upper()+"="+str(i)+",", file=fh)
  38. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  39. i = i+1
  40. print('\tUA_NS0_VTABLE_MAX = 0\n};\n', file=fh)
  41. print('''\t}\n\treturn retval;
  42. }
  43. UA_VTable UA_namespace_zero[] = {''', file=fc)
  44. for row in rows2:
  45. if skipType(row[2]):
  46. continue
  47. name = "UA_" + row[0]
  48. print('#define '+name.upper()+'_NS0 (UA_namespace_zero['+name.upper()+'].Id)', file=fh)
  49. print("\t{" + row[1] + ", &" + name + "_calcSize, &" + name + "_decode, &" + name + "_encode},",end='\n',file=fc)
  50. print("\t{0,NULL,NULL,NULL}\n};",file=fc)
  51. print('#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  52. fh.close()
  53. fc.close()
  54. f.close()