aboutsummaryrefslogtreecommitdiffstats
path: root/gsdparser
blob: 7aaa145fcb35462de4fd3252ee5df876a0b4eea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""
#
# PROFIBUS - GSD file parser
#
# Copyright (c) 2016 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2,
# or (at your option) any later version.
#
"""

from __future__ import division, absolute_import, print_function, unicode_literals

import sys
from pyprofibus.gsd.interp import GsdInterp, GsdError

import sys
import getopt


def usage():
	print("GSD file parser")
	print("")
	print("Usage: gsdparser [OPTIONS] [ACTIONS] FILE.GSD")
	print("")
	print("FILE.GSD is the GSD file to parse.")
	print("")
	print("Options:")
	print(" -d|--debug          Enable parser debugging (default off)")
	print(" -h|--help           Show this help.")
	print("")
	print("Actions:")
	print(" -S|--summary        Print a summary of the GSD file contents.")
	print("                     (The default, if no action is specified)")

def main():
	opt_debug = False
	actions = []

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hdS",
			[ "help",
			  "debug",
			  "summary", ])
	except getopt.GetoptError as e:
		sys.stderr.write(str(e) + "\n")
		usage()
		return 1
	for (o, v) in opts:
		if o in ("-h", "--help"):
			usage()
			return 0
		if o in ("-d", "--debug"):
			opt_debug = True
		if o in ("-S", "--summary"):
			actions.append("summary")
	if len(args) != 1:
		usage()
		return 1
	gsdFile = args[0]
	if not actions:
		actions = [ "summary", ]

	try:
		interp = GsdInterp.fromFile(gsdFile, debug = opt_debug)
		for action in actions:
			if action == "summary":
				print(str(interp))
			else:
				assert(0)
	except GsdError as e:
		sys.stderr.write("ERROR: %s\n" % str(e))
		return 1
	except Exception as e:
		sys.stderr.write("Exception: %s\n" % str(e))
		return 1
	return 0

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface