
<OX!                 @   sM  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 d d   Z d d   Z d d   Z e j  d e  d d d  Z! d d d d  Z" d S)z
Functions to convert unicode IRIs into ASCII byte string URIs and back. Exports
the following items:

 - iri_to_uri()
 - uri_to_iri()
    )unicode_literalsdivisionabsolute_importprint_function)idnaN   )unwrap)byte_clsstr_cls	type_namebytes_to_list	int_types   )urlsplit
urlunsplit)quoteunquote)r   unquote_to_bytesr   r   c             C   s  t  |  t  s* t t d t |      d } t j d k  r |  j d  r |  j d  r d } t j	 d |   } | r | j
 d  } d |  t |  d  }  t |   } | r | |  d d  }  t | d d   } n t |   } | d k rt | j  } | j } | d k	 r0| j d
  } t | j d d } t | j d d } | j } | d k	 rt |  j d  } d }	 | d k	 r|	 | 7}	 | r|	 d | 7}	 |	 d 7}	 | d k	 r|	 | 7}	 | d k	 r,| d k o| d k }
 | d k o| d k } |
 r,| r,|	 d | 7}	 t | j d d } t | j d d } t | j d d } | d k r| d k r| d k rd } | d k rd } t | |	 | | | f  } t  | t  r| j d  } | S)z
    Normalizes and encodes a unicode IRI into an ASCII byte string URI

    :param value:
        A unicode string of an IRI

    :return:
        A byte string of the ASCII-encoded URI
    z@
            value must be a unicode string, not %s
            N      zhttp://zhttps://z	^[^:]*://r   r   r   safez!$&'()*+,;=ascii       :   @s   https   80s   httpss   443z/!$&'()*+,;=@:z/?!$&'()*+,;=@:   / latin1)r   r   )
isinstancer
   	TypeErrorr   r   sysversion_info
startswithrematchgrouplenr   	_urlquoteschemehostnameencodeusernamepasswordportpathqueryfragmentr   )valuer)   Zreal_prefixZprefix_matchparsedr*   r,   r-   r.   netlocZdefault_httpZdefault_httpsr/   r0   r1   output r6   1/usr/lib/python3/dist-packages/asn1crypto/_iri.py
iri_to_uri%   sd    /		


$r8   c             C   s  t  |  t  s* t t d t |      t |   } | j } | d k	 rZ | j d  } t | j	 d d d g } t | j
 d d d g } | j } | r | j d  } | j } | r t  | t  r | j d  } d } | d k	 r| | 7} | r| d | 7} | d 7} | d k	 r,| | 7} | d k	 rL| d t |  7} t | j d d	 g d
 d } t | j d d d g d
 d }	 t | j  }
 t | | | |	 |
 f  S)z
    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
    z=
            value must be a byte string, not %s
            Nr   remap:@r   r   /preserveT&=)r   r	   r    r   r   r   r)   decode_urlunquoter,   r-   r*   r.   r   r
   r/   r0   r1   r   )r2   r3   r)   r,   r-   r*   r.   r4   r/   r0   r1   r6   r6   r7   
uri_to_iriu   s<    			


!rB   c             C   sH   t  |  j |  j |  j   } d d   | D } d j |  |  j f S)a>  
    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)
    c             S   s   g  |  ] } d  |  q S)z%%%02xr6   ).0Znumr6   r6   r7   
<listcomp>   s   	 z,_iri_utf8_errors_handler.<locals>.<listcomp>r   )r   objectstartendjoin)excZbytes_as_intsreplacementsr6   r6   r7   _iri_utf8_errors_handler   s    rK   iriutf8r   c                s  |  d k s |  d k r d Sg    t  j d |   r  f d d   } t  j d | |   }    f d d   } t  j d | |   }  t |  j d	  d
  j d	  } t | t  s | j d  } t    d k r   f d d   } t  j d | |  } | S)a  
    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
    Nr   z%[0-9a-fA-F]{2}c                s^   t  |  j d   } | j d d  } x0 t    D]" } | j | d t |   } q4 W| S)Nr   zutf-8rL   z%%%02x)r   r&   r@   listreplaceord)r%   byte_stringZunicode_stringZ	safe_char)r   r6   r7   _try_unescape   s
     z _urlquote.<locals>._try_unescapez(?:%[0-9a-fA-F]{2})+c                s#     j  |  j d  j d   d S)Nr   r    )appendr&   r+   )r%   )escapesr6   r7   _extract_escape   s    z"_urlquote.<locals>._extract_escapezutf-8r   r   r   c                s     j  d  S)Nr   )pop)_)rT   r6   r7   _return_escape   s    z!_urlquote.<locals>._return_escapes   %00)r$   searchsuburlquoter+   r   r	   r'   )stringr   rQ   rU   r5   rX   r6   )rT   r   r7   r(      s    $r(   c       	      C   s+  |  d k r |  S|  d k r  d S| r d d d d d g } i  } xK | D]C } | j  d	  } | | | <|  j | j d
  | j d
   }  qH Wt |   }  | r x< | D]4 } |  j | j d
  d t |  j d
   }  q W|  j d d  } | r'x, | j   D] \ } } | j | |  } qW| S)a  
    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
    Nr   r   r   r   z%%%02xzutf-8rL   )rV   rN   r+   r   rO   r@   items)	rP   r9   r=   rJ   Zpreserve_unmapcharZreplacementr5   Zoriginalr6   r6   r7   rA      s(    
(2rA   )r   )#__doc__Z
__future__r   r   r   r   	encodingsr   codecsr$   r!   Z_errorsr   Z_typesr	   r
   r   r   r   r"   Zurlparser   r   Zurllibr   r[   r   r   Zurllib.parser8   rB   rK   register_errorr(   rA   r6   r6   r6   r7   <module>	   s"   "("P44