aboutsummaryrefslogtreecommitdiffstats
path: root/pyprofibus/gsd/fields.py
blob: 3b1845db453989ff889a1570e52ad0c42972903a (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
#
# 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.
#

import re

__all__ = [
	"GSD_STR",
	"gsdrepr",
	"PrmText",
	"PrmTextValue",
	"ExtUserPrmData",
	"ExtUserPrmDataConst",
	"ExtUserPrmDataRef",
	"Module",
]

GSD_STR = (
	"Prm_Text_Ref",
	"Ext_Module_Prm_Data_Len",
	"ExtUserPrmData",
	"Ext_User_Prm_Data_Const",
	"Ext_User_Prm_Data_Ref",
)

def gsdrepr(x):
	if isinstance(x, list):
		return "[%s]" % ", ".join(gsdrepr(a) for a in x)
	if isinstance(x, tuple):
		return "(%s, )" % ", ".join(gsdrepr(a) for a in x)
	if isinstance(x, dict):
		return "{%s}" % ", ".join("%s : %s\n" % (gsdrepr(a), gsdrepr(b))
					  for a, b in x.items())
	if isinstance(x, str):
		for i, cs in enumerate(GSD_STR):
			if cs == x:
				return "GSD_STR[%s]" % i
	return repr(x)

class _Item(object):
	"""Abstract item base class.
	"""

	__slots__ = (
		"fields",
	)

	def __init__(self, fields=None):
		self.fields = fields or {}

	def getField(self, name, default=None):
		"""Get a field by name.
		"""
		return self.fields.get(name, default)

	def _repr_field(self, pfx=", ", sfx=""):
		if self.fields:
			return "%sfields=%s%s" % (pfx, gsdrepr(self.fields), sfx)
		return ""

class PrmText(_Item):
	"""PrmText section.
	"""

	REPR_NAME = "PrmText"

	__slots__ = (
		"refNr",
		"texts",
	)

	def __init__(self, refNr, texts=None, **kwargs):
		_Item.__init__(self, **kwargs)
		self.refNr = refNr
		self.texts = texts or []

	def __repr__(self):
		return "%s(%s, %s%s)" % (
			self.REPR_NAME,
			gsdrepr(self.refNr),
			gsdrepr(self.texts),
			self._repr_field())

class PrmTextValue(_Item):
	"""PrmText text value.
	"""

	REPR_NAME = "PrmTextValue"

	__slots__ = (
		"offset",
		"text",
	)

	def __init__(self, offset, text, **kwargs):
		_Item.__init__(self, **kwargs)
		self.offset = offset
		self.text = text

	def __repr__(self):
		return "%s(%s, %s%s)" % (
			self.REPR_NAME,
			gsdrepr(self.offset),
			gsdrepr(self.text),
			self._repr_field())

class ExtUserPrmData(_Item):
	"""ExtUserPrmData section.
	"""

	REPR_NAME = "ExtUserPrmData"

	__slots__ = (
		"refNr",
		"name",
	)

	def __init__(self, refNr, name, **kwargs):
		_Item.__init__(self, **kwargs)
		self.refNr = refNr
		self.name = name

	def __repr__(self):
		return "%s(%s, %s%s)" % (
			self.REPR_NAME,
			gsdrepr(self.refNr),
			gsdrepr(self.name),
			self._repr_field())

class ExtUserPrmDataConst(_Item):
	"""Ext_User_Prm_Data_Const(x)
	"""

	REPR_NAME = "ExtUserPrmDataConst"

	__slots__ = (
		"offset",
		"dataBytes",
	)

	def __init__(self, offset, dataBytes, **kwargs):
		_Item.__init__(self, **kwargs)
		self.offset = offset
		self.dataBytes = dataBytes

	def __repr__(self):
		return "%s(%s, %s%s)" % (
			self.REPR_NAME,
			gsdrepr(self.offset),
			gsdrepr(self.dataBytes),
			self._repr_field())

class ExtUserPrmDataRef(_Item):
	"""Ext_User_Prm_Data_Ref(x)
	"""

	REPR_NAME = "ExtUserPrmDataRef"

	__slots__ = (
		"offset",
		"refNr",
	)

	def __init__(self, offset, refNr, **kwargs):
		_Item.__init__(self, **kwargs)
		self.offset = offset
		self.refNr = refNr

	def __repr__(self):
		return "%s(%s, %s%s)" % (
			self.REPR_NAME,
			gsdrepr(self.offset),
			gsdrepr(self.refNr),
			self._repr_field())

class Module(_Item):
	"""Module section.
	"""

	REPR_NAME = "Module"

	__slots__ = (
		"name",
		"configBytes",
	)

	@classmethod
	def sanitizeName(cls, name):
		return re.subn(r"\s+", " ", name)[0].strip()

	def __init__(self, name, configBytes, **kwargs):
		_Item.__init__(self, **kwargs)
		self.name = name
		self.configBytes = configBytes

	def __repr__(self):
		return "%s(%s, %s%s)" % (
			self.REPR_NAME,
			gsdrepr(self.name),
			gsdrepr(self.configBytes),
			self._repr_field())
bues.ch cgit interface