gsdparser 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. """
  3. #
  4. # PROFIBUS - GSD file parser
  5. #
  6. # Copyright (c) 2016 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(" -d|--debug Enable parser debugging (default off)")
  26. print(" -h|--help Show this help.")
  27. print("")
  28. print("Actions:")
  29. print(" -S|--summary Print a summary of the GSD file contents.")
  30. print(" (The default, if no action is specified)")
  31. def main():
  32. opt_debug = False
  33. actions = []
  34. try:
  35. (opts, args) = getopt.getopt(sys.argv[1:],
  36. "hdS",
  37. [ "help",
  38. "debug",
  39. "summary", ])
  40. except getopt.GetoptError as e:
  41. sys.stderr.write(str(e) + "\n")
  42. usage()
  43. return 1
  44. for (o, v) in opts:
  45. if o in ("-h", "--help"):
  46. usage()
  47. return 0
  48. if o in ("-d", "--debug"):
  49. opt_debug = True
  50. if o in ("-S", "--summary"):
  51. actions.append("summary")
  52. if len(args) != 1:
  53. usage()
  54. return 1
  55. gsdFile = args[0]
  56. if not actions:
  57. actions = [ "summary", ]
  58. try:
  59. interp = GsdInterp.fromFile(gsdFile, debug = opt_debug)
  60. for action in actions:
  61. if action == "summary":
  62. print(str(interp))
  63. else:
  64. assert(0)
  65. except GsdError as e:
  66. sys.stderr.write("ERROR: %s\n" % str(e))
  67. return 1
  68. except Exception as e:
  69. sys.stderr.write("Exception: %s\n" % str(e))
  70. return 1
  71. return 0
  72. if __name__ == "__main__":
  73. sys.exit(main())