generate_namespace.py 2.1 KB

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