U
    d3                     @   s
  d Z ddlZddlZddlZddlZddlmZ ddl	Z	ddl	m
Z
mZmZmZmZmZ edZdddd	d
dZe
eef edddZe
eef edddZeedddZe
eef edddZeedddZd?e
eef eedddZe	jd@e
eef deedddZe	jdAe
eef eeedddZdBe
eef ee ee
eef dd dZdCe
eef eeeeee f d"d#d$ZeedfZ e	jeedd%d&Z!e	jeedd'd&Z!e	jdddd(d&Z!e
deef ee dd)d&Z!eedfZ"e	jeedd*d+Z#e	jeedd,d+Z#e	jdddd-d+Z#e
deef ee dd.d+Z#e#Z$e#Z%e#Z&eed/d0d1Z'ee#d2Z(d!d3d!d4d5gfe
eef ee
eeegef f eee ed6d7d8Z)e	j*ed9d:d;Z+eeef d<d=d>Z,e, Z-dS )DzEscaping/unescaping methods for HTML, JSON, URLs, and others.

Also includes a few other miscellaneous string manipulation functions that
have crept in over time.
    N)unicode_type)UnionAnyOptionalDictListCallablez[&<>"']z&amp;z&lt;z&gt;z&quot;z&#39;)&<>"')valuereturnc                 C   s   t dd t| S )a0  Escapes a string so it is valid within HTML or XML.

    Escapes the characters ``<``, ``>``, ``"``, ``'``, and ``&``.
    When used in attribute values the escaped strings must be enclosed
    in quotes.

    .. versionchanged:: 3.2

       Added the single quote to the list of escaped characters.
    c                 S   s   t | d S )Nr   )_XHTML_ESCAPE_DICTgroup)match r   2/tmp/pip-unpacked-wheel-fekwu36z/tornado/escape.py<lambda>7       zxhtml_escape.<locals>.<lambda>)_XHTML_ESCAPE_REsubto_basestringr   r   r   r   xhtml_escape+   s     r   c                 C   s   t dtt| S )z!Un-escapes an XML-escaped string.z&(#?)(\w+?);)rer   _convert_entity_unicoder   r   r   r   xhtml_unescape;   s    r   c                 C   s   t | ddS )z%JSON-encodes the given Python object.z</z<\/)jsondumpsreplacer   r   r   r   json_encodeC   s    r#   c                 C   s   t t| S )zcReturns Python objects for the given JSON string.

    Supports both `str` and `bytes` inputs.
    )r    loadsr   r   r   r   r   json_decodeN   s    r%   c                 C   s   t dd|  S )z>Replace all sequences of whitespace chars with a single space.z[\x00-\x20]+ )r   r   stripr   r   r   r   squeezeV   s    r(   T)r   plusr   c                 C   s    |rt jjnt jj}|t| S )ai  Returns a URL-encoded version of the given value.

    If ``plus`` is true (the default), spaces will be represented
    as "+" instead of "%20".  This is appropriate for query strings
    but not for the path component of a URL.  Note that this default
    is the reverse of Python's urllib module.

    .. versionadded:: 3.1
        The ``plus`` argument
    )urllibparse
quote_plusquoteutf8)r   r)   r-   r   r   r   
url_escape[   s    r/   )r   encodingr)   r   c                 C   s   d S Nr   r   r0   r)   r   r   r   url_unescapej   s    r3   utf-8c                 C   s   d S r1   r   r2   r   r   r   r3   o   s    c                 C   sP   |dkr(|rt | dd} tj| S |r4tjjntjj}|t | |dS dS )a^  Decodes the given value from a URL.

    The argument may be either a byte or unicode string.

    If encoding is None, the result will be a byte string.  Otherwise,
    the result is a unicode string in the specified encoding.

    If ``plus`` is true (the default), plus signs will be interpreted
    as spaces (literal plus signs must be represented as "%2B").  This
    is appropriate for query strings and form-encoded values but not
    for the path component of a URL.  Note that this default is the
    reverse of Python's urllib module.

    .. versionadded:: 3.1
       The ``plus`` argument
    N+r&   )r0   )r   r"   r*   r+   unquote_to_bytesunquote_plusunquote)r   r0   r)   r8   r   r   r   r3   v   s    F)qskeep_blank_valuesstrict_parsingr   c                 C   sV   t | tr| d} tjj| ||ddd}i }| D ]\}}dd |D ||< q6|S )a/  Parses a query string like urlparse.parse_qs,
    but takes bytes and returns the values as byte strings.

    Keys still become type str (interpreted as latin1 in python3!)
    because it's too painful to keep them as byte strings in
    python3 and in practice they're nearly always ascii anyway.
    latin1strict)r0   errorsc                 S   s   g | ]}| d qS )r<   )encode.0ir   r   r   
