Code Duplication    Length = 22-44 lines in 4 locations

pyof/foundation/base.py 1 location

@@ 133-176 (lines=44) @@
130
        if self.isenum():
131
            if isinstance(self._value, self.enum_ref):
132
                return self._value.value
133
            return self._value
134
        elif self.is_bitmask():
135
            return self._value.bitmask
136
        else:
137
            return self._value
138
139
    def pack(self, value=None):
140
        r"""Pack the value as a binary representation.
141
142
        Considering an example with UBInt8 class, that inherits from
143
        GenericType:
144
145
        >>> from pyof.foundation.basic_types import UBInt8
146
        >>> objectA = UBInt8(1)
147
        >>> objectB = 5
148
        >>> objectA.pack()
149
        b'\x01'
150
        >>> objectA.pack(objectB)
151
        b'\x05'
152
153
        Args:
154
            value: If the value is None, then we will pack the value of the
155
                current instance. Otherwise, if value is an instance of the
156
                same type as the current instance, then we call the pack of the
157
                value object. Otherwise, we will use the current instance pack
158
                method on the passed value.
159
160
        Returns:
161
            bytes: The binary representation.
162
163
        Raises:
164
            :exc:`~.exceptions.BadValueException`: If the value does not
165
                fit the binary format.
166
        """
167
        if isinstance(value, type(self)):
168
            return value.pack()
169
170
        if value is None:
171
            value = self.value
172
        elif 'value' in dir(value):
173
            # if it is enum or bitmask gets only the 'int' value
174
            value = value.value
175
176
        try:
177
            return struct.pack(self._fmt, value)
178
        except struct.error:
179
            msg = '{} could not pack {} = {}.'.format(type(self).__name__,

pyof/foundation/basic_types.py 3 locations

@@ 331-360 (lines=30) @@
328
        """
329
        super().__init__(hw_address)
330
331
    def pack(self, value=None):
332
        """Pack the value as a binary representation.
333
334
        If the passed value (or the self._value) is zero (int), then the pack
335
        will assume that the value to be packed is '00:00:00:00:00:00'.
336
337
        Returns
338
            bytes: The binary representation.
339
340
        Raises:
341
            struct.error: If the value does not fit the binary format.
342
        """
343
        if isinstance(value, type(self)):
344
            return value.pack()
345
346
        if value is None:
347
            value = self._value
348
349
        if value == 0:
350
            value = '00:00:00:00:00:00'
351
352
        value = value.split(':')
353
354
        try:
355
            return struct.pack('!6B', *[int(x, 16) for x in value])
356
        except struct.error as err:
357
            msg = "HWAddress error. "
358
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
359
                                                         err)
360
            raise exceptions.PackException(msg)
361
362
    def unpack(self, buff, offset=0):
363
        """Unpack a binary message into this object's attributes.
@@ 188-209 (lines=22) @@
185
        self.length = length
186
        self._fmt = '!{}{}'.format(self.length, 's')
187
188
    def pack(self, value=None):
189
        """Pack the value as a binary representation.
190
191
        Returns:
192
            bytes: The binary representation.
193
194
        Raises:
195
            struct.error: If the value does not fit the binary format.
196
        """
197
        if isinstance(value, type(self)):
198
            return value.pack()
199
200
        try:
201
            if value is None:
202
                value = self.value
203
            packed = struct.pack(self._fmt, bytes(value, 'ascii'))
204
            return packed[:-1] + b'\0'  # null-terminated
205
        except struct.error as err:
206
            msg = "Char Pack error. "
207
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
208
                                                         err)
209
            raise exceptions.PackException(msg)
210
211
    def unpack(self, buff, offset=0):
212
        """Unpack a binary message into this object's attributes.
@@ 254-284 (lines=31) @@
251
        super().__init__(address)
252
        self.netmask = int(netmask)
253
254
    def pack(self, value=None):
255
        """Pack the value as a binary representation.
256
257
        If the value is None the self._value will be used to pack.
258
259
        Args:
260
            value (str): IP Address with ipv4 format.
261
262
        Returns:
263
            bytes: The binary representation.
264
265
        Raises:
266
            struct.error: If the value does not fit the binary format.
267
        """
268
        if isinstance(value, type(self)):
269
            return value.pack()
270
271
        if value is None:
272
            value = self._value
273
274
        if value.find('/') >= 0:
275
            value = value.split('/')[0]
276
277
        try:
278
            value = value.split('.')
279
            return struct.pack('!4B', *[int(x) for x in value])
280
        except struct.error as err:
281
            msg = "IPAddress error. "
282
            msg += "Class: {}, struct error: {} ".format(type(value).__name__,
283
                                                         err)
284
            raise exceptions.PackException(msg)
285
286
    def unpack(self, buff, offset=0):
287
        """Unpack a binary message into this object's attributes.