| @@ 132-175 (lines=44) @@ | ||
| 129 | else: |
|
| 130 | return self._value |
|
| 131 | ||
| 132 | def pack(self, value=None): |
|
| 133 | r"""Pack the value as a binary representation. |
|
| 134 | ||
| 135 | Considering an example with UBInt8 class, that inherits from |
|
| 136 | GenericType: |
|
| 137 | ||
| 138 | >>> from pyof.foundation.basic_types import UBInt8 |
|
| 139 | >>> objectA = UBInt8(1) |
|
| 140 | >>> objectB = 5 |
|
| 141 | >>> objectA.pack() |
|
| 142 | b'\x01' |
|
| 143 | >>> objectA.pack(objectB) |
|
| 144 | b'\x05' |
|
| 145 | ||
| 146 | Args: |
|
| 147 | value: If the value is None, then we will pack the value of the |
|
| 148 | current instance. Otherwise, if value is an instance of the |
|
| 149 | same type as the current instance, then we call the pack of the |
|
| 150 | value object. Otherwise, we will use the current instance pack |
|
| 151 | method on the passed value. |
|
| 152 | ||
| 153 | Returns: |
|
| 154 | bytes: The binary representation. |
|
| 155 | ||
| 156 | Raises: |
|
| 157 | :exc:`~.exceptions.BadValueException`: If the value does not |
|
| 158 | fit the binary format. |
|
| 159 | """ |
|
| 160 | if isinstance(value, type(self)): |
|
| 161 | return value.pack() |
|
| 162 | ||
| 163 | if value is None: |
|
| 164 | value = self.value |
|
| 165 | elif 'value' in dir(value): |
|
| 166 | # if it is enum or bitmask gets only the 'int' value |
|
| 167 | value = value.value |
|
| 168 | ||
| 169 | try: |
|
| 170 | return struct.pack(self._fmt, value) |
|
| 171 | except struct.error: |
|
| 172 | msg = '{} could not pack {} = {}.'.format(type(self).__name__, |
|
| 173 | type(value).__name__, |
|
| 174 | value) |
|
| 175 | raise PackException(msg) |
|
| 176 | ||
| 177 | def unpack(self, buff, offset=0): |
|
| 178 | """Unpack *buff* into this object. |
|
| @@ 328-357 (lines=30) @@ | ||
| 325 | """ |
|
| 326 | super().__init__(hw_address) |
|
| 327 | ||
| 328 | def pack(self, value=None): |
|
| 329 | """Pack the value as a binary representation. |
|
| 330 | ||
| 331 | If the passed value (or the self._value) is zero (int), then the pack |
|
| 332 | will assume that the value to be packed is '00:00:00:00:00:00'. |
|
| 333 | ||
| 334 | Returns |
|
| 335 | bytes: The binary representation. |
|
| 336 | ||
| 337 | Raises: |
|
| 338 | struct.error: If the value does not fit the binary format. |
|
| 339 | """ |
|
| 340 | if isinstance(value, type(self)): |
|
| 341 | return value.pack() |
|
| 342 | ||
| 343 | if value is None: |
|
| 344 | value = self._value |
|
| 345 | ||
| 346 | if value == 0: |
|
| 347 | value = '00:00:00:00:00:00' |
|
| 348 | ||
| 349 | value = value.split(':') |
|
| 350 | ||
| 351 | try: |
|
| 352 | return struct.pack('!6B', *[int(x, 16) for x in value]) |
|
| 353 | except struct.error as err: |
|
| 354 | msg = "HWAddress error. " |
|
| 355 | msg += "Class: {}, struct error: {} ".format(type(value).__name__, |
|
| 356 | err) |
|
| 357 | raise exceptions.PackException(msg) |
|
| 358 | ||
| 359 | def unpack(self, buff, offset=0): |
|
| 360 | """Unpack a binary message into this object's attributes. |
|
| @@ 184-205 (lines=22) @@ | ||
| 181 | self.length = length |
|
| 182 | self._fmt = '!{}{}'.format(self.length, 's') |
|
| 183 | ||
| 184 | def pack(self, value=None): |
|
| 185 | """Pack the value as a binary representation. |
|
| 186 | ||
| 187 | Returns: |
|
| 188 | bytes: The binary representation. |
|
| 189 | ||
| 190 | Raises: |
|
| 191 | struct.error: If the value does not fit the binary format. |
|
| 192 | """ |
|
| 193 | if isinstance(value, type(self)): |
|
| 194 | return value.pack() |
|
| 195 | ||
| 196 | try: |
|
| 197 | if value is None: |
|
| 198 | value = self.value |
|
| 199 | packed = struct.pack(self._fmt, bytes(value, 'ascii')) |
|
| 200 | return packed[:-1] + b'\0' # null-terminated |
|
| 201 | except struct.error as err: |
|
| 202 | msg = "Char Pack error. " |
|
| 203 | msg += "Class: {}, struct error: {} ".format(type(value).__name__, |
|
| 204 | err) |
|
| 205 | raise exceptions.PackException(msg) |
|
| 206 | ||
| 207 | def unpack(self, buff, offset=0): |
|
| 208 | """Unpack a binary message into this object's attributes. |
|
| @@ 251-281 (lines=31) @@ | ||
| 248 | super().__init__(address) |
|
| 249 | self.netmask = int(netmask) |
|
| 250 | ||
| 251 | def pack(self, value=None): |
|
| 252 | """Pack the value as a binary representation. |
|
| 253 | ||
| 254 | If the value is None the self._value will be used to pack. |
|
| 255 | ||
| 256 | Args: |
|
| 257 | value (str): IP Address with ipv4 format. |
|
| 258 | ||
| 259 | Returns |
|
| 260 | bytes: The binary representation. |
|
| 261 | ||
| 262 | Raises: |
|
| 263 | struct.error: If the value does not fit the binary format. |
|
| 264 | """ |
|
| 265 | if isinstance(value, type(self)): |
|
| 266 | return value.pack() |
|
| 267 | ||
| 268 | if value is None: |
|
| 269 | value = self._value |
|
| 270 | ||
| 271 | if value.find('/') >= 0: |
|
| 272 | value = value.split('/')[0] |
|
| 273 | ||
| 274 | try: |
|
| 275 | value = value.split('.') |
|
| 276 | return struct.pack('!4B', *[int(x) for x in value]) |
|
| 277 | except struct.error as err: |
|
| 278 | msg = "IPAddress error. " |
|
| 279 | msg += "Class: {}, struct error: {} ".format(type(value).__name__, |
|
| 280 | err) |
|
| 281 | raise exceptions.PackException(msg) |
|
| 282 | ||
| 283 | def unpack(self, buff, offset=0): |
|
| 284 | """Unpack a binary message into this object's attributes. |
|