U
    ARd                     @  sT  U d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ dZded	< d
Zded< dd Zdd Zdd Zdd ZdddddZdddddZedZdddd d!d"Z ed#e!d$Z"d%d&d'd(d)Z#G d*d+ d+e$Z%d,dd-d.d/Z&d0d1d0d2d3d4Z'd0dd5d6d7d8Z(dS )9zA bunch of useful utilities.    )annotationsN)	AnyDictIterableListMappingSetTypeVarUnioncast)Final)env_utilzhttps://docs.streamlit.io/r   HELP_DOCgdy=zFinal[float]FLOAT_EQUALITY_EPSILONc                   s    g t   fdd}|S )z2Decorator to memoize the result of a no-args func.c                     s   s    d S Nr   )append funcresultr   2/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/util.pywrapped_func'   s    zmemoize.<locals>.wrapped_func)	functoolswraps)r   r   r   r   r   memoize#   s    r   c                 C  sl   t jrt|  dS t jr<t dr0td|  dS t|  dS t jrPtd|  dS ddl}td|	  dS )a  Open a web browser pointing to a given URL.

    We use this function instead of Python's `webbrowser` module because this
    way we can capture stdout/stderr to avoid polluting the terminal with the
    browser's messages. For example, Chrome always prints things like "Created
    new window in existing browser session", and those get on the user's way.

    url : str
        The URL. Must include the protocol.

    Nzxdg-openopenr   z$Cannot open browser in platform "%s")
r   Z
IS_WINDOWS_open_browser_with_webbrowserZIS_LINUX_OR_BSDZis_executable_in_path_open_browser_with_commandZ	IS_DARWINplatformErrorsystem)urlr   r   r   r   open_browser0   s    


r"   c                 C  s   dd l }||  d S r   )
webbrowserr   )r!   r#   r   r   r   r   Z   s    r   c              	   C  s6   | |g}t tjd}tj||tjd W 5 Q R X d S )Nw)stdoutstderr)r   osdevnull
subprocessPopenSTDOUT)commandr!   Zcmd_liner(   r   r   r   r   `   s    r   r   )itemreturnc                 C  s   t | trt| S | S )z;Convert a tuple to a list. Leave as is if it's not a tuple.)
isinstancetuplelist)r-   r   r   r   _maybe_tuple_to_listf   s    
r2   str)selfr.   c                   s~   j j}dddg t t g trB fddtD }n fddj D }d	dd |D }| d	| d
S )zA clean repr for a class, excluding both values that are likely defaults,
    and those explicitly default for dataclasses.
    N Fc                 3  sH   | ]@}|j rt|j|jkrt|j kr|jt|jfV  qd S N)reprgetattrnamedefault).0fdefaultsr4   r   r   	<genexpr>v   s
   zrepr_.<locals>.<genexpr>c                 3  s"   | ]\}}| kr||fV  qd S r6   r   )r;   r<   v)r>   r   r   r?   ~   s      z, c                 s  s    | ]\}}| d |V  qdS )=Nr   )r;   fieldvaluer   r   r   r?      s     ())
	__class____name__setdictdataclassesZis_dataclassfields__dict__itemsjoin)r4   	classnameZfields_valsZfield_reprsr   r=   r   repr_m   s    
rP   _ValuezIterable[_Value]int)iterablexr.   c                 C  sd   t | D ]D\}}||kr |  S t|trt|trt|| tk r|  S qtdt|dS )a  Return zero-based index of the first item whose value is equal to x.
    Raises a ValueError if there is no such item.

    We need a custom implementation instead of the built-in list .index() to
    be compatible with NumPy array and Pandas Series.

    Parameters
    ----------
    iterable : list, tuple, numpy.ndarray, pandas.Series
    x : Any

    Returns
    -------
    int
    z{} is not in iterableN)	enumerater/   floatabsr   
ValueErrorformatr3   )rS   rT   irC   r   r   r   index_   s    
r[   _Key)boundzMapping[_Key, _Value]zDict[str, _Value])rI   r.   c                 C  s   dd |   D S )Nc                 S  s   i | ]\}}|   |qS r   )lowerstrip)r;   kr@   r   r   r   
<dictcomp>   s     
 z)lower_clean_dict_keys.<locals>.<dictcomp>rM   )rI   r   r   r   lower_clean_dict_keys   s    rc   c                   @  s   e Zd ZdS )r   N)rG   
__module____qualname__r   r   r   r   r      s   r   zUnion[bytes, str])sr.   c                 C  s<   t d}ttt| tkr$| dn| }|| | S )z(Return the md5 hash of the given string.md5zutf-8)	hashlibnewr   bytestyper3   encodeupdate	hexdigest)rf   hbr   r   r   calc_md5   s    
 
rq   zDict[str, List[str]]z	List[str])query_paramskeys_to_excluder.   c                   s    fdd|   D S )zoReturns new object query_params : Dict[str, List[str]], but without keys defined with keys_to_drop : List[str].c                   s"   i | ]\}}|   kr||qS r   r^   )r;   keyrC   rs   r   r   ra      s    z,exclude_key_query_params.<locals>.<dictcomp>rb   )rr   rs   r   rv   r   exclude_key_query_params   s    
rw   zSet[str])rr   	param_keyr.   c                   s&   t dd  fdd D D S )zWExtracts key (case-insensitive) query params from Dict, and returns them as Set of str.c                 S  s   g | ]}|D ]}|  qqS r   rt   )r;   Zsublistr-   r   r   r   
<listcomp>   s    z,extract_key_query_params.<locals>.<listcomp>c                   s4   g | ],}|   kr|rd d | D qS )c                 S  s   g | ]}|  qS r   rt   )r;   rC   r   r   r   ry      s     z7extract_key_query_params.<locals>.<listcomp>.<listcomp>)r^   get)r;   ru   rx   rr   r   r   ry      s    
)rH   keys)rr   rx   r   r{   r   extract_key_query_params   s    r}   ))__doc__
__future__r   rJ   r   rh   r'   r)   typingr   r   r   r   r   r   r	   r
   r   Ztyping_extensionsr   Z	streamlitr   r   __annotations__r   r   r"   r   r   r2   rP   rQ   r[   r3   r\   rc   	Exceptionr   rq   rw   r}   r   r   r   r   <module>   s2   ,*