L ih!UdZddlmZddlZddlZddlmZddlmZddl m Z m Z ee Z ded<dd ZGd d ZGd d Zy)z@Provides global MediaFileManager object as `media_file_manager`.) annotationsN)Final) get_logger) MediaFileKindMediaFileStorager_LOGGERc:ddlm}|}|y|jS)z'Get the active AppSession's session_id.r)get_script_run_ctxdontcare)7streamlit.runtime.scriptrunner_utils.script_run_contextr session_id)r ctxs j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/streamlit/runtime/media_file_manager.py_get_session_idrs%  C { >>c\eZdZdZej fddZeddZed dZ d dZ y) MediaFileMetadatazBMetadata that the MediaFileManager needs for each file it manages.c ||_d|_yNF)_kind_is_marked_for_delete)selfkinds r__init__zMediaFileMetadata.__init__/s %*"rc|jSN)rrs rrzMediaFileMetadata.kind3s zzrc|jSrrrs ris_marked_for_deletez&MediaFileMetadata.is_marked_for_delete7s)))rcd|_y)NTrrs rmark_for_deletez!MediaFileMetadata.mark_for_delete;s %)"rN)rrreturnNone)r#r)r#boolr#r$) __name__ __module__ __qualname____doc__rMEDIArpropertyrr r"rrrr,s@L-:-@-@+***rrc^eZdZdZd dZd dZd dZd dZd ddZ d ddZ y)MediaFileManageraIn-memory file manager for MediaFile objects. This keeps track of: - Which files exist, and what their IDs are. This is important so we can serve files by ID -- that's the whole point of this class! - Which files are being used by which AppSession (by ID). This is important so we can remove files from memory when no more sessions need them. - The exact location in the app where each file is being used (i.e. the file's "coordinates"). This is is important so we can mark a file as "not being used by a certain session" if it gets replaced by another file at the same coordinates. For example, when doing an animation where the same image is constantly replace with new frames. (This doesn't solve the case where the file's coordinates keep changing for some reason, though! e.g. if new elements keep being prepended to the app. Unlikely to happen, but we should address it at some point.) c||_i|_tjt|_t j|_yr) _storage_file_metadata collections defaultdictdict_files_by_session_and_coord threadingLock_lock)rstorages rrzMediaFileManager.__init__Rs; =?  # #D ) (^^% rct|jj}|jj D]!}|j |j #|S)zCompute the set of files that are stored in the manager, but are not referenced by any active session. These are files that can be safely deleted. Thread safety: callers must hold `self._lock`. )setr2keysr6valuesdifference_update)rfile_idssession_file_ids_by_coords r_get_inactive_file_idsz'MediaFileManager._get_inactive_file_idsbs_t**//12*.)I)I)P)P)R K %  & &'@'G'G'I J Krctjd|j5|jD]}|j|}|j t jk(r|j|A|j t jk(s_|jr|j|}|j dddy#1swYyxYw)zuRemove all files that are no longer referenced by any active session. Safe to call from any thread. zRemoving orphaned files...N) rdebugr9rBr2rrr+ _delete_file DOWNLOADABLEr r")rfile_idfiles rremove_orphaned_filesz&MediaFileManager.remove_orphaned_filesrs  23 ZZ /668 /**7399 3 33%%g.YY-"<"<<00))'2,,. / / / /sA/C 0C  Cctjd||jj||j|=y)zDelete the given file from storage, and remove its metadata from self._files_by_id. Thread safety: callers must hold `self._lock`. zDeleting File: %sN)rrDr1 delete_filer2)rrGs rrEzMediaFileManager._delete_files3  )73 !!'*    (rNc| t}tjd||j5||jvr |j|=dddtjd|jj tjdt |jt |jy#1swYuxYw)a#Remove the given session's file references. (This does not remove any files from the manager - you must call `remove_orphaned_files` for that.) Should be called whenever ScriptRunner completes and when a session ends. Safe to call from any thread. Nz*Disconnecting files for session with ID %szSessions still active: %rz"Files: %s; Sessions with files: %s)rrrDr9r6r=lenr2)rr s rclear_session_refsz#MediaFileManager.clear_session_refss  (*J BJO ZZ AT===44Z@ A  ')I)I)N)N)P   0 ## $ 00 1  A As B??Ccjt}|j5|rtjntj}|j j ||||}t|} | |j|<||j||<|j j|cdddS#1swYyxYw)aAdd a new MediaFile with the given parameters and return its URL. If an identical file already exists, return the existing URL and registers the current session as a user. Safe to call from any thread. Parameters ---------- path_or_data : bytes or str If bytes: the media file's raw data. If str: the name of a file to load from disk. mimetype : str The mime type for the file. E.g. "audio/mpeg". This string will be used in the "Content-Type" header when the file is served over HTTP. coordinates : str Unique string identifying an element's location. Prevents memory leak of "forgotten" file IDs when element media is being replaced-in-place (e.g. an st.image stream). coordinates should be of the form: "1.(3.-14).5" file_name : str or None Optional file_name. Used to set the filename in the response header. is_for_static_download: bool Indicate that data stored for downloading as a file, not as a media for rendering at page. [default: False] Returns ------- str The url that the frontend can use to fetch the media. Raises ------ If a filename is passed, any Exception raised when trying to read the file will be re-raised. )rN) rr9rrFr+r1load_and_get_idrr2r6get_url) r path_or_datamimetype coordinates file_nameis_for_static_downloadr rrGmetadatas raddzMediaFileManager.adds\%& ZZ 2***"((  mm33hiG)d3H+3D   (HOD , ,Z 8 E==((1 2 2 2s BB))B2)r:rr#r$)r#zset[str]r&)rGstrr#r$r)r str | Noner#r$r) rRz bytes | strrSrYrTrYrUrZrVr%r#rY) r'r(r)r*rrBrIrErNrXr-rrr/r/?sh$&  /$) D!%', >2!>2>2 >2  >2 !% >2 >2rr/)r#rY)r* __future__rr3r7typingrstreamlit.loggerr$streamlit.runtime.media_file_storagerrr'r__annotations__rrr/r-rrr`sGG"'PH%% **&j2j2r