Completed
Push — dev ( 3b343f...d958a8 )
by Olivier
04:35 queued 02:25
created

opcua.ObjectAttributes   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
wmc 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
1 1
import struct
2 1
import logging
3
4 1
import opcua.uaprotocol_auto as auto
5 1
import opcua.uatypes as uatypes
6 1
import opcua.utils as utils
7 1
from opcua.object_ids import ObjectIds
8 1
from opcua.attribute_ids import AttributeIds
9
10 1
logger = logging.getLogger('opcua.uaprotocol')
11
12 1
OPC_TCP_SCHEME = 'opc.tcp'
13
14 1
class AccessLevelMask(object):
15
    """
16
    used by AccessLevel and UserAccessLevel
17
    """
18 1
    CurrentRead = 0
19 1
    CurrentWrite = 1
20 1
    HistoryRead = 2
21 1
    HistoryWrite = 3
22 1
    SemanticChange = 4
23
24
25 1
class Hello(uatypes.FrozenClass):
26
27 1
    def __init__(self):
28 1
        self.ProtocolVersion = 0
29 1
        self.ReceiveBufferSize = 65536
30 1
        self.SendBufferSize = 65536
31 1
        self.MaxMessageSize = 0
32 1
        self.MaxChunkCount = 0
33 1
        self.EndpointUrl = ""
34 1
        self._freeze()
35
36 1
    def to_binary(self):
37 1
        b = []
38 1
        b.append(struct.pack("<I", self.ProtocolVersion))
39 1
        b.append(struct.pack("<I", self.ReceiveBufferSize))
40 1
        b.append(struct.pack("<I", self.SendBufferSize))
41 1
        b.append(struct.pack("<I", self.MaxMessageSize))
42 1
        b.append(struct.pack("<I", self.MaxChunkCount))
43 1
        b.append(uatypes.pack_string(self.EndpointUrl))
44 1
        return b"".join(b)
45
46 1
    @staticmethod
47
    def from_binary(data):
48 1
        hello = Hello()
49 1
        hello.ProtocolVersion = struct.unpack("<I", data.read(4))[0]
50 1
        hello.ReceiveBufferSize = struct.unpack("<I", data.read(4))[0]
51 1
        hello.SendBufferSize = struct.unpack("<I", data.read(4))[0]
52 1
        hello.MaxMessageSize = struct.unpack("<I", data.read(4))[0]
53 1
        hello.MaxChunkCount = struct.unpack("<I", data.read(4))[0]
54 1
        hello.EndpointUrl = uatypes.unpack_string(data)
55 1
        return hello
56
57
58 1
class MessageType(object):
59 1
    Invalid = b"INV"  # FIXME: check value
60 1
    Hello = b"HEL"
61 1
    Acknowledge = b"ACK"
62 1
    Error = b"ERR"
63 1
    SecureOpen = b"OPN"
64 1
    SecureClose = b"CLO"
65 1
    SecureMessage = b"MSG"
66
67
68 1
class ChunkType(object):
69 1
    Invalid = b"0"  # FIXME check
70 1
    Single = b"F"
71 1
    Intermediate = b"C"
72 1
    Final = b"A"
73
74
75 1
class Header(uatypes.FrozenClass):
76
77 1
    def __init__(self, msgType=None, chunkType=None, channelid=0):
78 1
        self.MessageType = msgType
79 1
        self.ChunkType = chunkType
80 1
        self.ChannelId = channelid
81 1
        self.body_size = 0
82 1
        self.packet_size = 0
83 1
        self._freeze()
84
85 1
    def add_size(self, size):
86 1
        self.body_size += size
87
88 1
    def to_binary(self):
89 1
        b = []
90 1
        b.append(struct.pack("<3s", self.MessageType))
91 1
        b.append(struct.pack("<s", self.ChunkType))
92 1
        size = self.body_size + 8
93 1
        if self.MessageType in (MessageType.SecureOpen, MessageType.SecureClose, MessageType.SecureMessage):
94 1
            size += 4
95 1
        b.append(struct.pack("<I", size))
96 1
        if self.MessageType in (MessageType.SecureOpen, MessageType.SecureClose, MessageType.SecureMessage):
97 1
            b.append(struct.pack("<I", self.ChannelId))
98 1
        return b"".join(b)
99
100 1
    @staticmethod
101
    def from_string(data):
102 1
        hdr = Header()
103 1
        hdr.MessageType = struct.unpack("<3s", data.read(3))[0]
