Collapse a list of IP objects. Example: collapse_addresses([IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]) -> [IPv4Network('192.0.2.0/24')] Args: addresses: An iterable of IPv4Network or IPv6Network object
(addresses)
| 299 | |
| 300 | |
| 301 | def collapse_addresses(addresses): |
| 302 | """Collapse a list of IP objects. |
| 303 | |
| 304 | Example: |
| 305 | collapse_addresses([IPv4Network('192.0.2.0/25'), |
| 306 | IPv4Network('192.0.2.128/25')]) -> |
| 307 | [IPv4Network('192.0.2.0/24')] |
| 308 | |
| 309 | Args: |
| 310 | addresses: An iterable of IPv4Network or IPv6Network objects. |
| 311 | |
| 312 | Returns: |
| 313 | An iterator of the collapsed IPv(4|6)Network objects. |
| 314 | |
| 315 | Raises: |
| 316 | TypeError: If passed a list of mixed version objects. |
| 317 | |
| 318 | """ |
| 319 | addrs = [] |
| 320 | ips = [] |
| 321 | nets = [] |
| 322 | |
| 323 | # split IP addresses and networks |
| 324 | for ip in addresses: |
| 325 | if isinstance(ip, _BaseAddress): |
| 326 | if ips and ips[-1].version != ip.version: |
| 327 | raise TypeError("%s and %s are not of the same version" % ( |
| 328 | ip, ips[-1])) |
| 329 | ips.append(ip) |
| 330 | elif ip._prefixlen == ip.max_prefixlen: |
| 331 | if ips and ips[-1].version != ip.version: |
| 332 | raise TypeError("%s and %s are not of the same version" % ( |
| 333 | ip, ips[-1])) |
| 334 | try: |
| 335 | ips.append(ip.ip) |
| 336 | except AttributeError: |
| 337 | ips.append(ip.network_address) |
| 338 | else: |
| 339 | if nets and nets[-1].version != ip.version: |
| 340 | raise TypeError("%s and %s are not of the same version" % ( |
| 341 | ip, nets[-1])) |
| 342 | nets.append(ip) |
| 343 | |
| 344 | # sort and dedup |
| 345 | ips = sorted(set(ips)) |
| 346 | |
| 347 | # find consecutive address ranges in the sorted sequence and summarize them |
| 348 | if ips: |
| 349 | for first, last in _find_address_range(ips): |
| 350 | addrs.extend(summarize_address_range(first, last)) |
| 351 | |
| 352 | return _collapse_addresses_internal(addrs + nets) |
| 353 | |
| 354 | |
| 355 | def get_mixed_type_key(obj): |
nothing calls this directly
no test coverage detected
searching dependent graphs…