3 œäL]´!ã@sèdZddlmZmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddl mZmZmZmZmZe jdkr”dd lmZmZdd lmZmZndd lmZmZmZmZd d „Zdd„Zdd„Zej deƒddd„Z!ddd„Z"dS)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_bytesrrcCs t|tƒsttdt|ƒƒƒ‚d}tjdkr¢|jdƒ r¢|jdƒ r¢d}tj d|ƒ}|rt|j dƒ}d|t |ƒd…}t |ƒ}|rª||dd…}t |dd…ƒ}nt |ƒ}|dkr¼t |jƒ}|j}|dk rÔ|jd ƒ}t |jd d �}t |jd d �}|j}|dk �rt|ƒjd ƒ}d} |dk �r>| |7} |�r6| d|7} | d7} |dk �rP| |7} |dk �rš|dk�oj|dk} |dk�o||dk} | �rš| �rš| d|7} t |jdd �} t |jdd �} t |jdd �}| dk�ræ|dk�ræ| dk�ræd} | dk�rôd} t|| | | |fƒ}t|tƒ�r|jdƒ}|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éézhttp://zhttps://z ^[^:]*://rrrz !$&'()*+,;=)ÚsafeÚasciióó:ó@shttps80shttpss443z/!$&'()*+,;=@:z/?!$&'()*+,;=@:ó/ÚÚlatin1)rréýÿÿÿ)Ú isinstancer Ú TypeErrorrr ÚsysÚ version_infoÚ startswithÚreÚmatchÚgroupÚlenrÚ _urlquoteÚschemeÚhostnameÚencodeÚusernameÚpasswordÚportÚpathÚqueryÚfragmentr)Úvaluer)Ú real_prefixZ prefix_matchÚparsedr*r,r-r.ÚnetlocZ default_httpZ default_httpsr/r0r1Úoutput©r7ú7/tmp/pip-install-wfra5znf/asn1crypto/asn1crypto/_iri.pyÚ iri_to_uri%sd  "             r9c Cs*t|tƒsttdt|ƒƒƒ‚t|ƒ}|j}|dk r<|jdƒ}t|j ddgd�}t|j ddgd�}|j }|rt|jdƒ}|j }|r”t|t ƒ r”|jdƒ}d}|dk rÀ||7}|r¸|d|7}|d7}|dk rÐ||7}|dk rè|dt|ƒ7}t|jd gd d �}t|jd d gd 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ú:ú@)Úremaprrú/T)r<Úpreserveú&ú=)rr r rr rr)ÚdecodeÚ _urlunquoter,r-r*r.r r r/r0r1r) r2r4r)r,r-r*r.r5r/r0r1r7r7r8Ú uri_to_irius<       rCcCs4t|j|j|j…ƒ}dd„|Dƒ}dj|ƒ|jfS)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) cSsg|] }d|‘qS)z%%%02xr7)Ú.0Únumr7r7r8ú ·sz,_iri_utf8_errors_handler..r)r ÚobjectÚstartÚendÚjoin)ÚexcZ bytes_as_intsÚ replacementsr7r7r8Ú_iri_utf8_errors_handler©s rMÚiriutf8rcs®|dks|dkrdSg‰tjd|ƒrX‡fdd„}tjd||ƒ}‡fdd„}tjd||ƒ}t|jd ƒˆjd ƒd �}t|tƒs„|jd ƒ}tˆƒd krª‡fd d„}tjd||ƒ}|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 Nrz%[0-9a-fA-F]{2}csDt|jdƒƒ}|jddƒ}x$tˆƒD]}|j|dt|ƒƒ}q$W|S)Nrzutf-8rNz%%%02x)rr&rAÚlistÚreplaceÚord)r%Ú byte_stringZunicode_stringZ safe_char)rr7r8Ú _try_unescapeÖs  z _urlquote.._try_unescapez(?:%[0-9a-fA-F]{2})+csˆj|jdƒjdƒƒdS)Nrrú)Úappendr&r+)r%)Úescapesr7r8Ú_extract_escapeàsz"_urlquote.._extract_escapezutf-8)rrrcs ˆjdƒS)Nr)Úpop)Ú_)rVr7r8Ú_return_escapeësz!_urlquote.._return_escapes%00)r$ÚsearchÚsubÚurlquoter+rr r')ÚstringrrSrWr6rZr7)rVrr8r(¾s       r(c CsÖ|dkr |S|dkrdS|rfdddddg}i}x6|D].}|jd ƒ}|||<|j|jd ƒ|jd ƒƒ}q4Wt|ƒ}|r x,|D]$}|j|jd ƒd t|ƒjd ƒƒ}qxW|jd 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 Nrrúúúúúrrz%%%02xzutf-8rN)rXrPr+rrQrAÚitems) rRr<r>rLZpreserve_unmapÚcharÚ replacementr6Úoriginalr7r7r8rBòs(   $ rB)r)r)NN)#Ú__doc__Ú __future__rrrrÚ encodingsrÚcodecsr$r!Ú_errorsrÚ_typesr r r r r r"ÚurlparserrÚurllibrr]rrÚ urllib.parser9rCrMÚregister_errorr(rBr7r7r7r8Ú s"   P4  4