o
    Yh                     @  s0  U d dl m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
mZmZ d dlmZmZ d dlmZ e
r?d dlmZ dZd	ed
< dZd	ed< 	d;d<ddZejd=d>ddZejd=d>ddZd?dd Zd@d"d#ZdAd%d&ZdAd'd(ZdBd*d+ZdCd-d.Z dDd1d2Z!dEd3d4Z"dFd6d7Z#dGd9d:Z$dS )H    )annotationsN)Path)IOTYPE_CHECKINGAnyFinal)env_utilerrors)is_binary_string)	Generatorz
.streamlitr   CONFIG_FOLDER_NAMEstaticAPP_STATIC_FOLDER_NAMEautodatabytesencodingstrreturnio.StringIO | io.BytesIOc                 C  s<   |dkrt | r
dnd}n|}|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)r   r   data_encoding r   T/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/file_util.pyget_encoded_file_data%   s   
r   FpathbinaryboolGenerator[IO[Any], None, None]c                 c  s|    t | }t|jdkrtd| dd}|r|d7 }ttjt	| |}|V  W d   dS 1 s7w   Y  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: ""rbN)
get_streamlit_file_pathosstatst_sizer	   Erroropenr   joinr   )r   r   filenamemodehandler   r   r   streamlit_readD   s   "r/   c              
   c  s    d}|r	|d7 }t | } tjtj| dd zt| |}|V  W d   W dS 1 s.w   Y  W dS  tya } z dtj|  g}|jtj	krUt
jrU|d td|d}~ww )	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_okNzUnable to write file: zSPython is limited to files below 2GB on OSX. See https://bugs.python.org/issue24658
)r%   r&   makedirsr   dirnamer*   OSErrorabspatherrnoEINVALr   	IS_DARWINappendr	   r)   r+   )r   r   r-   r.   emsgr   r   r   streamlit_write]   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   r4   normpath__file__r+   )r4   r   r   r   get_static_dir   s   r@   main_script_pathc                 C  s   t | jt }tj|S )z+Get the folder where app static files live.)r   parentr   r&   r   r6   )rA   
static_dirr   r   r   get_app_static_dir   s   rD   filepathc                  G  s,   t  }|du rtdt|t t |   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   homeRuntimeErrorr   r   )rE   rF   r   r   r   r%      s   r%   c                  G  s   t t t t|   S )zReturn the full path to a filepath in ${CWD}/.streamlit.

    This doesn't guarantee that the file (or its directory) exists.
    )r   r   cwdr   rE   r   r   r   get_project_streamlit_file_path   s   rJ   r,   c                 C  s$   t ttjtj| t | S )zReturn the full path to a file in the .streamlit folder relative to the
    main script's path.

    This doesn't guarantee that the file (or its directory) exists.
    )r   r   r&   r   r6   r4   r   )rA   r,   r   r   r   #get_main_script_streamlit_file_path   s   rK   folderpath_globc                 C  sJ   | ds| dr|d7 }n|d7 }ddl}tj| d }|||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/*r   N)endswithfnmatchr&   r   r4   )rE   rL   rP   file_dirr   r   r   file_is_in_folder_glob   s   


rR   	directoryintc                 C  sF   d}t | D ]\}}}|D ]}t j||}|t j|7 }qq|S )z(Return the size of a directory in bytes.r   )r&   walkr   r+   getsize)rS   
total_sizedirpath_	filenamesffpr   r   r   get_directory_size   s   r]   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   r6   .0r   r   r   r   
<listcomp>   s    z&file_in_pythonpath.<locals>.<listcomp>c                 3  s"    | ]}t tj |V  qd S )N)rR   r&   r   r>   r`   rI   r   r   	<genexpr>   s
    
z%file_in_pythonpath.<locals>.<genexpr>)r&   environgetlensplitpathsepany)rE   
pythonpathabsolute_pathsr   rI   r   file_in_pythonpath   s   rl   argsc                  G  s   t jt jj|  S )zReturn the normalized path of the joined path.

    Parameters
    ----------
    *args : str
        The path components to join.

    Returns
    -------
    str
        The normalized path of the joined path.
    )r&   r   r>   r+   )rm   r   r   r   normalize_path_join   s   rn   main_scriptc                 C  s   t t | }tj|S )a  Return the full path to the main script directory.

    Parameters
    ----------
    main_script : str
        The main script path. The path can be an absolute path or a relative
        path.

    Returns
    -------
    str
        The full path to the main script directory.
    )rn   r&   getcwdr   r4   )ro   rA   r   r   r   get_main_script_directory   s   rq   )r   )r   r   r   r   r   r   )F)r   r   r   r    r   r!   )r   r   )rA   r   r   r   )rE   r   r   r   )rA   r   r,   r   r   r   )rE   r   rL   r   r   r    )rS   r   r   rT   )rE   r   r   r    )rm   r   r   r   )ro   r   r   r   )%
__future__r   
contextlibr7   r   r&   pathlibr   typingr   r   r   r   	streamlitr   r	   streamlit.string_utilr
   collections.abcr   r   __annotations__r   r   contextmanagerr/   r=   r@   rD   r%   rJ   rK   rR   r]   rl   rn   rq   r   r   r   r   <module>   s:   
!








