U
    ARd0                     @   sz  d dl Z d dlZd dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ er\d dlmZ dd ee	d	d
D Ze dde dZeedddZdedddZeedddZeeeef dddZeedddZeddddd d!d"heed#d$d%h B Zd&d' Zeed(d)d*Z eed+d,d-Z!eed.d/d0Z"eed1d2d3Z#e$ed4d5d6Z%e d7Z&d8d9 Z'dS ):    N)datetime)TYPE_CHECKINGAnyTuplecast)
ALL_EMOJIS)StreamlitAPIException)SupportsStrc                 C   s   g | ]}t |qS  )reescape).0er
   r
   9/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/string_util.py
<listcomp>   s     r   T)reversez^(|z)[_ -]*(.*))stringreturnc                 C   s
   |  dS )zDecodes a string as ascii.ascii)decoder   r
   r
   r   decode_ascii"   s    r   r	   )textr   c                 C   s   t t|  S )z;Convert an object to text, dedent it, and strip whitespace.)textwrapdedentstrstripr   r
   r
   r   
clean_text'   s    r   c                 C   s   |  ddtkS )z'Check if input string is a valid emoji.u   ️ )replacer   r   r
   r
   r   is_emoji,   s    r"   c                 C   s:   t t| }|dkrd| fS tt|}|d|dfS )zReturn a tuple containing the first emoji found in the given string and
    the rest of the string (minus an optional separator between the two).
    Nr          )r   searchEMOJI_EXTRACTION_REGEXr   r   group)r   re_matchr
   r
   r   extract_leading_emoji1   s
    
r)   )
raw_stringr   c                 C   s6   ddddddddg}| }|D ]}| |d| }q|S )	aI  Returns a new string which escapes all markdown metacharacters.

    Args
    ----
    raw_string : str
        A string, possibly with markdown metacharacters, e.g. "1 * 2"

    Returns
    -------
    A string with all metacharacters escaped.

    Examples
    --------
    ::
        escape_markdown("1 * 2") -> "1 \\* 2"
    \*-=`!#r   )r!   )r*   Zmetacharactersresult	characterr
   r
   r   escape_markdown@   s
    r4         	   
                      c                 C   s   t | dtS )z8Guess if an input bytesarray can be encoded as a string.N)bool	translate	TEXTCHARS)inpr
   r
   r   is_binary_string[   s    rC   )namer   c                 C   s:   t |  dd}tdd|}|dkr6td|  |S )a|  
    Taken from https://github.com/django/django/blob/196a99da5d9c4c33a78259a58d38fb114a4d2ee8/django/utils/text.py#L225-L238

    Return the given string converted to a string that can be used for a clean
    filename. Remove leading and trailing spaces; convert other spaces to
    underscores; and remove anything that is not an alphanumeric, dash,
    underscore, or dot.
     _z(?u)[^-\w.]r    >   r    ...z$Could not derive file name from '%s')r   r   r!   r   subr   )rD   sr
   r
   r   clean_filenamea   s
    	rK   )snake_case_stringr   c              	   C   sX   |  d}g }|D ]:}|rz||  W q tk
rJ   || Y qX qd|S )z4Transform input string from snake_case to CamelCase.rF   r    )splitappendtitle	Exceptionjoin)rL   wordsZcapitalized_words_arrwordr
   r
   r   snake_case_to_camel_caser   s    
rT   )input_stringr   c                 C   s.   t  }| s|dS |  d|d S dS )zbAppend datetime string to input string.
    Returns datetime string if input is empty string.
    z%Y-%m-%d_%H-%M-%SrF   N)r   nowstrftime)rU   rV   r
   r
   r   append_date_time_to_string   s    
rX   )title_stringr   c                 C   s$   |  dd} t| }t|} t| S )z3Generated download filename from page title string.u    · Streamlitr    )r!   rK   rT   rX   )rY   Zfile_name_stringr
   r
   r   %generate_download_filename_from_title   s    rZ   )numr   c                 C   s\   t d| }d}t|dkr0|d7 }|d }qdd|dd	d
ddddg| S )z9Simplifies number into Human readable format, returns strz{:.2g}r   i  r#   g     @@z{}{}z{:f}0rG   r    kmbt)floatformatabsrstrip)r[   Znum_convertedZ	magnituder
   r
   r   simplify_number   s    
re   z0^\<[a-zA-Z_]+[a-zA-Z0-9<>._ ]* at 0x[0-9a-f]+\>$c                 C   s   t | rdS dS )zAReturns True if the string looks like <foo blarg at 0x15ee6f9a0>.TF)_OBJ_MEM_ADDRESSmatchr   r
   r
   r   is_mem_address_str   s    
rh   )(r   r   r   typingr   r   r   r   Zstreamlit.emojisr   Zstreamlit.errorsr   Zstreamlit.type_utilr	   sortedZESCAPED_EMOJIcompilerQ   r&   bytesr   r   r   r?   r"   r)   r4   	bytearraysetrangerA   rC   rK   rT   rX   rZ   intre   rf   rh   r
   r
   r
   r   <module>   s.   *
