generate_statuscode_descriptions.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from __future__ import print_function
  2. import sys
  3. import platform
  4. import getpass
  5. import time
  6. import argparse
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('statuscodes', help='path/to/Opc.Ua.StatusCodes.csv')
  9. parser.add_argument('outfile', help='outfile w/o extension')
  10. args = parser.parse_args()
  11. f = open(args.statuscodes)
  12. input_str = f.read()
  13. f.close()
  14. input_str = input_str.replace('\r','')
  15. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  16. fc = open(args.outfile + ".c",'w')
  17. def printc(string):
  18. print(string, end='\n', file=fc)
  19. printc('''/**********************************************************
  20. * '''+args.outfile+'''.hgen -- do not modify
  21. **********************************************************
  22. * Generated from '''+args.statuscodes+''' with script '''+sys.argv[0]+'''
  23. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+
  24. time.strftime("%Y-%m-%d %I:%M:%S")+'''
  25. **********************************************************/\n
  26. #include "ua_types.h"''')
  27. count = 2
  28. for row in rows:
  29. count += 1
  30. printc('''
  31. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  32. static const size_t statusCodeDescriptionsSize = 1;
  33. static const UA_StatusCodeDescription statusCodeDescriptions[1] = {
  34. {0xffffffff, \"StatusCode descriptions not available\", \"open62541 was compiled without support for statuscode descriptions\"}
  35. };
  36. #else
  37. static const size_t statusCodeDescriptionsSize = %s;
  38. static const UA_StatusCodeDescription statusCodeDescriptions[%i] =
  39. {''' % (count, count))
  40. printc(" {UA_STATUSCODE_GOOD, \"Good\", \"Success / No error\"},")
  41. for row in rows:
  42. printc(" {UA_STATUSCODE_%s, \"%s\", \"%s\"}," % (row[0].upper(), row[0], row[2]))
  43. printc(" {0xffffffff, \"Unknown\", \"Unknown StatusCode\"},")
  44. printc('''\n};
  45. #endif''')
  46. printc('''
  47. const UA_StatusCodeDescription * UA_StatusCode_description(UA_StatusCode code) {
  48. for(size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  49. if(statusCodeDescriptions[i].code == code)
  50. return &statusCodeDescriptions[i];
  51. }
  52. return &statusCodeDescriptions[statusCodeDescriptionsSize-1];
  53. }
  54. ''')
  55. fc.close()