<listcomp>   s     z"parse_qs_bytes.<locals>.<listcomp>)
isinstancebytesdecoder*   r+   parse_qsitems)r9   r:   r;   resultencodedkvr   r   r   parse_qs_bytes   s    

    rM   c                 C   s   d S r1   r   r   r   r   r   r.      s    r.   c                 C   s   d S r1   r   r   r   r   r   r.      s    c                 C   s   d S r1   r   r   r   r   r   r.      s    c                 C   s2   t | tr| S t | ts(tdt|  | dS )zConverts a string argument to a byte string.

    If the argument is already a byte string or None, it is returned unchanged.
    Otherwise it must be a unicode string and is encoded as utf8.
    (Expected bytes, unicode, or None; got %rr4   )rD   _UTF8_TYPESr   	TypeErrortyper?   r   r   r   r   r.      s
    

c                 C   s   d S r1   r   r   r   r   r   
to_unicode   s    rR   c                 C   s   d S r1   r   r   r   r   r   rR      s    c                 C   s   d S r1   r   r   r   r   r   rR      s    c                 C   s2   t | tr| S t | ts(tdt|  | dS )zConverts a string argument to a unicode string.

    If the argument is already a unicode string or None, it is returned
    unchanged.  Otherwise it must be a byte string and is decoded as utf8.
    rN   r4   )rD   _TO_UNICODE_TYPESrE   rP   rQ   rF   r   r   r   r   rR      s
    

)objr   c                 C   sr   t | tr tdd |  D S t | tr<tdd | D S t | trXtdd | D S t | trjt| S | S dS )zvWalks a simple data structure, converting byte strings to unicode.

    Supports lists, tuples, and dictionaries.
    c                 s   s"   | ]\}}t |t |fV  qd S r1   recursive_unicode)rA   rK   rL   r   r   r   	<genexpr>   s    z$recursive_unicode.<locals>.<genexpr>c                 s   s   | ]}t |V  qd S r1   rU   r@   r   r   r   rW      s     c                 s   s   | ]}t |V  qd S r1   rU   r@   r   r   r   rW      s     N)rD   dictrH   listtuplerE   rR   )rT   r   r   r   rV      s    



rV   z\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&amp;|&quot;)*(?:[^!"#$%&'()*+,.:;<=>?@\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&amp;|&quot;)*\)))+) httphttps)textshortenextra_paramsrequire_protocolpermitted_protocolsr   c                    sL    rt  sd    tjtd fdd}tt| } t|| S )a  Converts plain text into HTML with links.

    For example: ``linkify("Hello http://tornadoweb.org!")`` would return
    ``Hello <a href="http://tornadoweb.org">http://tornadoweb.org</a>!``

    Parameters:

    * ``shorten``: Long urls will be shortened for display.

    * ``extra_params``: Extra text to include in the link tag, or a callable
      taking the link as an argument and returning the extra text
      e.g. ``linkify(text, extra_params='rel="nofollow" class="external"')``,
      or::

          def extra_params_cb(url):
              if url.startswith("http://example.com"):
                  return 'class="internal"'
              else:
                  return 'class="external" rel="nofollow"'
          linkify(text, extra_params=extra_params_cb)

    * ``require_protocol``: Only linkify urls which include a protocol. If
      this is False, urls such as www.facebook.com will also be linkified.

    * ``permitted_protocols``: List (or set) of protocols which should be
      linkified, e.g. ``linkify(text, permitted_protocols=["http", "ftp",
      "mailto"])``. It is very unsafe to include protocols such as
      ``javascript``.
    r&   mr   c           
         s  |  d}|  d}r |s |S |r0|kr0|S |  d}|sFd| }t r`d |  }n }d}r|t||kr||}|rt|d t|  dpd }nd}||d  d	}t|dkr|d | |d  d	 |d d d
 dd dd  }t||d kr"|d | }||kr||d}	|	|d krP|d |	 }|d7 }t|t|krp|}n|d| 7 }d|||f S )N      zhttp://r&         r[   r   /   ?.g      ?r	      z...z title="%s"z<a href="%s"%s>%s</a>)r   callabler'   lensplitrfind)
rd   urlprotohrefparamsmax_lenZbefore_clipZ	proto_lenpartsampr`   rb   ra   r_   r   r   	make_link:  sN    


 
"

zlinkify.<locals>.make_link)	rn   r'   typingMatchstrr   r   _URL_REr   )r^   r_   r`   ra   rb   rz   r   ry   r   linkify  s
    $>r   rc   c                 C   s   |  ddkrzP|  dd d  dkrHtt|  ddd  dW S tt|  dW S W n" tk
r   d|  d  Y S X zt|  d W S  tk
r   d|  d  Y S X d S )Nre   #rf   x   z&#%s;z&%s;)r   lowerchrint
ValueError_HTML_UNICODE_MAPKeyError)rd   r   r   r   r   |  s    r   )r   c                  C   s*   i } t jj D ]\}}t|| |< q| S r1   )htmlentitiesname2codepointrH   r   )Zunicode_mapnamer   r   r   r   _build_unicode_map  s    r   )T)T)r4   T)r4   T)FF).__doc__html.entitiesr   r    r   urllib.parser*   Ztornado.utilr   r{   r   r   r   r   r   r   compiler   r   r}   rE   r   r   r#   r%   r(   boolr/   overloadr3   rM   rQ   rO   r.   rS   rR   r   Z
native_strr   rV   r~   r   r|   r   r   r   r   r   r   r   <module>   s    
	    
     
  
   
  	
i