o
    沪g                     @  s   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
 d dlmZ dZdZ	d.d/ddZejd0ddZejd0ddZd1ddZd2ddZd1ddZdd Zd3d"d#Zd4d&d'Zd5d(d)Zd*d+ Zd,d- ZdS )6    )annotationsN)Path)env_utilerrors)is_binary_stringz
.streamlitstatic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   O/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/file_util.pyget_encoded_file_data!   s   
r   Fc                 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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 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_writeY   s&   &r5   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_diry   s   r8   main_script_pathc                 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.   )r9   
static_dirr   r   r   get_app_static_dir   s   
r=   c                  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"   )filepathr>   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"   r@   r   r   r   get_project_streamlit_file_path   s   rC   r@   folderpath_globboolc                 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    r,   )r@   rD   rI   file_dirr   r   r   file_is_in_folder_glob   s   


rK   	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)rL   
total_sizedirpath_	filenamesffpr   r   r   get_directory_size   s   rV   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)rK   r   r    r6   rY   rB   r   r   	<genexpr>   s
    
z%file_in_pythonpath.<locals>.<genexpr>)r   environgetlensplitpathsepany)r@   
pythonpathabsolute_pathsr   rB   r   file_in_pythonpath   s   re   c                  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    r6   r!   )argsr   r   r   normalize_path_join   s   rg   c                 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.
    )rg   r   getcwdr    r,   )main_scriptr9   r   r   r   get_main_script_directory   s   rj   )r   )r	   r
   r   r   r   r   )F)r   r   )r9   r   r   r   )r@   r   rD   r   r   rE   )rL   r   r   rM   )r@   r   r   rE   )
__future__r   
contextlibr/   r   r   pathlibr   	streamlitr   r   streamlit.string_utilr   r"   r;   r   contextmanagerr'   r5   r8   r=   r   rC   rK   rV   re   rg   rj   r   r   r   r   <module>   s2   






