Code Duplication    Length = 61-74 lines in 2 locations

opcua/ua/uaprotocol_auto.py 1 location

@@ 297-370 (lines=74) @@
294
    ReferenceType = 1371236
295
    View = 1335532
296
297
298
class AttributeWriteMask(IntEnum):
299
    '''
300
    Define bits used to indicate which attributes are writable.
301
302
    :ivar None_:
303
    :vartype None_: 0
304
    :ivar AccessLevel:
305
    :vartype AccessLevel: 1
306
    :ivar ArrayDimensions:
307
    :vartype ArrayDimensions: 2
308
    :ivar BrowseName:
309
    :vartype BrowseName: 4
310
    :ivar ContainsNoLoops:
311
    :vartype ContainsNoLoops: 8
312
    :ivar DataType:
313
    :vartype DataType: 16
314
    :ivar Description:
315
    :vartype Description: 32
316
    :ivar DisplayName:
317
    :vartype DisplayName: 64
318
    :ivar EventNotifier:
319
    :vartype EventNotifier: 128
320
    :ivar Executable:
321
    :vartype Executable: 256
322
    :ivar Historizing:
323
    :vartype Historizing: 512
324
    :ivar InverseName:
325
    :vartype InverseName: 1024
326
    :ivar IsAbstract:
327
    :vartype IsAbstract: 2048
328
    :ivar MinimumSamplingInterval:
329
    :vartype MinimumSamplingInterval: 4096
330
    :ivar NodeClass:
331
    :vartype NodeClass: 8192
332
    :ivar NodeId:
333
    :vartype NodeId: 16384
334
    :ivar Symmetric:
335
    :vartype Symmetric: 32768
336
    :ivar UserAccessLevel:
337
    :vartype UserAccessLevel: 65536
338
    :ivar UserExecutable:
339
    :vartype UserExecutable: 131072
340
    :ivar UserWriteMask:
341
    :vartype UserWriteMask: 262144
342
    :ivar ValueRank:
343
    :vartype ValueRank: 524288
344
    :ivar WriteMask:
345
    :vartype WriteMask: 1048576
346
    :ivar ValueForVariableType:
347
    :vartype ValueForVariableType: 2097152
348
    '''
349
    None_ = 0
350
    AccessLevel = 1
351
    ArrayDimensions = 2
352
    BrowseName = 4
353
    ContainsNoLoops = 8
354
    DataType = 16
355
    Description = 32
356
    DisplayName = 64
357
    EventNotifier = 128
358
    Executable = 256
359
    Historizing = 512
360
    InverseName = 1024
361
    IsAbstract = 2048
362
    MinimumSamplingInterval = 4096
363
    NodeClass = 8192
364
    NodeId = 16384
365
    Symmetric = 32768
366
    UserAccessLevel = 65536
367
    UserExecutable = 131072
368
    UserWriteMask = 262144
369
    ValueRank = 524288
370
    WriteMask = 1048576
371
    ValueForVariableType = 2097152
372
373

opcua/ua/uatypes.py 1 location

@@ 805-865 (lines=61) @@
802
        if val is None:
803
            return VariantType.Null
804
        elif isinstance(val, bool):
805
            return VariantType.Boolean
806
        elif isinstance(val, float):
807
            return VariantType.Double
808
        elif isinstance(val, int):
809
            return VariantType.Int64
810
        elif type(val) in (str, unicode):
811
            return VariantType.String
812
        elif isinstance(val, bytes):
813
            return VariantType.ByteString
814
        elif isinstance(val, datetime):
815
            return VariantType.DateTime
816
        else:
817
            if isinstance(val, object):
818
                try:
819
                    return getattr(VariantType, val.__class__.__name__)
820
                except AttributeError:
821
                    return VariantType.ExtensionObject
822
            else:
823
                raise UaError("Could not guess UA type of {} with type {}, specify UA type".format(val, type(val)))
824
825
    def __str__(self):
826
        return "Variant(val:{!s},type:{})".format(self.Value, self.VariantType)
827
    __repr__ = __str__
828
829
    def to_binary(self):
830
        b = []
831
        encoding = self.VariantType.value & 0b111111
832
        if type(self.Value) in (list, tuple):
833
            if self.Dimensions is not None:
834
                encoding = uabin.set_bit(encoding, 6)
835
            encoding = uabin.set_bit(encoding, 7)
836
            b.append(uabin.Primitives.UInt8.pack(encoding))
837
            b.append(uabin.pack_uatype_array(self.VariantType, flatten(self.Value)))
838
            if self.Dimensions is not None:
839
                b.append(uabin.pack_uatype_array(VariantType.Int32, self.Dimensions))
840
        else:
841
            b.append(uabin.Primitives.UInt8.pack(encoding))
842
            b.append(uabin.pack_uatype(self.VariantType, self.Value))
843
844
        return b"".join(b)
845
846
    @staticmethod
847
    def from_binary(data):
848
        dimensions = None
849
        encoding = ord(data.read(1))
850
        int_type = encoding & 0b00111111
851
        vtype = DataType_to_VariantType(int_type)
852
        if vtype == VariantType.Null:
853
            return Variant(None, vtype, encoding)
854
        if uabin.test_bit(encoding, 7):
855
            value = uabin.unpack_uatype_array(vtype, data)
856
        else:
857
            value = uabin.unpack_uatype(vtype, data)
858
        if uabin.test_bit(encoding, 6):
859
            dimensions = uabin.unpack_uatype_array(VariantType.Int32, data)
860
            value = reshape(value, dimensions)
861
862
        return Variant(value, vtype, dimensions)
863
864
865
def reshape(flat, dims):
866
    subdims = dims[1:]
867
    subsize = 1
868
    for i in subdims: