def remove(self, addr, flags=0): """ Removes an IP address or subnet or IPRange from this IP set. Does nothing if it is not already a member. Note that this method behaves more like discard() found in regular Python sets because it doesn't raise KeyError exceptions if the IP address or subnet is question does not exist. It doesn't make sense to fully emulate that behaviour here as IP sets contain groups of individual IP addresses as individual set members using IPNetwork objects. :param addr: An IP address or subnet, or an IPRange. :param flags: decides which rules are applied to the interpretation of the addr value. See the netaddr.core namespace documentation for supported constant values. """ if isinstance(addr, IPRange): cidrs = iprange_to_cidrs(addr[0], addr[-1]) for cidr in cidrs: self.remove(cidr) return if isinstance(addr, _int_type): addr = IPAddress(addr, flags=flags) else: addr = IPNetwork(addr) # This add() is required for address blocks provided that are larger # than blocks found within the set but have overlaps. e.g. :- # # >>> IPSet(['192.0.2.0/24']).remove('192.0.2.0/23') # IPSet([]) # self.add(addr) remainder = None matching_cidr = None # Search for a matching CIDR and exclude IP from it. for cidr in self._cidrs: if addr in cidr: remainder = cidr_exclude(cidr, addr) matching_cidr = cidr break # Replace matching CIDR with remaining CIDR elements. if remainder is not None: del self._cidrs[matching_cidr] for cidr in remainder: self._cidrs[cidr] = True
def extract_subnet(self, prefix, count=None): """Extract 1 or more subnets of size specified by CIDR prefix.""" for cidr in self.available_subnets(): subnets = list(cidr.subnet(prefix, count=count)) if not subnets: continue self.remove_subnet(cidr) self._subnets = self._subnets.union( set(cidr_exclude(cidr, cidr_merge(subnets)[0]))) return subnets return []
def extract_subnet(self, prefix, count=None): """Extract 1 or more subnets of size specified by CIDR prefix.""" for cidr in self.available_subnets(): subnets = list(cidr.subnet(prefix, count=count)) if not subnets: continue self.remove_subnet(cidr) self._subnets = self._subnets.union( set( cidr_exclude(cidr, cidr_merge(subnets)[0]) ) ) return subnets return []
def remove(self, ip): """ Removes an IP address or subnet from this IP set. Does nothing if it is not already a member. Note that this method behaves more like discard() found in regular Python sets because it doesn't raise KeyError exceptions if the IP address or subnet is question does not exist. It doesn't make sense to fully emulate that behaviour here as IP sets contain groups of individual IP addresses as individual set members using IPNetwork objects. @param ip: An IP address or subnet. """ ip = IPNetwork(ip) # This add() is required for address blocks provided that are larger # than blocks found within the set but have overlaps. e.g. :- # # >>> IPSet(['192.0.2.0/24']).remove('192.0.2.0/23') # IPSet([]) # self.add(ip) remainder = None matching_cidr = None # Search for a matching CIDR and exclude IP from it. for cidr in self._cidrs: if ip in cidr: remainder = cidr_exclude(cidr, ip) matching_cidr = cidr break # Replace matching CIDR with remaining CIDR elements. if remainder is not None: del self._cidrs[matching_cidr] for cidr in remainder: self._cidrs[cidr] = True self.compact()