104 1
        hdr.ChunkType = struct.unpack("<c", data.read(1))[0]
105 1
        hdr.packet_size = struct.unpack("<I", data.read(4))[0]
106 1
        hdr.body_size =  hdr.packet_size - 8
107 1
        if hdr.MessageType in (MessageType.SecureOpen, MessageType.SecureClose, MessageType.SecureMessage):
108 1
            hdr.body_size -= 4
109 1
            hdr.ChannelId = struct.unpack("<I", data.read(4))[0]
110 1
        return hdr
111
112 1
    def __str__(self):
113
        return "Header(type:{}, chunk_type:{}, body_size:{}, channel:{})".format(self.MessageType, self.ChunkType, self.body_size, self.ChannelId)
114 1
    __repr__ = __str__
115
116
117 1
class ErrorMessage(uatypes.FrozenClass):
118
119 1
    def __init__(self):
120
        self.Error = uatypes.StatusCode()
121
        self.Reason = ""
122
        self._freeze()
123
124 1
    def to_binary(self):
125
        b = []
126
        b.append(self.Error.to_binary())
127
        b.append(uatypes.pack_string(self.Reason))
128
        return b"".join(b)
129
130 1
    @staticmethod
131
    def from_binary(data):
132
        ack = ErrorMessage()
133
        ack.Error = uatypes.StatusCode.from_binary(data)
134
        ack.Reason = uatypes.unpack_string(data)
135
        return ack
136
137 1
    def __str__(self):
138
        return "MessageAbort(error:{}, reason:{})".format(self.Error, self.Reason)
139 1
    __repr__ = __str__
140
141
142 1
class Acknowledge(uatypes.FrozenClass):
143
144 1
    def __init__(self):
145 1
        self.ProtocolVersion = 0
146 1
        self.ReceiveBufferSize = 65536
147 1
        self.SendBufferSize = 65536
148 1
        self.MaxMessageSize = 0  # No limits
149 1
        self.MaxChunkCount = 0  # No limits
150 1
        self._freeze()
151
152 1
    def to_binary(self):
153 1
        b = []
154 1
        b.append(struct.pack("<I", self.ProtocolVersion))
155 1
        b.append(struct.pack("<I", self.ReceiveBufferSize))
156 1
        b.append(struct.pack("<I", self.SendBufferSize))
157 1
        b.append(struct.pack("<I", self.MaxMessageSize))
158 1
        b.append(struct.pack("<I", self.MaxChunkCount))
159 1
        return b"".join(b)
160
161 1
    @staticmethod
162
    def from_binary(data):
163 1
        ack = Acknowledge()
164 1
        ack.ProtocolVersion = struct.unpack("<I", data.read(4))[0]
165 1
        ack.ReceiveBufferSize = struct.unpack("<I", data.read(4))[0]
166 1
        ack.SendBufferSize = struct.unpack("<I", data.read(4))[0]
167 1
        ack.MaxMessageSize = struct.unpack("<I", data.read(4))[0]
168 1
        ack.MaxChunkCount = struct.unpack("<I", data.read(4))[0]
169 1
        return ack
170
171
172 1
class AsymmetricAlgorithmHeader(uatypes.FrozenClass):
173
174 1
    def __init__(self):
175 1
        self.SecurityPolicyURI = "http://opcfoundation.org/UA/SecurityPolicy#None"
176 1
        self.SenderCertificate = b""
177 1
        self.ReceiverCertificateThumbPrint = b""
178 1
        self._freeze()
179
180 1
    def to_binary(self):
181 1
        b = []
182 1
        b.append(uatypes.pack_string(self.SecurityPolicyURI))
183 1
        b.append(uatypes.pack_string(self.SenderCertificate))
184 1
        b.append(uatypes.pack_string(self.ReceiverCertificateThumbPrint))
185 1
        return b"".join(b)
186
187 1
    @staticmethod
188
    def from_binary(data):
189 1
        hdr = AsymmetricAlgorithmHeader()
190 1
        hdr.SecurityPolicyURI = uatypes.unpack_bytes(data)
191 1
        hdr.SenderCertificate = uatypes.unpack_bytes(data)
192 1
        hdr.ReceiverCertificateThumbPrint = uatypes.unpack_bytes(data)
193 1
        return hdr
194
195 1
    def __str__(self):
196
        return "{}(SecurytyPolicy:{}, certificatesize:{}, receiverCertificatesize:{} )".format(self.__class__.__name__, self.SecurityPolicyURI, len(self.SenderCertificate), len(self.ReceiverCertificateThumbPrint))
