ó
l]›]c           @` sA  d  Z  d d l m Z m Z m Z m Z d d l m Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z d d l m Z m Z m Z m Z m Z e
 j d k  rÒ d d	 l m Z m Z d d
 l m Z m Z n" d d l m Z m Z m Z m Z e d „ Z d „  Z d „  Z  e j! d e  ƒ d d „ Z" d d d „ Z$ d S(   u‡   
Functions to convert unicode IRIs into ASCII byte string URIs and back. Exports
the following items:

 - iri_to_uri()
 - uri_to_iri()
i    (   t   unicode_literalst   divisiont   absolute_importt   print_function(   t   idnaNi   (   t   unwrap(   t   byte_clst   str_clst	   type_namet   bytes_to_listt	   int_typesi   (   t   urlsplitt
   urlunsplit(   t   quotet   unquote(   R   t   unquote_to_bytesR   R   c         C` s	  t  |  t ƒ s- t t d t |  ƒ ƒ ƒ ‚ n  d } t j d k  rÜ |  j d ƒ rÜ |  j d ƒ rÜ d } t	 j
 d |  ƒ } | r¦ | j d ƒ } d |  t | ƒ }  n  t |  ƒ } | rè | |  d }  t | d  ƒ } qè n t |  ƒ } | d k rt | j ƒ } n  | j } | d k	 r-| j d	 ƒ } n  t | j d
 d ƒ} t | j d
 d ƒ} | j }	 |	 d k	 r„t |	 ƒ j d ƒ }	 n  d }
 | d k	 rÄ|
 | 7}
 | r·|
 d | 7}
 n  |
 d 7}
 n  | d k	 rÝ|
 | 7}
 n  |	 d k	 rB| d k oþ|	 d k } | d k o|	 d k } | s.| rB| rB|
 d |	 7}
 qBn  t | j d
 d ƒ} t | j d
 d ƒ} t | j d
 d ƒ} | r´| d k r´| d k r´| d k r´d } n  | d k rÉd } n  t | |
 | | | f ƒ } t  | t ƒ r| j d ƒ } n  | S(   uö   
    Encodes a unicode IRI into an ASCII byte string URI

    :param value:
        A unicode string of an IRI

    :param normalize:
        A bool that controls URI normalization

    :return:
        A byte string of the ASCII-encoded URI
    u@   
            value must be a unicode string, not %s
            i   i   u   http://u   https://u	   ^[^:]*://i    iýÿÿÿu   idnat   safeu   !$&'()*+,;=u   asciit    t   :t   @t   httpt   80t   httpst   443u   /!$&'()*+,;=@:u   /?!$&'()*+,;=@:t   /u    u   latin1N(   i   i   (   t
   isinstanceR   t	   TypeErrorR   R   t   Nonet   syst   version_infot
   startswitht   ret   matcht   groupt   lenR   t	   _urlquotet   schemet   hostnamet   encodet   usernamet   passwordt   portt   patht   queryt   fragmentR   (   t   valuet	   normalizeR$   t   real_prefixt   prefix_matcht   parsedR%   R'   R(   R)   t   netloct   default_httpt   default_httpsR*   R+   R,   t   output(    (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt
   iri_to_uri%   sd    /		
*		c         C` sË  t  |  t ƒ s- t t d t |  ƒ ƒ ƒ ‚ n  t |  ƒ } | j } | d k	 r` | j d ƒ } n  t	 | j
 d d d g ƒ} t	 | j d d d g ƒ} | j } | r· | j d ƒ } n  | j } | rè t  | t ƒ rè | j d ƒ } n  d } | d k	 r(| | 7} | r| d | 7} n  | d 7} n  | d k	 rA| | 7} n  | d k	 rd| d t | ƒ 7} n  t	 | j d d g d	 t ƒ} t	 | j d d
 d g d	 t ƒ}	 t	 | j ƒ }
 t | | | |	 |
 f ƒ S(   u³   
    Converts an ASCII URI byte string into a unicode IRI

    :param value:
        An ASCII-encoded byte string of the URI

    :return:
        A unicode string of the IRI
    u=   
            value must be a byte string, not %s
            u   asciit   remapu   :u   @u   idnau    u   /t   preserveu   &u   =N(   R   R   R   R   R   R   R$   R   t   decodet   _urlunquoteR'   R(   R%   R)   R
   R   R*   t   TrueR+   R,   R   (   R-   R1   R$   R'   R(   R%   R)   R2   R*   R+   R,   (    (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt
   uri_to_irix   s<    			
!c         C` sO   t  |  j |  j |  j !ƒ } g  | D] } d | ^ q# } d j | ƒ |  j f S(   u>  
    Error handler for decoding UTF-8 parts of a URI into an IRI. Leaves byte
    sequences encoded in %XX format, but as part of a unicode string.

    :param exc:
        The UnicodeDecodeError exception

    :return:
        A 2-element tuple of (replacement unicode string, integer index to
        resume at)
    u   %%%02xu    (   R	   t   objectt   startt   endt   join(   t   exct   bytes_as_intst   numt   replacements(    (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt   _iri_utf8_errors_handler¬   s    u   iriutf8u    c         ` s  |  d k s |  d k r d Sg  ‰  t j d |  ƒ r ‡ f d †  } t j d | |  ƒ }  ‡  f d †  } t j d | |  ƒ }  n  t |  j d ƒ d ˆ j d ƒ ƒ} t | t ƒ sÄ | j d ƒ } n  t ˆ  ƒ d	 k rý ‡  f d
 †  } t j d | | ƒ } n  | S(   u  
    Quotes a unicode string for use in a URL

    :param string:
        A unicode string

    :param safe:
        A unicode string of character to not encode

    :return:
        None (if string is None) or an ASCII byte string of the quoted string
    u    u   %[0-9a-fA-F]{2}c         ` s^   t  |  j d ƒ ƒ } | j d d ƒ } x0 t ˆ  ƒ D]" } | j | d t | ƒ ƒ } q4 W| S(   Ni    u   utf-8u   iriutf8u   %%%02x(   R   R!   R9   t   listt   replacet   ord(   R    t   byte_stringt   unicode_stringt	   safe_char(   R   (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt   _try_unescapeÙ   s
     u   (?:%[0-9a-fA-F]{2})+c         ` s#   ˆ  j  |  j d ƒ j d ƒ ƒ d S(   Ni    u   asciiu    (   t   appendR!   R&   (   R    (   t   escapes(    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt   _extract_escapeã   s    u   utf-8R   u   asciii    c         ` s   ˆ  j  d ƒ S(   Ni    (   t   pop(   t   _(   RN   (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt   _return_escapeî   s    s   %00N(	   R   R   t   searcht   subt   urlquoteR&   R   R   R"   (   t   stringR   RL   RO   R5   RR   (    (   RN   R   s.   lib/python2.7/site-packages/asn1crypto/_iri.pyR#   Á   s    $c   	      C` s4  |  d k r |  S|  d k r  d S| r’ d d d d d g } i  } xN | D]C } | j d ƒ } | | | <|  j | j d	 ƒ | j d	 ƒ ƒ }  qH Wn  t |  ƒ }  | ræ x? | D]4 } |  j | j d	 ƒ d
 t | ƒ j d	 ƒ ƒ }  q« Wn  |  j d d ƒ } | r0x/ | j ƒ  D] \ } } | j | | ƒ } qWn  | S(   u  
    Unquotes a URI portion from a byte string into unicode using UTF-8

    :param byte_string:
        A byte string of the data to unquote

    :param remap:
        A list of characters (as unicode) that should be re-mapped to a
        %XX encoding. This is used when characters are not valid in part of a
        URL.

    :param preserve:
        A bool - indicates that the chars to be remapped if they occur in
        non-hex form, should be preserved. E.g. / for URL path.

    :return:
        A unicode string
    R   u    u   u   u   u   u   i    u   asciiu   %%%02xu   utf-8u   iriutf8N(   R   RP   RG   R&   R   RH   R9   t   items(	   RI   R7   R8   RD   t   preserve_unmapt   chart   replacementR5   t   original(    (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyR:   õ   s(    
+5(   i   (%   t   __doc__t
   __future__R    R   R   R   t	   encodingsR   t   codecsR   R   t   _errorsR   t   _typesR   R   R   R	   R
   R   t   urlparseR   R   t   urllibR   RU   R   R   t   urllib.parset   FalseR6   R<   RE   t   register_errorR#   R   R:   (    (    (    s.   lib/python2.7/site-packages/asn1crypto/_iri.pyt   <module>	   s"   "("S	4	4