| @@ 133-176 (lines=44) @@ | ||
| 130 | else: |
|
| 131 | return self._value |
|
| 132 | ||
| 133 | def pack(self, value=None): |
|
| 134 | r"""Pack the value as a binary representation. |
|
| 135 | ||
| 136 | Considering an example with UBInt8 class, that inherits from |
|
| 137 | GenericType: |
|
| 138 | ||
| 139 | >>> from pyof.foundation.basic_types import UBInt8 |
|
| 140 | >>> objectA = UBInt8(1) |
|
| 141 | >>> objectB = 5 |
|
| 142 | >>> objectA.pack() |
|
| 143 | b'\x01' |
|
| 144 | >>> objectA.pack(objectB) |
|
| 145 | b'\x05' |
|
| 146 | ||
| 147 | Args: |
|
| 148 | value: If the value is None, then we will pack the value of the |
|
| 149 | current instance. Otherwise, if value is an instance of the |
|
| 150 | same type as the current instance, then we call the pack of the |
|
| 151 | value object. Otherwise, we will use the current instance pack |
|
| 152 | method on the passed value. |
|
| 153 | ||
| 154 | Returns: |
|
| 155 | bytes: The binary representation. |
|
| 156 | ||
| 157 | Raises: |
|
| 158 | :exc:`~.exceptions.BadValueException`: If the value does not |
|
| 159 | fit the binary format. |
|
| 160 | """ |
|
| 161 | if isinstance(value, type(self)): |
|
| 162 | return value.pack() |
|
| 163 | ||
| 164 | if value is None: |
|
| 165 | value = self.value |
|
| 166 | elif 'value' in dir(value): |
|
| 167 | # if it is enum or bitmask gets only the 'int' value |
|
| 168 | value = value.value |
|
| 169 | ||
| 170 | try: |
|
| 171 | return struct.pack(self._fmt, value) |
|
| 172 | except struct.error: |
|
| 173 | msg = '{} could not pack {} = {}.'.format(type(self).__name__, |
|
| 174 | type(value).__name__, |
|
| 175 | value) |
|
| 176 | raise PackException(msg) |
|
| 177 | ||
| 178 | def unpack(self, buff, offset=0): |
|
| 179 | """Unpack *buff* into this object. |
|
| @@ 331-360 (lines=30) @@ | ||
| 328 | super().__init__(hw_address) |
|
| 329 | ||
| 330 | def pack(self, value=None): |
|
| 331 | """Pack the value as a binary representation. |
|
| 332 | ||
| 333 | If the passed value (or the self._value) is zero (int), then the pack |
|
| 334 | will assume that the value to be packed is '00:00:00:00:00:00'. |
|
| 335 | ||
| 336 | Returns |
|
| 337 | bytes: The binary representation. |
|
| 338 | ||
| 339 | Raises: |
|
| 340 | struct.error: If the value does not fit the binary format. |
|
| 341 | """ |
|
| 342 | if isinstance(value, type(self)): |
|
| 343 | return value.pack() |
|
| 344 | ||
| 345 | if value is None: |
|
| 346 | value = self._value |
|
| 347 | ||
| 348 | if value == 0: |
|
| 349 | value = '00:00:00:00:00:00' |
|
| 350 | ||
| 351 | value = value.split(':') |
|
| 352 | ||
| 353 | try: |
|
| 354 | return struct.pack('!6B', *[int(x, 16) for x in value]) |
|
| 355 | except struct.error as err: |
|
| 356 | msg = "HWAddress error. " |
|
| 357 | msg += "Class: {}, struct error: {} ".format(type(value).__name__, |
|
| 358 | err) |
|
| 359 | raise exceptions.PackException(msg) |
|
| 360 | ||
| 361 | def unpack(self, buff, offset=0): |
|
| 362 | """Unpack a binary message into this object's attributes. |
|
| 363 | ||
| @@ 188-209 (lines=22) @@ | ||
| 185 | """ |
|
| 186 | if isinstance(value, type(self)): |
|
| 187 | return value.pack() |
|
| 188 | ||
| 189 | try: |
|
| 190 | if value is None: |
|
| 191 | value = self.value |
|
| 192 | packed = struct.pack(self._fmt, bytes(value, 'ascii')) |
|
| 193 | return packed[:-1] + b'\0' # null-terminated |
|
| 194 | except struct.error as err: |
|
| 195 | msg = "Char Pack error. " |
|
| 196 | msg += "Class: {}, struct error: {} ".format(type(value).__name__, |
|
| 197 | err) |
|
| 198 | raise exceptions.PackException(msg) |
|
| 199 | ||
| 200 | def unpack(self, buff, offset=0): |
|
| 201 | """Unpack a binary message into this object's attributes. |
|
| 202 | ||
| 203 | Unpack the binary value *buff* and update this object attributes based |
|
| 204 | on the results. |
|
| 205 | ||
| 206 | Args: |
|
| 207 | buff (bytes): Binary data package to be unpacked. |
|
| 208 | offset (int): Where to begin unpacking. |
|
| 209 | ||
| 210 | Raises: |
|
| 211 | Exception: If there is a struct unpacking error. |
|
| 212 | """ |
|