generate_statuscode_descriptions.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. from __future__ import print_function
  6. import sys
  7. import platform
  8. import getpass
  9. import time
  10. import argparse
  11. from io import open
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument('statuscodes', help='path/to/Opc.Ua.StatusCodes.csv')
  14. parser.add_argument('outfile', help='outfile w/o extension')
  15. args = parser.parse_args()
  16. rows = []
  17. with open(args.statuscodes, mode="rt") as f:
  18. lines = f.readlines()
  19. for l in lines:
  20. rows.append(tuple(l.strip().split(',')))
  21. fc = open(args.outfile + ".c", "wt", encoding='utf8')
  22. def printc(string):
  23. print(string, end=u'\n', file=fc)
  24. printc(u'''/**********************************************************
  25. * Autogenerated -- do not modify
  26. * Generated from %s with script %s
  27. *********************************************************/
  28. #include "ua_types.h"''' % (args.statuscodes, sys.argv[0]))
  29. count = 2 + len(rows)
  30. printc(u'''
  31. /* Definition for the deprecated StatusCode description API */
  32. const UA_StatusCodeDescription statusCodeExplanation_default = {0xffffffff, "", ""};
  33. typedef struct {
  34. UA_StatusCode code;
  35. const char *name;
  36. } UA_StatusCodeName;
  37. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  38. static const char * emptyStatusCodeName = "";
  39. const char * UA_StatusCode_name(UA_StatusCode code) {
  40. return emptyStatusCodeName;
  41. }
  42. #else
  43. static const size_t statusCodeDescriptionsSize = %s;
  44. static const UA_StatusCodeName statusCodeDescriptions[%i] = {
  45. {UA_STATUSCODE_GOOD, \"Good\"},
  46. ''' % (count, count))
  47. for row in rows:
  48. printc(u" {UA_STATUSCODE_%s, \"%s\",}," % (row[0].upper(), row[0]))
  49. printc(u''' {0xffffffff, "Unknown StatusCode"}
  50. };
  51. const char * UA_StatusCode_name(UA_StatusCode code) {
  52. for(size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  53. if(statusCodeDescriptions[i].code == code)
  54. return statusCodeDescriptions[i].name;
  55. }
  56. return statusCodeDescriptions[statusCodeDescriptionsSize-1].name;
  57. }
  58. #endif''')
  59. fc.close()