
    g                       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Zej                   dd	       Zej                   dd
       ZddZddZddZd ZddZddZddZd Zd Zy)    )annotationsN)Path)env_utilerrors)is_binary_stringz
.streamlitstaticc                    |dk(  rt        |       rdnd}n|}|r$t        j                  | j                  |            S t        j                  |       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.

    autoNzutf-8)r   ioStringIOdecodeBytesIO)dataencodingdata_encodings      H/var/www/openai/venv/lib/python3.12/site-packages/streamlit/file_util.pyget_encoded_file_datar   !   sK    " 6 !1 6G {{4;;}566::d    c              #  8  K   t        |       }t        j                  |      j                  dk(  rt	        j
                  d| d      d}|r|dz  }t        t        j                  j                  t        |       |      5 }| ddd       y# 1 sw Y   yxY ww)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handles        r   streamlit_readr&   @   s      't,H	wwx  A%ll3H:Q?@@D	bggll-t4d	;v 
<	;	;s   A>B B	BBBc              #    K   d}|r|dz  }t        |       } t        j                  t        j                  j	                  |       d       	 t        | |      5 }| ddd       y# 1 sw Y   yxY w# t        $ r}dt        j                  j                  |        g}|j                  t        j                  k(  r!t        j                  r|j                  d       t        j                  dj                  |            d}~ww xY 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msgs         r   streamlit_writer5   Y   s      D"4(DKK%5
+$L  +'(='>?@77ell"x'9'9JJ9 ll499S>**+sO   A	DA2 A&A2 %D&A/+A2 .D/A2 2	D;BD  DDc                     t         j                  j                  t         j                  j                  t                    } t         j                  j                  t         j                  j                  | d            S )z3Get the folder where static HTML/JS/CSS files live.r   )r   r   r,   normpath__file__r    )r,   s    r   get_static_dirr9   y   sG    ggoobgg..x89G77BGGLL(;<<r   c                |    t        |       } | j                  t        z  }t        j                  j                  |      S )z*Get the folder where app static files live)r   parentAPP_STATIC_FOLDER_NAMEr   r   r.   )main_script_path
static_dirs     r   get_app_static_dirr?      s2    ,-!((+AAJ77??:&&r   c                 z    t        j                         }|t        d      t        |t        z  t        |  z        S )zyReturn the full path to a file in ~/.streamlit.

    This doesn't guarantee that the file (or its directory) exists.
    zNo home directory.)r   homeRuntimeErrorstrr!   )filepathrA   s     r   r   r      s:    
 99;D|/00t((4?:;;r   c                 \    t        t        j                         t        z  t        |  z        S )zReturn the full path to a filepath in ${CWD}/.streamlit.

    This doesn't guarantee that the file (or its directory) exists.
    )rC   r   cwdr!   )rD   s    r   get_project_streamlit_file_pathrG      s$    
 txxz..x@AAr   c                    |j                  d      s|j                  d      r|dz  }n|dz  }ddl}t        j                  j	                  |       dz   }|j                  ||      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,   )rD   folderpath_globrL   file_dirs       r   file_is_in_folder_globrO      s_     ##C(##C(s"Ot#Owwx(3.H??8_55r   c                    d}t        j                  |       D ]O  \  }}}|D ]D  }t         j                  j                  ||      }|t         j                  j	                  |      z  }F Q |S )z(Return the size of a directory in bytes.r   )r   walkr   r    getsize)	directory
total_sizedirpath_	filenamesffps          r   get_directory_sizerZ      s^    J!#!3IAgq)B"''//"--J  "4 r   c                &    t         j                  j                  dd      }t        |      dk(  ry|j	                  t         j
                        D cg c]!  }t         j                  j                  |      # }}t         fd|D              S c c}w )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              3  p   K   | ]-  }t        t        j                  j                        |       / y w)N)rO   r   r   r7   ).0r   rD   s     r   	<genexpr>z%file_in_pythonpath.<locals>.<genexpr>   s/      "D 	rww//94@"s   36)	r   environgetlensplitpathsepr   r.   any)rD   
pythonpathr   absolute_pathss   `   r   file_in_pythonpathri      sz     b1J
:!8B8H8H8TU8Tbggood+8TNU "   Vs   &Bc                 t    t         j                  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   r7   r    )argss    r   normalize_path_joinrl      s&     77BGGLL$/00r   c                |    t        t        j                         |       }t        j                  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.
    )rl   r   getcwdr   r,   )main_scriptr=   s     r   get_main_script_directoryrp      s+     +299;D77??+,,r   )r
   )r   bytesr   rC   returnzio.StringIO | io.BytesIO)F)rr   rC   )r=   rC   rr   rC   )rD   rC   rM   rC   rr   bool)rS   rC   rr   int)rD   rC   rr   rs   )
__future__r   
contextlibr/   r   r   pathlibr   	streamlitr   r   streamlit.string_utilr   r!   r<   r   contextmanagerr&   r5   r9   r?   r   rG   rO   rZ   ri   rl   rp    r   r   <module>r|      s    #   	 	  & 2 "  "  "(
>  0 + +>='	<B6241 -r   