U
    ARd                     @   s   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 d dlmZm	Z	 d dl
mZ dZdZd"dd	Ze jd#ddZe jd$ddZdd ZeedddZdd ZedddZdd ZedddZeedddZedd d!ZdS )%    N)Path)env_utilutil)is_binary_stringz
.streamlitstaticautoc                 C   s8   |dkrt | rd}nd}|r.t| |S t| S )a<  Coerce bytes to a BytesIO or a StringIO.

    Parameters
    ----------
    data : bytes
    encoding : str

    Returns
    -------
    BytesIO or StringIO
        If the file's data is in a well-known textual format (or if the encoding
        parameter is set), return a StringIO. Otherwise, return BytesIO.

    r   Nzutf-8)r   ioStringIOdecodeBytesIO)dataencoding r   7/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/file_util.pyget_encoded_file_data    s    r   Fc              	   c   s`   t | }t|jdkr&td| d}|r6|d7 }ttjt	| |}|V  W 5 Q R X dS )a$  Opens a context to read this file relative to the streamlit path.

    For example:

    with streamlit_read('foo.txt') as foo:
        ...

    opens the file `.streamlit/foo.txt`

    path   - the path to write to (within the streamlit directory)
    binary - set to True for binary IO
    r   zRead zero byte file: "%s"rbN)
get_streamlit_file_pathosstatst_sizer   ErroropenpathjoinCONFIG_FOLDER_NAME)r   binaryfilenamemodehandler   r   r   streamlit_read>   s    r    c              
   c   s   d}|r|d7 }t | } tjtj| dd z t| |}|V  W 5 Q R X W n` tk
r } zBdtj|  g}|jtj	krt
jr|d td|W 5 d}~X Y nX dS )	a  Opens a file for writing within the streamlit path, and
    ensuring that the path exists. For example:

        with streamlit_write('foo/bar.txt') as bar:
            ...

    opens the file .streamlit/foo/bar.txt for writing,
    creating any necessary directories along the way.

    path   - the path to write to (within the streamlit directory)
    binary - set to True for binary IO
    wr   T)exist_okzUnable to write file: %szSPython is limited to files below 2GB on OSX. See https://bugs.python.org/issue24658
N)r   r   makedirsr   dirnamer   OSErrorabspatherrnoEINVALr   Z	IS_DARWINappendr   r   r   )r   r   r   r   emsgr   r   r   streamlit_writeW   s    r-   c                  C   s*   t jt jt} t jt j| dS )z3Get the folder where static HTML/JS/CSS files live.r   r   r   r%   normpath__file__r   r%   r   r   r   get_static_dirw   s    r2   )main_script_pathreturnc                 C   s   t | } | jt }tj|S )z*Get the folder where app static files live)r   parentAPP_STATIC_FOLDER_NAMEr   r   r'   )r3   Z
static_dirr   r   r   get_app_static_dir}   s    
r7   c                  C   s*   t jt jt} t jt j| dS )z(Get the folder where static assets live.zstatic/assetsr.   r1   r   r   r   get_assets_dir   s    r8   )r4   c                  G   s0   t jd}|dkrtdt jj|tf|  S )zyReturn the full path to a file in ~/.streamlit.

    This doesn't guarantee that the file (or its directory) exists.
    ~NzNo home directory.)r   r   
expanduserRuntimeErrorr   r   )filepathhomer   r   r   r      s    r   c                  G   s   t jjt  tf|  S )zReturn the full path to a filepath in ${CWD}/.streamlit.

    This doesn't guarantee that the file (or its directory) exists.
    )r   r   r   getcwdr   r<   r   r   r   get_project_streamlit_file_path   s    r@   c                 C   sB   | ds&| dr|d7 }n|d7 }tj| d }t||S )zTest whether a file is in some folder with globbing support.

    Parameters
    ----------
    filepath : str
        A file path.
    folderpath_glob: str
        A path to a folder that may include globbing.

    */z/*)endswithr   r   r%   fnmatch)r<   Zfolderpath_globZfile_dirr   r   r   file_is_in_folder_glob   s    


rE   )	directoryr4   c                 C   sF   d}t | D ]2\}}}|D ]"}t j||}|t j|7 }qq|S )z(Return the size of a directory in bytes.r   )r   walkr   r   getsize)rF   Z
total_sizedirpath_	filenamesffpr   r   r   get_directory_size   s    rN   c                    sJ   t jdd}t|dkrdS dd |t jD }t fdd|D S )	aE  Test whether a filepath is in the same folder of a path specified in the PYTHONPATH env variable.


    Parameters
    ----------
    filepath : str
        An absolute file path.

    Returns
    -------
    boolean
        True if contained in PYTHONPATH, False otherwise. False if PYTHONPATH is not defined or empty.

    
PYTHONPATH r   Fc                 S   s   g | ]}t j|qS r   )r   r   r'   .0r   r   r   r   
<listcomp>   s     z&file_in_pythonpath.<locals>.<listcomp>c                 3   s    | ]}t tj |V  qd S )N)rE   r   r   r/   rQ   r?   r   r   	<genexpr>   s   z%file_in_pythonpath.<locals>.<genexpr>)r   environgetlensplitpathsepany)r<   
pythonpathZabsolute_pathsr   r?   r   file_in_pythonpath   s    r\   )r   )F)F)
contextlibr(   rD   r   r   pathlibr   Z	streamlitr   r   Zstreamlit.string_utilr   r   r6   r   contextmanagerr    r-   r2   strr7   r8   r   r@   boolrE   intrN   r\   r   r   r   r   <module>   s,   

