gsdparser 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python3
  2. """
  3. #
  4. # PROFIBUS - GSD file parser
  5. #
  6. # Copyright (c) 2016-2020 Michael Buesch <m@bues.ch>
  7. #
  8. # Licensed under the terms of the GNU General Public License version 2,
  9. # or (at your option) any later version.
  10. #
  11. """
  12. from __future__ import division, absolute_import, print_function, unicode_literals
  13. import sys
  14. from pyprofibus.gsd.interp import GsdInterp, GsdError
  15. import sys
  16. import getopt
  17. def usage():
  18. print("GSD file parser")
  19. print("")
  20. print("Usage: gsdparser [OPTIONS] [ACTIONS] FILE.GSD")
  21. print("")
  22. print("FILE.GSD is the GSD file to parse.")
  23. print("")
  24. print("Options:")
  25. print(" -o|--output FILE Write output to FILE instead of stdout.")
  26. print(" -d|--debug Enable parser debugging (default off).")
  27. print(" -h|--help Show this help.")
  28. print("")
  29. print("Actions:")
  30. print(" -S|--summary Print a summary of the GSD file contents.")
  31. print(" (The default, if no action is specified)")
  32. print(" -D|--dump Dump the GSD data structure as Python code.")
  33. print("")
  34. print("Options for --dump:")
  35. print(" --dump-strip Strip leading and trailing whitespace from strings.")
  36. print(" --dump-notext Do not dump PrmText.")
  37. print(" --dump-noextuserprmdata Discard all ExtUserPrmData and ExtUserPrmDataRef.")
  38. print(" --dump-module NAME Only dump this module. (default: Dump all)")
  39. print(" Can be specified more then once to dump multiple modules.")
  40. def out(fd, text):
  41. fd.write(text)
  42. fd.flush()
  43. def main():
  44. opt_output = None
  45. opt_debug = False
  46. opt_dumpStrip = False
  47. opt_dumpNoText = False
  48. opt_dumpNoExtUserPrmData = False
  49. opt_dumpModules = []
  50. actions = []
  51. try:
  52. (opts, args) = getopt.getopt(sys.argv[1:],
  53. "ho:dSD",
  54. [ "help",
  55. "output=",
  56. "debug",
  57. "summary",
  58. "dump",
  59. "dump-strip",
  60. "dump-notext",
  61. "dump-noextuserprmdata",
  62. "dump-module=", ])
  63. except getopt.GetoptError as e:
  64. sys.stderr.write(str(e) + "\n")
  65. usage()
  66. return 1
  67. for (o, v) in opts:
  68. if o in ("-h", "--help"):
  69. usage()
  70. return 0
  71. if o in ("-o", "--output"):
  72. opt_output = v
  73. if o in ("-d", "--debug"):
  74. opt_debug = True
  75. if o in ("-S", "--summary"):
  76. actions.append( ("summary", None) )
  77. if o in ("-D", "--dump"):
  78. actions.append( ("dump", None) )
  79. if o in ("--dump-strip", ):
  80. opt_dumpStrip = True
  81. if o in ("--dump-notext", ):
  82. opt_dumpNoText = True
  83. if o in ("--dump-noextuserprmdata", ):
  84. opt_dumpNoExtUserPrmData = True
  85. if o in ("--dump-module", ):
  86. opt_dumpModules.append(v)
  87. if len(args) != 1:
  88. usage()
  89. return 1
  90. gsdFile = args[0]
  91. if not actions:
  92. actions = [ ("summary", None), ]
  93. try:
  94. if opt_output is None:
  95. outFd = sys.stdout
  96. else:
  97. outFd = open(opt_output, "w", encoding="UTF-8")
  98. except OSError as e:
  99. sys.stderr.write("ERROR: %s\n" % str(e))
  100. return 1
  101. try:
  102. interp = GsdInterp.fromFile(gsdFile, debug=opt_debug)
  103. for action, v in actions:
  104. if action == "summary":
  105. out(outFd, str(interp))
  106. elif action == "dump":
  107. py = interp.dumpPy(stripStr=opt_dumpStrip,
  108. noText=opt_dumpNoText,
  109. noExtUserPrmData=opt_dumpNoExtUserPrmData,
  110. modules=(opt_dumpModules or None))
  111. out(outFd, py)
  112. else:
  113. assert(0)
  114. except GsdError as e:
  115. sys.stderr.write("ERROR: %s\n" % str(e))
  116. return 1
  117. except Exception as e:
  118. sys.stderr.write("Exception: %s\n" % str(e))
  119. return 1
  120. finally:
  121. if opt_output is not None:
  122. outFd.flush()
  123. outFd.close()
  124. return 0
  125. if __name__ == "__main__":
  126. sys.exit(main())