197 1
    __repr__ = __str__
198
199
200 1
class SymmetricAlgorithmHeader(uatypes.FrozenClass):
201
202 1
    def __init__(self):
203 1
        self.TokenId = 0
204 1
        self._freeze()
205
206 1
    @staticmethod
207
    def from_binary(data):
208 1
        obj = SymmetricAlgorithmHeader()
209 1
        obj.TokenId = struct.unpack("<I", data.read(4))[0]
210 1
        return obj
211
212 1
    def to_binary(self):
213 1
        return struct.pack("<I", self.TokenId)
214
215 1
    def __str__(self):
216
        return "{}(TokenId:{} )".format(self.__class__.__name__, self.TokenId)
217 1
    __repr__ = __str__
218
219
220 1
class SequenceHeader(uatypes.FrozenClass):
221
222 1
    def __init__(self):
223 1
        self.SequenceNumber = None
224 1
        self.RequestId = None
225 1
        self._freeze()
226
227 1
    @staticmethod
228
    def from_binary(data):
229 1
        obj = SequenceHeader()
230 1
        obj.SequenceNumber = struct.unpack("<I", data.read(4))[0]
231 1
        obj.RequestId = struct.unpack("<I", data.read(4))[0]
232 1
        return obj
233
234 1
    def to_binary(self):
235 1
        b = []
236 1
        b.append(struct.pack("<I", self.SequenceNumber))
237 1
        b.append(struct.pack("<I", self.RequestId))
238 1
        return b"".join(b)
239
240 1
    def __str__(self):
241
        return "{}(SequenceNumber:{}, RequestId:{} )".format(self.__class__.__name__, self.SequenceNumber, self.RequestId)
242 1
    __repr__ = __str__
243
244
# FIXES for missing switchfield in NodeAttributes classes
245 1
ana = auto.NodeAttributesMask
246
247
248 1
class ObjectAttributes(auto.ObjectAttributes):
249
250 1
    def __init__(self):
251 1
        auto.ObjectAttributes.__init__(self)
252 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.EventNotifier
253
254
255 1
class ObjectTypeAttributes(auto.ObjectTypeAttributes):
256
257 1
    def __init__(self):
258 1
        auto.ObjectTypeAttributes.__init__(self)
259 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.IsAbstract
260
261
262 1
class VariableAttributes(auto.VariableAttributes):
263
264 1
    def __init__(self):
265 1
        auto.VariableAttributes.__init__(self)
266 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.Value | ana.DataType | ana.ValueRank | ana.ArrayDimensions | ana.AccessLevel | ana.UserAccessLevel | ana.MinimumSamplingInterval | ana.Historizing
267 1
        self.Historizing = False
268
269
270 1
class VariableTypeAttributes(auto.VariableTypeAttributes):
271
272 1
    def __init__(self):
273 1
        auto.VariableTypeAttributes.__init__(self)
274 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.Value | ana.DataType | ana.ValueRank | ana.ArrayDimensions | ana.IsAbstract
275
276
277 1
class MethodAttributes(auto.MethodAttributes):
278
279 1
    def __init__(self):
280 1
        auto.MethodAttributes.__init__(self)
281 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.Executable | ana.UserExecutable
282
283
284 1
class ReferenceTypeAttributes(auto.ReferenceTypeAttributes):
285
286 1
    def __init__(self):
287 1
        auto.ReferenceTypeAttributes.__init__(self)
288 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.IsAbstract | ana.Symmetric | ana.InverseName
289
290
291 1
class DataTypeAttributes(auto.DataTypeAttributes):
292
293 1
    def __init__(self):
294 1
        auto.DataTypeAttributes.__init__(self)
295 1
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.IsAbstract
296
297
298 1
class ViewAttributes(auto.ViewAttributes):
299
300 1
    def __init__(self):
301
        auto.ViewAttributes.__init__(self)
302
        self.SpecifiedAttributes = ana.DisplayName | ana.Description | ana.WriteMask | ana.UserWriteMask | ana.ContainsNoLoops | ana.EventNotifier
303
304
305 1
class Argument(auto.Argument):
306
307 1
    def __init__(self):
308 1
        auto.Argument.__init__(self)
309 1
        self.ValueRank = -2
310
311
312
AttributeIdsInv = {v: k for k, v in AttributeIds.__dict__.items()}
313