
\[\#                 @   s  d  d l  Z  d  d l Z d  d l Z d  d l m Z e j d  Z d Z d Z d d   Z	 d d	   Z
 d
 d   Z Gd d   d e j  Z Gd d   d e j  Z Gd d   d e j  Z Gd d   d e e j  Z Gd d   d e e j  Z d d   Z d S)    N)	ucd_3_2_0u   [.。．｡]s   xn--zxn--c             C   s  g  } x9 |  D]1 } t  j |  r( q n  | j t  j |   q Wd j |  }  t j d |   }  x |  D] } t  j |  s t  j |  s t  j	 |  s t  j
 |  s t  j |  s t  j |  s t  j |  s t  j |  s t  j |  rj t d |   qj qj Wd d   |  D } xd | D]\ } | r(t d d   |  D  r\t d   n  | d	 sr| d rt d   qq(q(W|  S)N ZNFKCzInvalid character %rc             S   s   g  |  ] } t  j |   q S )
stringprepZin_table_d1).0xr   r   $/usr/lib/python3.4/encodings/idna.py
<listcomp>)   s   	 znameprep.<locals>.<listcomp>c             s   s   |  ] } t  j |  Vq d  S)N)r   Zin_table_d2)r   r   r   r   r   	<genexpr>2   s    znameprep.<locals>.<genexpr>zViolation of BIDI requirement 2r      zViolation of BIDI requirement 3)r   Zin_table_b1appendZmap_table_b2joinunicodedataZ	normalizeZin_table_c12Zin_table_c22Zin_table_c3Zin_table_c4Zin_table_c5Zin_table_c6Zin_table_c7Zin_table_c8Zin_table_c9UnicodeErrorany)labelZnewlabelcZRandALr   r   r   nameprep   s4    r   c             C   s-  y |  j  d  }  Wn t k
 r' Yn3 Xd t |   k  oE d k  n rN |  St d   t |   }  y |  j  d  }  Wn t k
 r Yn3 Xd t |   k  o d k  n r |  St d   |  j t  r t d   n  |  j  d  }  t |  }  d t |   k  od k  n r|  St d   d  S)Nasciir   @   zlabel empty or too longzLabel starts with ACE prefixpunycode)encoder   lenr   
startswithsace_prefix
ace_prefix)r   r   r   r   ToASCII>   s,    ""
"r   c             C   s  t  |  t  r d } n4 y |  j d  }  d } Wn t k
 rK d } Yn X| s t |   }  y |  j d  }  Wq t k
 r t d   Yq Xn  |  j t  s t |  d  S|  t t  d   } | j	 d  } t
 |  } t |  d  j   t | d  k rt d |  |   n  | S)NTr   FzInvalid character in IDN labelr   zIDNA does not round-trip)
isinstancebytesr   r   r   r   r   strr   decoder   lower)r   Z
pure_asciiZlabel1resultZlabel2r   r   r   	ToUnicodei   s*    	
$r$   c               @   s.   e  Z d  Z d d d  Z d d d  Z d S)Codecstrictc             C   st  | d k r t  d |   n  | s) d Sy | j d  } Wn t k
 rP Yn X| j d  } xI | d  d  D]7 } d t |  k  o d k  n sq t  d	   qq qq Wt | d  d k r t  d
   n  | t |  f St   } t j |  } | r| d rd } | d =n d } x7 | D]/ } | rC| j d  n  | j t |   q'Wt	 | |  t |  f S)Nr&   zunsupported error handling     r   r      .r   r   zlabel empty or too longzlabel too long)r'   r   r   r   r   r   )
r   r   UnicodeEncodeErrorsplitr   	bytearraydotsextendr   r   )selfinputerrorsr#   labelsr   trailing_dotr   r   r   r      s4    "	
zCodec.encodec             C   s  | d k r t  d |   n  | s) d	 St | t  sG t |  } n  t | k r y | j d  t |  f SWq t k
 r Yq Xn  | j d  } | r t | d
  d k r d } | d =n d } g  } x! | D] } | j t	 |   q Wd j
 |  | t |  f S)Nr&   zUnsupported error handling r   r   r   r(   r   .)r   r   r   r   )r   r   r   r   r!   r   UnicodeDecodeErrorr*   r   r$   r   )r.   r/   r0   r1   r2   r#   r   r   r   r   r!      s(    
zCodec.decodeN)__name__
__module____qualname__r   r!   r   r   r   r   r%      s   %r%   c               @   s   e  Z d  Z d d   Z d S)IncrementalEncoderc       	      C   s  | d k r t  d |   n  | s) d St j |  } d } | r | d s^ d } | d	 =q | s | d
 =| r} d } q} q n  t   } d } xQ | D]I } | r | j d  | d 7} n  | j t |   | t |  7} q W| | 7} | t |  7} t |  | f S)Nr&   zunsupported error handling r'   r   r   r(   )r'   r   r   r   r   )r   r,   r*   r+   r-   r   r   r   )	r.   r/   r0   finalr1   r2   r#   sizer   r   r   r   _buffer_encode   s2    

	
z!IncrementalEncoder._buffer_encodeN)r5   r6   r7   r;   r   r   r   r   r8      s   r8   c               @   s   e  Z d  Z d d   Z d S)IncrementalDecoderc       	      C   s0  | d k r t  d |   n  | s) d St | t  rJ t j |  } n t | d  } | j d  } d } | r | d	 s d } | d
 =q | s | d =| r d } q q n  g  } d } xD | D]< } | j t |   | r | d 7} n  | t |  7} q Wd j |  | } | t |  7} | | f S)Nr&   zUnsupported error handling r   r   r   r3   r   )r   r   r   r   r   )	r   r   r    r,   r*   r   r$   r   r   )	r.   r/   r0   r9   r1   r2   r#   r:   r   r   r   r   _buffer_decode   s6    

z!IncrementalDecoder._buffer_decodeN)r5   r6   r7   r=   r   r   r   r   r<      s   r<   c               @   s   e  Z d  Z d S)StreamWriterN)r5   r6   r7   r   r   r   r   r>   $  s   r>   c               @   s   e  Z d  Z d S)StreamReaderN)r5   r6   r7   r   r   r   r   r?   '  s   r?   c               C   s@   t  j d d d t   j d t   j d t d t d t d t  S)	NnameZidnar   r!   incrementalencoderincrementaldecoderstreamwriterstreamreader)	codecs	CodecInfor%   r   r!   r8   r<   r>   r?   r   r   r   r   getregentry,  s    	rG   )r   rerE   r   r   compiler,   r   r   r   r   r$   r%   BufferedIncrementalEncoderr8   BufferedIncrementalDecoderr<   r>   r?   rG   r   r   r   r   <module>   s   $0+)H#'