ó
ú2ec           @   s;  d  Z  d Z d Z d Z d Z d d l m Z d d l Z d d l Z d e j	 f d	 „  ƒ  YZ
 e d
 „ Z e d „ Z e d k r7e
 d ƒ Z e
 d d d ƒ Z e GHx e j ƒ  D] Z e GHq³ Wx e j ƒ  D] Z e GHqÏ Wx e e Bj ƒ  D] Z e GHqï Wx e e Aj ƒ  D] Z e GHqWx e D] Z e GHq%Wn  d S(   sŠ   IP4 address range set implementation.

Implements an IPv4-range type.

Copyright (C) 2006, Heiko Wundram.
Released under the MIT-license.
s    Heiko Wundram <me@modelnine.org>s   0.2t   3s
   2006-01-20iÿÿÿÿ(   t   intsetNt   IP4Rangec           B   s®   e  Z d  Z d Z d Z d j g  e d ƒ D] Z e e ƒ ^ q+ ƒ Z	 d Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e Z d „  Z RS(   sb   IP4 address range class with efficient storage of address ranges.
    Supports all set operations.i    i   i    t    i   s   0123456789.c         G   s"  t  | ƒ d k rC t | d t ƒ rC t t |  ƒ j | d ƒ d St | ƒ } x¤t t  | ƒ ƒ D]} | | } t | t ƒ rö d | k r² |  j | j	 d d ƒ Œ  | | <qb qòd | k rà |  j
 | j	 d d ƒ Œ  | | <qò|  j | ƒ | | <qb t | t ƒ rÑt  t ƒ d k r&t d ƒ ‚ n  | \ } } t | t ƒ rW|  j | ƒ d } n! t | t j ƒ sxt d ƒ ‚ n  t | t ƒ r|  j | ƒ d } n! t | t j ƒ s¾t d ƒ ‚ n  | | f | | <qb t | t j ƒ sb t d ƒ ‚ qb qb Wt t |  ƒ j d	 |  j d
 |  j | Œ d S(   sH  Initialize an ip4range class. The constructor accepts an unlimited
        number of arguments that may either be tuples in the form (start,stop),
        integers, longs or strings, where start and stop in a tuple may
        also be of the form integer, long or string.

        Passing an integer or long means passing an IPv4-address that's already
        been converted to integer notation, whereas passing a string specifies
        an address where this conversion still has to be done. A string
        address may be in the following formats:

        - 1.2.3.4    - a plain address, interpreted as a single address
        - 1.2.3      - a set of addresses, interpreted as 1.2.3.0-1.2.3.255
        - localhost  - hostname to look up, interpreted as single address
        - 1.2.3<->5  - a set of addresses, interpreted as 1.2.3.0-1.2.5.255
        - 1.2.0.0/16 - a set of addresses, interpreted as 1.2.0.0-1.2.255.255

        Only the first three notations are valid if you use a string address in
        a tuple, whereby notation 2 is interpreted as 1.2.3.0 if specified as
        lower bound and 1.2.3.255 if specified as upper bound, not as a range
        of addresses.

        Specifying a range is done with the <-> operator. This is necessary
        because '-' might be present in a hostname. '<->' shouldn't be, ever.
        i   i    Ns   <->s   /i   s   Tuple is of invalid length.s   Invalid argument.t   mint   max(   t   lent
   isinstanceR   t   supert   __init__t   listt   ranget   strt   _parseRanget   splitt
   _parseMaskt   _parseAddrRanget   tuplet
   ValueErrort   sixt   integer_typest	   TypeErrort   _MINIP4t   _MAXIP4(   t   selft   argst   it   argvalt   addr1t   addr2(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR	   '   s:    %
"c         C   sÂ   t  | ƒ \ } } t  | ƒ \ } } | | k  r_ | | d | | d >d | d >@7} | } n | | k rz t d ƒ ‚ n  | d | d K} | d | d K} | d d | d >d 7} | | f S(   Ni   i   s!   Range has more dots than address.i   (   t
   _parseAddrR   (   R   R   R   t   naddr1t	   naddr1lent   naddr2t	   naddr2len(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR   k   s    	c   	      C   sc  t  | ƒ \ } } | d | d K} yD | s6 d } n t | ƒ } d | k oY d k n sg t ‚ n  Wn¾ t k
 r(y t  | t ƒ } Wn t k
 rª t d ƒ ‚ n Xd } d } | sÆ d } q)x | d @sà | d 7} qÉ Wx" | d @r| d L} | d 7} qä W| | d k r)t d ƒ ‚ q)n X| d | >d d | >@} | d d | >d } | | f S(   Ni   i   i    i    s   Mask isn't parseable.i   s   Mask isn't a proper host mask.(   R   t   intR   t   False(	   R   t   addrt   maskt   naddrt   naddrlent   masklent	   remainingR   R!   (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR   y   s6    		
