o
    h                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZmZ er6d dlmZ G d	d
 d
eZdS )    )annotationsN)defaultdict)TYPE_CHECKING)util)	CacheStatgroup_stats)UploadedFileManagerUploadedFileRecUploadFileUrlInfo)Sequencec                   @  s^   e Zd ZdZd ddZd!ddZd"ddZd#ddZd$ddZdd Z	d%ddZ
d&ddZdS )'MemoryUploadedFileManagerzHolds files uploaded by users of the running Streamlit app.
    This class can be used safely from multiple threads simultaneously.
    upload_endpointstrc                 C  s   t t| _|| _d S N)r   dictfile_storageendpoint)selfr    r   b/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/memory_uploaded_file_manager.py__init__&   s   

z"MemoryUploadedFileManager.__init__
session_idfile_idsSequence[str]returnlist[UploadedFileRec]c                 C  s:   | j | }g }|D ]}||d}|dur|| q	|S )a  Return a  list of UploadedFileRec for a given sequence of file_ids.

        Parameters
        ----------
        session_id
            The ID of the session that owns the files.
        file_ids
            The sequence of ids associated with files to retrieve.

        Returns
        -------
        List[UploadedFileRec]
            A list of URL UploadedFileRec instances, each instance contains information
            about uploaded file.
        N)r   getappend)r   r   r   session_storage	file_recsfile_idfile_recr   r   r   	get_files*   s   

z#MemoryUploadedFileManager.get_filesNonec                 C  s   | j |d dS )z1Remove all files associated with a given session.Nr   pop)r   r   r   r   r   remove_session_filesF   s   z.MemoryUploadedFileManager.remove_session_filesc                 C  s
   t | S r   )r   repr_)r   r   r   r   __repr__J   s   
z"MemoryUploadedFileManager.__repr__filer	   c                 C  s   || j | |j< dS )z
        Safe to call from any thread.

        Parameters
        ----------
        session_id
            The ID of the session that owns the file.
        file
            The file to add.
        N)r   r    )r   r   r)   r   r   r   add_fileM   s   z"MemoryUploadedFileManager.add_filec                 C  s   | j | }||d dS )z?Remove file with given file_id associated with a given session.Nr$   )r   r   r    r   r   r   r   remove_file_   s   
z%MemoryUploadedFileManager.remove_file
file_nameslist[UploadFileUrlInfo]c                 C  sV   g }|D ]$}t t }|t|| j d| d| | j d| d| d q|S )zFReturn a list of UploadFileUrlInfo for a given sequence of file_names./)r    
upload_url
delete_url)r   uuiduuid4r   r
   r   )r   r   r,   result_r    r   r   r   get_upload_urlsd   s   z)MemoryUploadedFileManager.get_upload_urlslist[CacheStat]c                 C  s@   g }| j  }| D ]	}||  qdd |D }t|S )zPReturn the manager's CacheStats.

        Safe to call from any thread.
        c                 S  s    g | ]}t d dt|jdqS )r    )category_name
cache_namebyte_length)r   lendata).0r)   r   r   r   
<listcomp>   s    z7MemoryUploadedFileManager.get_stats.<locals>.<listcomp>)r   copyvaluesextendr   )r   	all_filesfile_storage_copyr   statsr   r   r   	get_statst   s   
z#MemoryUploadedFileManager.get_statsN)r   r   )r   r   r   r   r   r   )r   r   r   r#   )r   r   )r   r   r)   r	   r   r#   )r   r   r,   r   r   r-   )r   r6   )__name__
__module____qualname____doc__r   r"   r&   r(   r*   r+   r5   rE   r   r   r   r   r   !   s    





r   )
__future__r   r1   collectionsr   typingr   	streamlitr   streamlit.runtime.statsr   r   'streamlit.runtime.uploaded_file_managerr   r	   r
   collections.abcr   r   r   r   r   r   <module>   s   