nodeset_compiler.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. ###
  7. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  8. ### Version: rev 14
  9. ###
  10. ### This program was created for educational purposes and has been
  11. ### contributed to the open62541 project by the author. All licensing
  12. ### terms for this source is inherited by the terms and conditions
  13. ### specified for by the open62541 project (see the projects readme
  14. ### file for more information on the MPLv2 terms and restrictions).
  15. ###
  16. ### This program is not meant to be used in a production environment. The
  17. ### author is not liable for any complications arising due to the use of
  18. ### this program.
  19. ###
  20. import logging
  21. import argparse
  22. from nodeset import *
  23. from backend_open62541 import generateOpen62541Code
  24. parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
  25. parser.add_argument('-e','--existing',
  26. metavar="<existingNodeSetXML>",
  27. type=argparse.FileType('r'),
  28. dest="existing",
  29. action='append',
  30. default=[],
  31. help='NodeSet XML files with nodes that are already present on the server.')
  32. parser.add_argument('infiles',
  33. metavar="<nodeSetXML>",
  34. action='append',
  35. type=argparse.FileType('r'),
  36. default=[],
  37. help='NodeSet XML files with nodes that shall be generated.')
  38. parser.add_argument('outputFile',
  39. metavar='<outputFile>',
  40. help='The path/basename for the <output file>.c and <output file>.h files to be generated. This will also be the function name used in the header and c-file.')
  41. parser.add_argument('--generate-ns0',
  42. action='store_true',
  43. dest="generate_ns0",
  44. help='Omit some consistency checks for bootstrapping namespace 0, create references to parents and type definitions manually')
  45. parser.add_argument('-b','--blacklist',
  46. metavar="<blacklistFile>",
  47. type=argparse.FileType('r'),
  48. action='append',
  49. dest="blacklistFiles",
  50. default=[],
  51. help='Loads a list of NodeIDs stored in blacklistFile (one NodeID per line). Any of the nodeIds encountered in this file will be removed from the nodeset prior to compilation. Any references to these nodes will also be removed')
  52. parser.add_argument('-s','--suppress',
  53. metavar="<attribute>",
  54. action='append',
  55. dest="suppressedAttributes",
  56. choices=['description', 'browseName', 'displayName', 'writeMask', 'userWriteMask','nodeid'],
  57. default=[],
  58. help="Suppresses the generation of some node attributes. Currently supported options are 'description', 'browseName', 'displayName', 'writeMask', 'userWriteMask' and 'nodeid'.")
  59. parser.add_argument('-v','--verbose', action='count', help='Make the script more verbose. Can be applied up to 4 times')
  60. args = parser.parse_args()
  61. # Set up logging
  62. logger = logging.getLogger(__name__)
  63. logger.setLevel(logging.INFO)
  64. verbosity = 0
  65. if args.verbose:
  66. verbosity = int(args.verbose)
  67. if (verbosity==1):
  68. logging.basicConfig(level=logging.ERROR)
  69. elif (verbosity==2):
  70. logging.basicConfig(level=logging.WARNING)
  71. elif (verbosity==3):
  72. logging.basicConfig(level=logging.INFO)
  73. elif (verbosity>=4):
  74. logging.basicConfig(level=logging.DEBUG)
  75. else:
  76. logging.basicConfig(level=logging.CRITICAL)
  77. # Create a new nodeset. The nodeset name is not significant.
  78. # Parse the XML files
  79. ns = NodeSet()
  80. for xmlfile in args.existing:
  81. logger.info("Preprocessing (existing) " + str(xmlfile.name))
  82. ns.addNodeSet(xmlfile, True)
  83. for xmlfile in args.infiles:
  84. logger.info("Preprocessing " + str(xmlfile.name))
  85. ns.addNodeSet(xmlfile)
  86. # # We need to notify the open62541 server of the namespaces used to be able to use i.e. ns=3
  87. # namespaceArrayNames = preProc.getUsedNamespaceArrayNames()
  88. # for key in namespaceArrayNames:
  89. # ns.addNamespace(key, namespaceArrayNames[key])
  90. # Remove blacklisted nodes from the nodeset
  91. # Doing this now ensures that unlinkable pointers will be cleanly removed
  92. # during sanitation.
  93. for blacklist in args.blacklistFiles:
  94. for line in blacklist.readlines():
  95. line = line.replace(" ","")
  96. id = line.replace("\n","")
  97. if ns.getNodeByIDString(id) == None:
  98. logger.info("Can't blacklist node, namespace does currently not contain a node with id " + str(id))
  99. else:
  100. ns.removeNodeById(line)
  101. blacklist.close()
  102. # Remove nodes that are not printable or contain parsing errors, such as
  103. # unresolvable or no references or invalid NodeIDs
  104. ns.sanitize()
  105. # Create the C code with the open62541 backend of the compiler
  106. logger.info("Generating Code")
  107. generateOpen62541Code(ns, args.outputFile, args.suppressedAttributes, args.generate_ns0)
  108. logger.info("NodeSet generation code successfully printed")