| @@ 132-175 (lines=44) @@ | ||
| 129 | return self._value.bitmask |
|
| 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): |
|
| @@ 328-357 (lines=30) @@ | ||
| 325 | Args: |
|
| 326 | hw_address (bytes): Hardware address. Defaults to |
|
| 327 | '00:00:00:00:00:00'. |
|
| 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) |
|
| @@ 184-205 (lines=22) @@ | ||
| 181 | value: The character to be build. |
|
| 182 | length (int): Character size. |
|
| 183 | """ |
|
| 184 | super().__init__(value) |
|
| 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) |
|
| @@ 251-281 (lines=31) @@ | ||
| 248 | else: |
|
| 249 | netmask = 32 |
|
| 250 | ||
| 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) |
|