gsdparser 1.7 KB

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