generate_namespace.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_kind = set(["Object","ObjectType","Variable","Method","ReferenceType"])
  13. exclude_types = set(["Number",
  14. "Integer", "UInteger", "Enumeration",
  15. "Image", "ImageBMP", "ImageGIF", "ImageJPG", "ImagePNG",
  16. "References", "BaseVariableType", "BaseDataVariableType",
  17. "PropertyType", "DataTypeDescriptionType", "DataTypeDictionaryType", "NamingRuleType",
  18. "IntegerId","Counter","Duration","NumericRange","Time","Date",
  19. "UtcTime", "LocaleId","UserTokenType",
  20. "ApplicationType","ApplicationInstanceCertificate",
  21. "ServerVendorCapabilityType","ServerStatusType","ServerDiagnosticsSummaryType",
  22. "SamplingIntervalDiagnosticsArrayType", "SamplingIntervalDiagnosticsType",
  23. "SubscriptionDiagnosticsArrayType", "SubscriptionDiagnosticsType",
  24. "SessionDiagnosticsArrayType", "SessionDiagnosticsVariableType",
  25. "SessionSecurityDiagnosticsArrayType", "SessionSecurityDiagnosticsType",
  26. "DataItemType", "AnalogItemType", "DiscreteItemType", "TwoStateDiscreteType",
  27. "MultiStateDiscreteType", "ProgramDiagnosticType", "StateVariableType", "FiniteStateVariableType",
  28. "TransitionVariableType", "FiniteTransitionVariableType", "BuildInfoType", "TwoStateVariableType",
  29. "ConditionVariableType", "MultiStateValueDiscreteType", "OptionSetType", "ArrayItemType",
  30. "YArrayItemType", "XYArrayItemType", "ImageItemType", "CubeItemType", "NDimensionArrayItemType"
  31. ])
  32. def skipKind(name):
  33. if name in exclude_kind:
  34. return True
  35. return False
  36. def skipType(name):
  37. if name in exclude_types:
  38. return True
  39. return False
  40. f = open(sys.argv[1])
  41. rows1, rows2, rows3 = tee(csv.reader(f), 3)
  42. fh = open(sys.argv[2] + ".hgen",'w');
  43. fc = open(sys.argv[2] + ".cgen",'w');
  44. print('''/**********************************************************
  45. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  46. * on node XXX by user XXX at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  47. * do not modify
  48. **********************************************************/
  49. #ifndef OPCUA_NAMESPACE_0_H_
  50. #define OPCUA_NAMESPACE_0_H_
  51. #include "opcua.h" // definition of UA_VTable and basic UA_Types
  52. UA_Int32 UA_toIndex(UA_Int32 id);
  53. extern UA_VTable UA_[];
  54. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  55. print('''/**********************************************************
  56. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  57. * on node XXX by user XXX at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  58. * do not modify
  59. **********************************************************/
  60. #include "opcua.h"
  61. UA_Int32 UA_toIndex(UA_Int32 id) {
  62. UA_Int32 retval = -1;
  63. switch (id) { ''', end='\n',file=fc)
  64. i = 0
  65. for row in rows1:
  66. if skipKind(row[2]):
  67. continue
  68. if skipType(row[0]):
  69. continue
  70. if row[0] == "BaseDataType":
  71. name = "UA_Variant"
  72. elif row[0] == "Structure":
  73. name = "UA_ExtensionObject"
  74. else:
  75. name = "UA_" + row[0]
  76. print("\t"+name.upper()+"="+str(i)+",", file=fh)
  77. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  78. i = i+1
  79. print('\tUA_NS0_VTABLE_MAX = 0\n};\n', file=fh)
  80. print('''\t}\n\treturn retval;
  81. }
  82. UA_VTable UA_[] = {''', file=fc)
  83. for row in rows2:
  84. if skipKind(row[2]):
  85. continue
  86. if skipType(row[0]):
  87. continue
  88. if row[0] == "BaseDataType":
  89. name = "UA_Variant"
  90. elif row[0] == "Structure":
  91. name = "UA_ExtensionObject"
  92. else:
  93. name = "UA_" + row[0]
  94. print('#define '+name.upper()+'_NS0 '+row[1], file=fh)
  95. print("\t{" + row[1] + ", (UA_Int32(*)(void const*)) " + name + "_calcSize, (UA_Int32(*)(UA_Byte const*,UA_Int32*,void*)) " + name + "_decode, (UA_Int32(*)(void const*,UA_Int32*,UA_Byte*))" + name + "_encode, (UA_Int32(*)(void **))" + name + "_new, (UA_Int32(*)(void *))" + name + "_delete},",end='\n',file=fc)
  96. print("\t{0,UA_NULL,UA_NULL,UA_NULL,UA_NULL,UA_NULL}\n};",file=fc)
  97. print('#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  98. fh.close()
  99. fc.close()
  100. f.close()