summaryrefslogtreecommitdiffstats
path: root/gsdparser
blob: 6b094fdbd32d2dc70e7a8d3d943f04133c2a997e (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
"""
#
# PROFIBUS - GSD file parser
#
# Copyright (c) 2016-2020 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(" -o|--output FILE         Write output to FILE instead of stdout.")
	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)")
	print(" -D|--dump                Dump the GSD data structure as Python code.")
	print("")
	print("Options for --dump:")
	print(" --dump-strip             Strip leading and trailing whitespace from strings.")
	print(" --dump-notext            Do not dump PrmText.")
	print(" --dump-noextuserprmdata  Discard all ExtUserPrmData and ExtUserPrmDataRef.")
	print(" --dump-module NAME       Only dump this module. (default: Dump all)")
	print("                          Can be specified more then once to dump multiple modules.")

def out(fd, text):
	fd.write(text)
	fd.flush()

def main():
	opt_output = None
	opt_debug = False
	opt_dumpStrip = False
	opt_dumpNoText = False
	opt_dumpNoExtUserPrmData = False
	opt_dumpModules = []
	actions = []

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"ho:dSD",
			[ "help",
			  "output=",
			  "debug",
			  "summary",
			  "dump",
			  "dump-strip",
			  "dump-notext",
			  "dump-noextuserprmdata",
			  "dump-module=", ])
	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 ("-o", "--output"):
			opt_output = v
		if o in ("-d", "--debug"):
			opt_debug = True
		if o in ("-S", "--summary"):
			actions.append( ("summary", None) )
		if o in ("-D", "--dump"):
			actions.append( ("dump", None) )
		if o in ("--dump-strip", ):
			opt_dumpStrip = True
		if o in ("--dump-notext", ):
			opt_dumpNoText = True
		if o in ("--dump-noextuserprmdata", ):
			opt_dumpNoExtUserPrmData = True
		if o in ("--dump-module", ):
			opt_dumpModules.append(v)
	if len(args) != 1:
		usage()
		return 1
	gsdFile = args[0]
	if not actions:
		actions = [ ("summary", None), ]

	try:
		if opt_output is None:
			outFd = sys.stdout
		else:
			outFd = open(opt_output, "w", encoding="UTF-8")
	except OSError as e:
		sys.stderr.write("ERROR: %s\n" % str(e))
		return 1
	try:

		interp = GsdInterp.fromFile(gsdFile, debug=opt_debug)
		for action, v in actions:
			if action == "summary":
				out(outFd, str(interp))
			elif action == "dump":
				py = interp.dumpPy(stripStr=opt_dumpStrip,
						   noText=opt_dumpNoText,
						   noExtUserPrmData=opt_dumpNoExtUserPrmData,
						   modules=(opt_dumpModules or None))
				out(outFd, py)
			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
	finally:
		if opt_output is not None:
			outFd.flush()
			outFd.close()
	return 0

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