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