U BB`"@sdZddlmZmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddl mZmZmZmZmZe jdkrdd lmZmZdd lmZmZndd lmZmZmZmZdd dZddZddZe dedddZ!dddZ"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_literalsdivisionabsolute_importprint_function)idnaN)unwrap)byte_clsstr_cls type_name bytes_to_list int_types))urlsplit urlunsplit)quoteunquote)runquote_to_bytesrrFcCs$t|tsttdt|d}tjdkr|ds|dsd}t d|}|rp| d}d|t |d}t |}|r||dd}t |dd }nt |}|dkrt |j}|j}|dk r|d }t |jd d }t |jd d }|j} | dk r t| d } d} |dk r:| |7} |r2| d|7} | d7} |dk rL| |7} | dk r|dkof| dk} |dkox| dk} |r| s| s| d| 7} t |jdd } t |jdd }t |jdd }|r|dkr|dkr| dkrd} | dkrd} t|| | ||f}t|tr |d}|S)z 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 z@ value must be a unicode string, not %s N)zhttp://zhttps://z ^[^:]*://rrrz !$&'()*+,;=safeascii:@shttps80shttpss443z/!$&'()*+,;=@:z/?!$&'()*+,;=@:/latin1) isinstancer TypeErrorrr sys version_info startswithrematchgrouplenr _urlquoteschemehostnameencodeusernamepasswordportpathqueryfragmentr)value normalizer* real_prefixZ prefix_matchparsedr+r-r.r/netlocZ default_httpZ default_httpsr0r1r2outputr9^C:\Users\shjeevan\PycharmProjects\Verisk-XactWare\venv3.8\Lib\site-packages\asn1crypto/_iri.py iri_to_uri%sf           $   r;c Cs(t|tsttdt|t|}|j}|dk r<|d}t|j ddgd}t|j ddgd}|j }|rt|d}|j }|rt|t s|d}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|||| | fS)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&=)r r r!rr rr*decode _urlunquoter-r.r+r/r r r0r1r2r) r3r6r*r-r.r+r/r7r0r1r2r9r9r: uri_to_irixs>      rEcCs4t|j|j|j}dd|D}d||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)%%%02xr9).0numr9r9r: sz,_iri_utf8_errors_handler..r)r objectstartendjoin)excZ bytes_as_ints replacementsr9r9r:_iri_utf8_errors_handlers rPiriutf8rcs|dks|dkrdSgtd|rXfdd}td||}fdd}td||}t|d d d }t|ts|d }td krfd d}td||}|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}cs@t|d}|dd}tD]}||dt|}q"|S)Nrutf-8rQrF)rr'rClistreplaceord)r& byte_stringZunicode_stringZ safe_charrr9r: _try_unescapes   z _urlquote.._try_unescapez(?:%[0-9a-fA-F]{2})+cs|dddS)Nrr)appendr'r,)r&escapesr9r:_extract_escapesz"_urlquote.._extract_escaperRrrrcs dS)Nr)pop)_rZr9r:_return_escapesz!_urlquote.._return_escapes%00)r%searchsuburlquoter,r r r()stringrrWr\r8r_r9)r[rr:r)s       r)c Cs|dkr |S|dkrdS|rbdddddg}i}|D].}|d }|||<||d |d }q2t|}|r|D]$}||d d t|d }qr|d d }|r|D]\}}|||}q|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 NrrrrrFrRrQ)r]rTr,rrUrCitems) rVr>r@rOZpreserve_unmapchar replacementr8originalr9r9r:rDs( " rD)F)r)NN)#__doc__ __future__rrrr encodingsrcodecsr%r"_errorsr_typesr r r r r r#urlparserrurllibrrbrr urllib.parser;rErPregister_errorr)rDr9r9r9r:s"    S4  4