ó €aUc@sˆdZddlZddlmZmZmZmZmZm Z ddl m Z m Z m Z d„Zd„Zdefd„ƒYZdS( s2Set based operations for IP addresses and subnets.i˙˙˙˙N(t IPNetworkt IPAddresstIPRanget cidr_merget cidr_excludetiprange_to_cidrs(t _sys_maxintt _dict_keyst _int_typec Cs|jj}||}|j|jkrK|j||j|jdfƒn|d7}|}x}|t|ƒkrÚ||}||krŠPn|jd|jkrŁn$|j||jd|jdfƒ|d7}|}q^W|jd}|j} || kr|j||| fƒn|S(sŽCalculate IPSet([supernet]) - IPSet(subnets). Assumptions: subnets is sorted, subnet_idx points to the first element in subnets that is a subnet of supernet. Results are appended to the ranges parameter as tuples of in format (version, first, last). Return value is the first subnet_idx that does not point to a subnet of supernet (or len(subnets) if all subsequents items are a subnet of supernet). i(t_moduletversiontfirsttappendtlentlast( tsupernettsubnetst subnet_idxtrangesR tsubnett prev_subnett cur_subnetR R((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt _subtracts(  #   $     ccsł|s dS|d\}}}xr|dD]f\}}}||dkr_||kr_|}q(nt||ƒt||ƒfV|}|}|}q(Wt||ƒt||ƒfVdS(sqIterate over sorted_ranges, merging where possible Sorted ranges must be a sorted iterable of (version, first, last) tuples. Merging occurs for pairs like [(4, 10, 42), (4, 43, 100)] which is merged into (4, 10, 100), and leads to return value ( IPAddress(10, 4), IPAddress(100, 4) ), which is suitable input for the iprange_to_cidrs function. Nii(R(t sorted_rangestcurrent_versiont current_startt current_stopt next_versiont next_startt next_stop((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt_iter_merged_ranges7s    tIPSetcBs‚eZdZd$Zd%dd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z e Z d „Zd „Zdd „Zdd„Zd„Zd„Zd„Zdd„Zd„Zd„Zd„Zd„Zd„ZeZd„Zd„ZeZd„ZeZ d„Z!e!Z"d„Z#e#Z$d„Z%e%Z&d„Z'e(d„ƒZ)d „Z*e*Z+d!„Z,d"„Z-d#„Z.RS(&s[ Represents an unordered collection (set) of unique IP addresses and subnets. t_cidrsicCst|tƒr(itt|ƒ6|_nét|tƒr`tjt|d|dƒtƒ|_nąt|tƒrtj|j ƒtƒ|_n„i|_|dk rg}x?|D]7}t|t ƒrŮt |d|ƒ}n|j |ƒqŻWx$t|ƒD]}t|j|…sN(R$R%R#R (R(tstate((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt __setstate__scCsí|j}|j}|j}|j|jjkrgxú|jƒD]#}||jkr=|j|=dSq=WnĆg}x˘|jD]—}|jj|ksw||krĄqwn|j}|j} ||krŰ| |krŰ|j|ƒqw||krw| |krw|j|=| s t ‚dSqwWx|D]} |j| =qW|jj|j } xŚ|j dkrč|j | ?d@} | rx|j ƒ} n |j ƒ} | |jkr—dS|j| =|j|=|j d8_ | d7} |j | ?| >|_ t|j|†sNRRsan iterable was expected!R!(R"RR$R%RRR R#RRRUthasattrRHRRR RG(R(R)R!R*R+R,((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyRSxs$     cCs i|_dS(s5Remove all IP addresses and subnets from this IP set.N(R (R(((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pytclear›scCs-y|j|jkSWntk r(tSXdS(s› :param other: an IP set :return: ``True`` if this IP set is equivalent to the ``other`` IP set, ``False`` otherwise. N(R tAttributeErrortNotImplemented(R(R\((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt__eq__Ÿs cCs-y|j|jkSWntk r(tSXdS(s› :param other: an IP set :return: ``False`` if this IP set is equivalent to the ``other`` IP set, ``True`` otherwise. N(R RdRe(R(R\((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt__ne__Ťs cCs2t|dƒstS|j|jko1|j|ƒS(s— :param other: an IP set :return: ``True`` if this IP set is less than the ``other`` IP set, ``False`` otherwise. R (RbRetsizetissubset(R(R\((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt__lt__ˇscCs(x!|jD]}||kr tSq WtS(sš :param other: an IP set. :return: ``True`` if every IP address and subnet in this IP set is found within ``other``. (R RJR#(R(R\R,((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyRiĂs cCs2t|dƒstS|j|jko1|j|ƒS(s› :param other: an IP set. :return: ``True`` if this IP set is greater than the ``other`` IP set, ``False`` otherwise. R (RbReRht issuperset(R(R\((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt__gt__ŃscCs;t|dƒstSx!|jD]}||krtSqWtS(sš :param other: an IP set. :return: ``True`` if every IP address and subnet in other IP set is found within this one. R (RbReR RJR#(R(R\R,((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyRkÝs  cCs|jƒ}|j|ƒ|S(są :param other: an IP set. :return: the union of this IP set and another as a new IP set (combines IP addresses and subnets from both sets). (RaRS(R(R\tip_set((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pytunionîs  c Cs'i}t|jƒ}t|jƒ}d}d}t|ƒ}t|ƒ}xĆ||kr||kr||} ||} | | kr¤t|| <|d7}|d7}qK| | krÇt|| <|d7}qK| | kręt|| <|d7}qK| | kr|d7}qK|d7}qKWtƒ} || _| S(sľ :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). ii(RQR R R#R( R(R\t result_cidrstown_netst other_netstown_idxt other_idxtown_lent other_lentown_curt other_curR]((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyR[űs4                  cCsJg}t|jƒ}t|jƒ}d}d}t|ƒ}t|ƒ}x||kr`||kr`||} ||} | | krš|d7}|d7}qK| | krČt| |||ƒ}|d7}qK| | kröt| |||ƒ}|d7}qK| | kr1|j| jj| j| jfƒ|d7}qK|j| jj| j| jfƒ|d7}qKWxF||krŠ||} |j| jj| j| jfƒ|d7}qdWxF||krň||} |j| jj| j| jfƒ|d7}q­Wt ƒ} xGt |ƒD]9\} } t | | ƒ}x|D]}t | j|t|ƒD]0\} } x!t| | ƒD]}t|| maxint (a Python limitation). Use the .size property for subnets of any size. sVrange contains more than %d (sys.maxint) IP addresses! Use the .size property instead.(RhRt IndexError(R(Rh((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt__len__Ąs    cCs#tg|jD]}|j^q ƒS(s˜ The cardinality of this IP set (based on the number of individual IP addresses including those implicitly defined in subnets). (tsumR Rh(R(R,((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyRh­scCs*dgt|jƒD]}t|ƒ^qS(s8:return: Python statement to create an equivalent objects IPSet(%r)(RQR tstr(R(tc((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt__repr__ľscCsf|jƒ}t|ƒdkrb|dd}x3|D](}|d|krMtS|dd}q3WntS(sĆ Returns True if the members of the set form a contiguous IP address range (with no gaps), False otherwise. :return: ``True`` if the ``IPSet`` object is contiguous. iii˙˙˙˙(R&R RJR#(R(RFR9R,((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt iscontiguousťs  cCsO|jƒr?|jƒ}|s"dSt|dd|ddƒStdƒ‚dS(sě Generates an IPRange for this IPSet, if all its members form a single contiguous sequence. Raises ``ValueError`` if the set is not contiguous. :return: An ``IPRange`` for all IPs in the IPSet. ii˙˙˙˙sIPSet is not contiguousN(RƒR&R'Rt ValueError(R(RF((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pytiprangeËs  ccscg|jƒD]!}|jj|j|jf^q }x(t|ƒD]\}}t||ƒVqAWdS(sůGenerate the merged IPRanges for this IPSet. In contrast to self.iprange(), this will work even when the IPSet is not contiguous. Adjacent IPRanges will be merged together, so you get the minimal number of IPRanges. N(R&R R R RRR(R(R,RRyRz((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyt iter_iprangesÜs1(s_cidrsN(/t__name__t __module__t__doc__t __slots__R'R-R/R4RERGRIRLRNt__bool__RRR&RURVRZR^RaRSRcRfRgRjRit__le__RlRkt__ge__Rnt__or__R[t__and__R{t__xor__R|t__sub__R~tpropertyRhR‚t__str__RƒR…R†(((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyRTsV  J       8  #     * B 4    (R‰t itertoolsROt netaddr.ipRRRRRRtnetaddr.compatRRRRRtobjectR(((s3/usr/lib/python2.7/site-packages/netaddr/ip/sets.pyts  . '