Repository navigation
Expand file tree
/
Copy pathvariable.py
More file actions
197 lines (160 loc) · 6.2 KB
/
Copy pathvariable.py
File metadata and controls
197 lines (160 loc) · 6.2 KB
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
import logging
try:
from collections.abc import Mapping
except ImportError:
from collections import Mapping
from . import objectdictionary
logger = logging.getLogger(__name__)
class Variable(object):
def __init__(self, od):
self.od = od
#: Description of this variable from Object Dictionary, overridable
self.name = od.name
if isinstance(od.parent, (objectdictionary.Record,
objectdictionary.Array)):
# Include the parent object's name for subentries
self.name = od.parent.name + "." + od.name
#: Holds a local, overridable copy of the Object Index
self.index = od.index
#: Holds a local, overridable copy of the Object Subindex
self.subindex = od.subindex
def get_data(self):
raise NotImplementedError("Variable is not readable")
def set_data(self, data):
raise NotImplementedError("Variable is not writable")
@property
def data(self):
"""Byte representation of the object as :class:`bytes`."""
return self.get_data()
@data.setter
def data(self, data):
self.set_data(data)
@property
def raw(self):
"""Raw representation of the object.
This table lists the translations between object dictionary data types
and Python native data types.
+---------------------------+----------------------------+
| Data type | Python type |
+===========================+============================+
| BOOLEAN | :class:`bool` |
+---------------------------+----------------------------+
| UNSIGNEDxx | :class:`int` |
+---------------------------+----------------------------+
| INTEGERxx | :class:`int` |
+---------------------------+----------------------------+
| REALxx | :class:`float` |
+---------------------------+----------------------------+
| VISIBLE_STRING | :class:`str` / |
| | ``unicode`` (Python 2) |
+---------------------------+----------------------------+
| UNICODE_STRING | :class:`str` / |
| | ``unicode`` (Python 2) |
+---------------------------+----------------------------+
| OCTET_STRING | :class:`bytes` |
+---------------------------+----------------------------+
| DOMAIN | :class:`bytes` |
+---------------------------+----------------------------+
Data types that this library does not handle yet must be read and
written as :class:`bytes`.
"""
value = self.od.decode_raw(self.data)
text = "Value of %s (0x%X:%d) is %r" % (
self.name, self.index,
self.subindex, value)
if value in self.od.value_descriptions:
text += " (%s)" % self.od.value_descriptions[value]
logger.debug(text)
return value
@raw.setter
def raw(self, value):
logger.debug("Writing %s (0x%X:%d) = %r",
self.name, self.index,
self.subindex, value)
self.data = self.od.encode_raw(value)
@property
def phys(self):
"""Physical value scaled with some factor (defaults to 1).
On object dictionaries that support specifying a factor, this can be
either a :class:`float` or an :class:`int`.
Non integers will be passed as is.
"""
value = self.od.decode_phys(self.raw)
if self.od.unit:
logger.debug("Physical value is %s %s", value, self.od.unit)
return value
@phys.setter
def phys(self, value):
self.raw = self.od.encode_phys(value)
@property
def desc(self):
"""Converts to and from a description of the value as a string."""
value = self.od.decode_desc(self.raw)
logger.debug("Description is '%s'", value)
return value
@desc.setter
def desc(self, desc):
self.raw = self.od.encode_desc(desc)
@property
def bits(self):
"""Access bits using integers, slices, or bit descriptions."""
return Bits(self)
def read(self, fmt="raw"):
"""Alternative way of reading using a function instead of attributes.
May be useful for asynchronous reading.
:param str fmt:
How to return the value
- 'raw'
- 'phys'
- 'desc'
:returns:
The value of the variable.
"""
if fmt == "raw":
return self.raw
elif fmt == "phys":
return self.phys
elif fmt == "desc":
return self.desc
def write(self, value, fmt="raw"):
"""Alternative way of writing using a function instead of attributes.
May be useful for asynchronous writing.
:param str fmt:
How to write the value
- 'raw'
- 'phys'
- 'desc'
"""
if fmt == "raw":
self.raw = value
elif fmt == "phys":
self.phys = value
elif fmt == "desc":
self.desc = value
class Bits(Mapping):
def __init__(self, variable):
self.variable = variable
self.read()
@staticmethod
def _get_bits(key):
if isinstance(key, slice):
bits = range(key.start, key.stop, key.step)
elif isinstance(key, int):
bits = [key]
else:
bits = key
return bits
def __getitem__(self, key):
return self.variable.od.decode_bits(self.raw, self._get_bits(key))
def __setitem__(self, key, value):
self.raw = self.variable.od.encode_bits(
self.raw, self._get_bits(key), value)
self.write()
def __iter__(self):
return iter(self.variable.od.bit_definitions)
def __len__(self):
return len(self.variable.od.bit_definitions)
def read(self):
self.raw = self.variable.raw
def write(self):
self.variable.raw = self.raw