Пример #1
0
    def difference(self, other):
        """
        @param other: an IP set.

        @return: the difference between this IP set and another as a new IP
            set (all IP addresses and subnets that are in this IP set but
            not found in the other.)
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset - r_ipv4_iset

        for start, end in list(ipv4_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 4), IPAddress(end-1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset - r_ipv6_iset

        for start, end in list(ipv6_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 6), IPAddress(end-1, 6))
            cidr_list.extend(cidrs)

        return IPSet(cidr_list)
Пример #2
0
Файл: sets.py Проект: embr/nonce
    def symmetric_difference(self, other):
        """
        :param other: an IP set.

        :return: the symmetric difference of this IP set and another as a new
            IP set (all IP addresses and subnets that are in exactly one
            of the sets).
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset ^ r_ipv4_iset

        for start, end in list(ipv4_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 4), IPAddress(end - 1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset ^ r_ipv6_iset

        for start, end in list(ipv6_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 6), IPAddress(end - 1, 6))
            cidr_list.extend(cidrs)

        return IPSet(cidr_list)
Пример #3
0
    def intersection(self, other):
        """
        @param other: an IP set.

        @return: the intersection of this IP set and another as a new IP set.
            (IP addresses and subnets common to both sets).
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset & r_ipv4_iset

        for start, end in list(ipv4_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 4), IPAddress(end-1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset & r_ipv6_iset

        for start, end in list(ipv6_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 6), IPAddress(end-1, 6))
            cidr_list.extend(cidrs)

        return IPSet(cidr_list)
Пример #4
0
    def difference(self, other):
        """
        :param other: an IP set.

        :return: the difference between this IP set and another as a new IP
            set (all IP addresses and subnets that are in this IP set but
            not found in the other.)
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset - r_ipv4_iset

        for start, end in ipv4_result._ranges:
            cidrs = iprange_to_cidrs(IPAddress(start, 4),
                                     IPAddress(end - 1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset - r_ipv6_iset

        for start, end in ipv6_result._ranges:
            cidrs = iprange_to_cidrs(IPAddress(start, 6),
                                     IPAddress(end - 1, 6))
            cidr_list.extend(cidrs)

        result = IPSet()
        # None of these CIDRs can be compacted, so skip that operation.
        result._cidrs = dict.fromkeys(cidr_list, True)
        return result
Пример #5
0
    def difference(self, other):
        """
        :param other: an IP set.

        :return: the difference between this IP set and another as a new IP
            set (all IP addresses and subnets that are in this IP set but
            not found in the other.)
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset - r_ipv4_iset

        for start, end in ipv4_result._ranges:
            cidrs = iprange_to_cidrs(IPAddress(start, 4), IPAddress(end-1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset - r_ipv6_iset

        for start, end in ipv6_result._ranges:
            cidrs = iprange_to_cidrs(IPAddress(start, 6), IPAddress(end-1, 6))
            cidr_list.extend(cidrs)

        result = IPSet()
        # None of these CIDRs can be compacted, so skip that operation.
        result._cidrs = dict.fromkeys(cidr_list, True)
        return result
Пример #6
0
    def issuperset(self, other):
        """
        @param other: an IP set.

        @return: C{True} if every IP address and subnet in other IP set
            is found within this one.
        """
        if not hasattr(other, '_cidrs'):
            return NotImplemented

        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv4 = l_ipv4_iset.issuperset(r_ipv4_iset)
        ipv6 = l_ipv6_iset.issuperset(r_ipv6_iset)

        return ipv4 and ipv6
Пример #7
0
    def issuperset(self, other):
        """
        :param other: an IP set.

        :return: ``True`` if every IP address and subnet in other IP set
            is found within this one.
        """
        if not hasattr(other, '_cidrs'):
            return NotImplemented

        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv4 = l_ipv4_iset.issuperset(r_ipv4_iset)
        ipv6 = l_ipv6_iset.issuperset(r_ipv6_iset)

        return ipv4 and ipv6
Пример #8
0
Файл: sets.py Проект: embr/nonce
    def issubset(self, other):
        """
        :param other: an IP set.

        :return: ``True`` if every IP address and subnet in this IP set
            is found within ``other``.
        """
        if not hasattr(other, "_cidrs"):
            return NotImplemented

        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv4 = l_ipv4_iset.issubset(r_ipv4_iset)
        ipv6 = l_ipv6_iset.issubset(r_ipv6_iset)

        return ipv4 and ipv6
Пример #9
0
    def symmetric_difference(self, other):
        """
        :param other: an IP set.

        :return: the symmetric difference of this IP set and another as a new
            IP set (all IP addresses and subnets that are in exactly one
            of the sets).
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset ^ r_ipv4_iset

        for start, end in list(ipv4_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 4),
                                     IPAddress(end - 1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset ^ r_ipv6_iset

        for start, end in list(ipv6_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 6),
                                     IPAddress(end - 1, 6))
            cidr_list.extend(cidrs)

        return IPSet(cidr_list)
Пример #10
0
    def difference(self, other):
        """
        @param other: an IP set.

        @return: the difference between this IP set and another as a new IP
            set (all IP addresses and subnets that are in this IP set but
            not found in the other.)
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset - r_ipv4_iset

        for start, end in list(ipv4_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 4),
                                     IPAddress(end - 1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset - r_ipv6_iset

        for start, end in list(ipv6_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 6),
                                     IPAddress(end - 1, 6))
            cidr_list.extend(cidrs)

        return IPSet(cidr_list)
Пример #11
0
    def intersection(self, other):
        """
        :param other: an IP set.

        :return: the intersection of this IP set and another as a new IP set.
            (IP addresses and subnets common to both sets).
        """
        cidr_list = []

        #   Separate IPv4 from IPv6.
        l_ipv4, l_ipv6 = partition_ips(self._cidrs)
        r_ipv4, r_ipv6 = partition_ips(other._cidrs)

        #   Process IPv4.
        l_ipv4_iset = _IntSet(*[(c.first, c.last) for c in l_ipv4])
        r_ipv4_iset = _IntSet(*[(c.first, c.last) for c in r_ipv4])

        ipv4_result = l_ipv4_iset & r_ipv4_iset

        for start, end in list(ipv4_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 4),
                                     IPAddress(end - 1, 4))
            cidr_list.extend(cidrs)

        #   Process IPv6.
        l_ipv6_iset = _IntSet(*[(c.first, c.last) for c in l_ipv6])
        r_ipv6_iset = _IntSet(*[(c.first, c.last) for c in r_ipv6])

        ipv6_result = l_ipv6_iset & r_ipv6_iset

        for start, end in list(ipv6_result._ranges):
            cidrs = iprange_to_cidrs(IPAddress(start, 6),
                                     IPAddress(end - 1, 6))
            cidr_list.extend(cidrs)

        return IPSet(cidr_list)