
    %e                         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Ze j        dd
            Ze j        dd            Zd ZdedefdZdefdZd ZdefdZdedefdZdefdZdS )    N)Path)env_utilutil)is_binary_stringz
.streamlitstaticautoc                     |dk    rt          |           rd}nd}|r't          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.

    r   Nzutf-8)r   ioStringIOdecodeBytesIO)dataencodings     3lib/python3.11/site-packages/streamlit/file_util.pyget_encoded_file_datar       sa     6D!! 	HH
 H 2{4;;x00111:d    Fc              #   B  K   t          |           }t          j        |          j        dk    rt	          j        d|z            d}|r|dz  }t          t          j                            t          |           |          5 }|V  ddd           dS # 1 swxY w 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: "%s"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	wx A%%j4x?@@@D 	bgll-t44d	;	; v                 s   BBBc              #      K   d}|r|dz  }t          |           } t          j        t          j                            |           d           	 t          | |          5 }|V  ddd           dS # 1 swxY w Y   dS # t          $ r}dt          j                            |           z  g}|j        t          j	        k    r!t          j        r|                    d           t          j        d                    |                    d}~ww xY 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: %szSPython 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_writer2   W   sU      D "4((DK%%5555
)$ 	LLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ) ) ))BGOOD,A,AAB7el""x'9"JJ9   j3((()s=   A> A1$A> 1A55A> 8A59A> >
DB DDc                      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)   s    r   get_static_dirr6   w   sK    goobg..x8899G7BGLL(;;<<<r   main_script_pathreturnc                 |    t          |           } | j        t          z  }t          j                            |          S )z*Get the folder where app static files live)r   parentAPP_STATIC_FOLDER_NAMEr   r   r+   )r7   
static_dirs     r   get_app_static_dirr=   }   s3    ,--!(+AAJ7??:&&&r   c                      t           j                            d          }|t          d          t          j        j        |t
          g| R  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homes     r   r   r      sJ     7c""D|/0007<0<8<<<<r   c                  ^    t          j        j        t          j                    t          g| R  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   )rB   s    r   get_project_streamlit_file_pathrF      s'    
 7<	%7C(CCCCr   c                     |                     d          s |                     d          r|dz  }n|dz  }t          j                            |           dz   }t	          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/*)endswithr   r   r)   fnmatch)rB   folderpath_globfile_dirs      r   file_is_in_folder_globrN      sp     ##C(( $##C(( 	$s"OOt#Owx((3.H?8_555r   	directoryc                     d}t          j        |           D ]M\  }}}|D ]D}t           j                            ||          }|t           j                            |          z  }EN|S )z(Return the size of a directory in bytes.r   )r   walkr   r   getsize)rO   
total_sizedirpath_	filenamesffps          r   get_directory_sizerY      sr    J!#!3!3 . .I 	. 	.Agq))B"'//"---JJ	. r   c                      t           j                            dd          }t          |          dk    rdS d |                    t           j                  D             }t           f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                 L    g | ]!}t           j                            |          "S  )r   r   r+   ).0r   s     r   
<listcomp>z&file_in_pythonpath.<locals>.<listcomp>   s&    UUUbgood++UUUr   c              3   r   K   | ]1}t          t          j                                      |          V  2d S )N)rN   r   r   r4   )r_   r   rB   s     r   	<genexpr>z%file_in_pythonpath.<locals>.<genexpr>   sQ         	rw//994@@     r   )r   environgetlensplitpathsepany)rB   
pythonpathabsolute_pathss   `  r   file_in_pythonpathrk      s     b11J
:!uUU
8H8H8T8TUUUN    "     r   )r   )F)
contextlibr,   rK   r
   r   pathlibr   	streamlitr   r   streamlit.string_utilr   r   r;   r   contextmanagerr#   r2   r6   strr=   r   rF   boolrN   intrY   rk   r^   r   r   <module>rt      s         				 				       $ $ $ $ $ $ $ $ 2 2 2 2 2 2 "  "    <    0 ) ) ) )>= = =' ' ' ' ' '
=# 
= 
= 
= 
=D D D6 6 6 6 6.# #    D      r   