ó
i4Vdc           @   sv   d  d l  m Z d  d l m Z d  d l m Z e e d  d  Z d   Z	 d   Z
 d   Z d	   Z d
   Z d S(   i˙˙˙˙(   t   ValidationError(   t   range(   t   ugettext_lazys!   This is not a valid IPv6 address.c         C   sŁ  d } d } d } d } t  |   s9 t | d d  n  t |   }  t |   }  | rp t |   } | rp | Sn  |  j d  } xĄ t t |   D] }	 | |	 j d  | |	 <| |	 sĆ d | |	 <n  | |	 d k r| d 7} | d k rő |	 } n  | | k r| } | } qq d } d } q W| d k r| | }
 |
 t |  k r[| d g 7} n  d g | | |
 +| d k rd g | } qn  d j	 |  } | j
   S(	   sU  
    Cleans an IPv6 address string.

    Validity is checked by calling is_valid_ipv6_address() - if an
    invalid address is passed, ValidationError is raised.

    Replaces the longest continuous zero-sequence with "::" and
    removes leading zeroes and makes sure all hextets are lowercase.

    Args:
        ip_str: A valid IPv6 address.
        unpack_ipv4: if an IPv4-mapped address is found,
        return the plain IPv4 address (default=False).
        error_message: An error message used in the ValidationError.

    Returns:
        A compressed IPv6 address, or the same value
    i˙˙˙˙i    t   codet   invalidt   :t   0i   t    (   t   is_valid_ipv6_addressR    t   _explode_shorthand_ip_stringt   _sanitize_ipv4_mappingt   _unpack_ipv4t   splitR   t   lent   lstript   joint   lower(   t   ip_strt   unpack_ipv4t   error_messaget   best_doublecolon_startt   best_doublecolon_lent   doublecolon_startt   doublecolon_lent   ipv4_unpackedt   hextetst   indext   best_doublecolon_endt   result(    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyt   clean_ipv6_address	   sH    

	
c         C   sĘ   |  j    j d  s |  S|  j d  } d | d k r< |  Sd t | d d d !d	  t | d d d
 !d	  t | d d d !d	  t | d d d
 !d	  f } d j | d d ! } | d | 7} | S(   s?  
    Sanitize IPv4 mapping in an expanded IPv6 address.

    This converts ::ffff:0a0a:0a0a to ::ffff:10.10.10.10.
    If there is nothing to sanitize, returns an unchanged
    string.

    Args:
        ip_str: A string, the expanded IPv6 address.

    Returns:
        The sanitized output string, if applicable.
    s   0000:0000:0000:0000:0000:ffff:R   t   .i˙˙˙˙s   %d.%d.%d.%di   i    i   i   i   i   (   R   t
   startswithR   t   intR   (   R   R   t   ipv4_addressR   (    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyR
   Z   s    c         C   s-   |  j    j d  s d S|  j d d  d S(   sg  
    Unpack an IPv4 address that was mapped in a compressed IPv6 address.

    This converts 0000:0000:0000:0000:0000:ffff:10.10.10.10 to 10.10.10.10.
    If there is nothing to sanitize, returns None.

    Args:
        ip_str: A string, the expanded IPv6 address.

    Returns:
        The unpacked IPv4 address, or None if there was nothing to unpack.
    s   0000:0000:0000:0000:0000:ffff:R   i   N(   R   R   t   Nonet   rsplit(   R   (    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyR      s    c         C   sŞ  d d l  m } d |  k r  t S|  j d  d k r9 t Sd |  k rI t S|  j d  rh |  j d  s |  j d  r |  j d  r t S|  j d  d k r¤ t Sd |  k rá |  j d  d k rá |  j d  d	 k rá t Sn  t |   }  xś |  j d  D]Ľ } | j d  d	 k r[|  j d  d | k s5t Sy | |  Wq˘t k
 rWt SXqý y2 t	 | d
  d k  st	 | d
  d k rt SWqý t
 k
 rĄt SXqý Wt S(   sŹ   
    Ensure we have a valid IPv6 address.

    Args:
        ip_str: A string, the IPv6 address.

    Returns:
        A boolean, True if this is a valid IPv6 address.
    i˙˙˙˙(   t   validate_ipv4_addressR   s   ::i   s   :::i   R   i   i   i    i˙˙  (   t   django.core.validatorsR$   t   Falset   countR   t   endswithR	   R   R    R    t
   ValueErrort   True(   R   R$   t   hextet(    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyR      s<    
!*	c         C   s<  t  |   s |  Sg  } |  j d  } d |  j d  d k rG d } n d } t |  d k rä t | d j d   t | d j d   } | d j d  } x% t | |  D] } | j d	  qł W| | d j d  7} n |  j d  } g  } x3 | D]+ } | j d
 d t |  | j    q Wd j |  S(   s   
    Expand a shortened IPv6 address.

    Args:
        ip_str: A string, the IPv6 address.

    Returns:
        A string, the expanded IPv6 address.
    s   ::R   R   i˙˙˙˙i   i   i   i    t   0000R   i   (   t   _is_shorthand_ipR   R   R   t   appendR   R   (   R   t   new_ipR+   t   fill_tot   sept   __t   ret_ip(    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyR	   Ň   s$    
	0)c         C   s@   |  j  d  d k r t St d   |  j d  D  r< t St S(   sĽ   Determine if the address is shortened.

    Args:
        ip_str: A string, the IPv6 address.

    Returns:
        A boolean, True if the address is shortened.
    s   ::i   c         s   s!   |  ] } t  |  d  k  Vq d S(   i   N(   R   (   t   .0t   x(    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pys	   <genexpr>	  s    R   (   R'   R*   t   anyR   R&   (   R   (    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyR-   ţ   s
    	N(   t   django.core.exceptionsR    t   django.utils.six.movesR   t   django.utils.translationR   t   _R&   R   R
   R   R   R	   R-   (    (    (    s_   /var/www/html/phendo-backend/phendo_python/env/lib/python2.7/site-packages/django/utils/ipv6.pyt   <module>   s   P	%		@	,