generate_statuscode_descriptions.py 2.3 KB

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