c         C   sT   t  | ƒ \ } } | d | d >} | d | d >d d | d >d } | | f S(   Ni   i   i   (   R   (   R   R%   R'   R(   R   R!   (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR   ˜   s    &c         C   sQ   g  } x5 t  d ƒ D]' } | j t | d @ƒ ƒ | d L} q Wd j t | ƒ ƒ S(   Ni   iÿ   i   s   .(   R   t   appendR   t   joint   reversed(   R   t   numt   rvR   (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt   _int2ip¢   s
    c         c   s2   x+ t  t |  ƒ j ƒ  D] } |  j | ƒ Vq Wd S(   sw   Returns an iterator which iterates over ips in this iprange. An
        IP is returned in string form (e.g. '1.2.3.4').N(   R   R   t   __iter__R0   (   R   t   v(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt   iteraddresses¬   s    c         c   sr   xk |  j  D]` } | d | d d k r= |  j | d ƒ Vq
 d |  j | d ƒ |  j | d d ƒ f Vq
 Wd S(   s­   Returns an iterator which iterates over ip-ip ranges which build
        this iprange if combined. An ip-ip pair is returned in string form
        (e.g. '1.2.3.4-2.3.4.5').i   i    s   %s-%sN(   t   _rangesR0   (   R   t   r(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt
   iterranges³   s    c         c   s7   x0 |  j  D]% } x |  j | ƒ D] } | Vq  Wq
 Wd S(   s«   Returns an iterator which iterates over ip/mask pairs which build
        this iprange if combined. An IP/Mask pair is returned in string form
        (e.g. '1.2.3.0/24').N(   R4   t
   _itermasks(   R   R5   R2   (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt	   itermasks¾   s    c         c   s  | g } x| r| j  ƒ  } d } xp t r– d d | >} | d | d d | >d d | >@} | | d k r‰ | | | d k r‰ Pq' | d 7} q' Wd |  j | ƒ | f V| d | k  rÙ | j | d | f ƒ n  | d | | k r | j | | | d f ƒ q q Wd  S(   Ni    i   i    s   %s/%s(   t   popt   TrueR0   R+   (   R   R5   t   rangest   curt   curmaskt
   curmasklent   start(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR7   Ç   s    			&$c         C   sš   g  } xt |  j  D]i \ } } | | d k rL | j d |  j | ƒ f ƒ q | j d |  j | ƒ |  j | d ƒ f ƒ q Wd |  j j d j | ƒ f S(   s?   Returns a string which can be used to reconstruct this iprange.i   s   %rs   (%r,%r)s   %s(%s)s   ,(   R4   R+   R0   t	   __class__t   __name__R,   (   R   R/   R?   t   stop(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt   __repr__Þ   s     I       Iÿÿÿÿ    (   RA   t
   __module__t   __doc__R   R   R,   R   t   nt   chrt   _UNITYTRANSt	   _IPREMOVER	   R   R   R   R0   R3   R6   R8   R7   R1   RC   (    (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR      s    .	D			
	
					c         C   s"  | rU t  d „  |  Dƒ ƒ rU y t j |  ƒ }  WqU t j k
 rQ t d ƒ ‚ qU Xn  d } x¶ t |  j d ƒ ƒ D]Ÿ \ } } | d k r˜ t d ƒ ‚ n  yD | sª d } n t | ƒ } d | k oÍ d k  n sÛ t ‚ n  Wn t k
 rû t d ƒ ‚ n X| d	 K} | | 7} qq W| | d
 f S(   Nc         s   s   |  ] } | t  j k Vq d  S(   N(   R   RI   (   t   .0t   ch(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pys	   <genexpr>ë   s    s   Invalid Hostname as argument.i    s   .i   s&   Address contains more than four parts.i   s   Address part out of range.i   i   (   t   anyt   sockett   gethostbynamet   errorR   t	   enumerateR   R#   (   R%   t   lookupR'   t   naddrpost   part(    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyR   ê   s(    "	
c         C   s   t  |  d | ƒd S(   NRQ   i    (   R   (   R%   RQ   (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt   ip2int  s    t   __main__s   172.22.162.250/24s   172.22.162.250s   172.22.163.250s   172.22.163.253<->255(   RE   t
   __author__t   __version__t   __revision__t   __date__t
   paste.utilR   RM   R   t   IntSetR   R:   R   RT   RA   t   xt   yR8   t   valR6   (    (    (    s8   /usr/local/lib/python2.7/dist-packages/paste/util/ip4.pyt   <module>   s0   Ì				