generate_open62541CCode.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env/python
  2. # -*- coding: utf-8 -*-
  3. ###
  4. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  5. ### Version: rev 13
  6. ###
  7. ### This program was created for educational purposes and is released into the
  8. ### public domain under the General Public Licence. A copy of the GNU GPL is
  9. ### available under http://www.gnu.org/licenses/gpl-3.0.html.
  10. ###
  11. ### This program is not meant to be used in a production environment. The
  12. ### author is not liable for any complications arising due to the use of
  13. ### this program.
  14. ###
  15. from sys import argv, exit
  16. from os import path
  17. from ua_namespace import *
  18. def usage():
  19. print "Skript usage:"
  20. print "generate_open62541CCode <namespace XML> [namespace.xml[ namespace.xml[...]]] <output file>"
  21. print ""
  22. print "generate_open62541CCode will first read all XML files passed on the command line, then "
  23. print "link and check the namespace. All nodes that fullfill the basic requirements will then be"
  24. print "printed as C-Code intended to be included in the open62541 OPC-UA Server that will"
  25. print "initialize the corresponding name space."
  26. if __name__ == '__main__':
  27. # Check if the parameters given correspond to actual files
  28. infiles = []
  29. ouffile = ""
  30. if len(argv) < 2:
  31. usage()
  32. exit(1)
  33. for filename in argv[1:-1]:
  34. if path.exists(filename):
  35. infiles.append(filename)
  36. else:
  37. print "File " + str(filename) + " does not exist."
  38. usage()
  39. exit(1)
  40. # Creating the header is tendious. We can skip the entire process if
  41. # the header exists.
  42. # if path.exists(argv[-1]):
  43. # print "Header " + str(argv[-1]) + " already exists."
  44. # print "Header generation is a lengthy process, please delete the header"
  45. # print " if you really want namespace 0 to be recompiled from the XML"
  46. # print " file."
  47. # exit(0)
  48. # Open the output file
  49. outfile = open(argv[-1], r"w+")
  50. # Create a new namespace
  51. # Note that the name is actually completely symbolic, it has no other
  52. # function but to distinguish this specific class.
  53. # A namespace class acts as a container for nodes. The nodes may belong
  54. # to any number of different OPC-UA namespaces.
  55. ns = opcua_namespace("open62541")
  56. # Parse the XML files
  57. for xmlfile in infiles:
  58. print "Parsing " + xmlfile
  59. ns.parseXML(xmlfile)
  60. # Link the references in the namespace
  61. ns.linkOpenPointers()
  62. # Remove nodes that are not printable or contain parsing errors, such as
  63. # unresolvable or no references or invalid NodeIDs
  64. ns.sanitize()
  65. # Parse Datatypes in order to find out what the XML keyed values actually
  66. # represent.
  67. # Ex. <rpm>123</rpm> is not encodable
  68. # only after parsing the datatypes, it is known that
  69. # rpm is encoded as a double
  70. ns.buildEncodingRules()
  71. # Allocate/Parse the data values. In order to do this, we must have run
  72. # buidEncodingRules.
  73. ns.allocateVariables()
  74. # Create the C Code
  75. for line in ns.printOpen62541Header():
  76. outfile.write(line+"\n")
  77. outfile.close()