o
    ßñh3  ã                   @  sô   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
 er>d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ G d
d„ deƒZG dd„ de	ƒZeG dd„ dƒƒZeG dd„ dƒƒZG dd„ deƒZG dd„ de	ƒZG dd„ de	ƒZdS )é    )Úannotations)Úabstractmethod)Ú	dataclass)ÚTYPE_CHECKINGÚCallableÚProtocolÚcast)Ú
ForwardMsg)Ú
AppSession)Ú
ScriptData)ÚScriptCache)ÚUploadedFileManagerc                   @  ó   e Zd ZdZdS )ÚSessionClientDisconnectedErrorz5Raised by operations on a disconnected SessionClient.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úU/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/session_manager.pyr      ó    r   c                   @  s   e Zd ZdZed	dd„ƒZdS )
ÚSessionClientz1Interface for sending data to a session's client.Úmsgr	   ÚreturnÚNonec                 C  ó   t ‚)z›Deliver a ForwardMsg to the client.

        If the SessionClient has been disconnected, it should raise a
        SessionClientDisconnectedError.
        ©ÚNotImplementedError)Úselfr   r   r   r   Úwrite_forward_msg$   s   zSessionClient.write_forward_msgN)r   r	   r   r   )r   r   r   r   r   r    r   r   r   r   r   !   s    r   c                   @  s.   e Zd ZU dZded< ded< dZded< d	S )
ÚActiveSessionInfozÆType containing data related to an active session.

    This type is nearly identical to SessionInfo. The difference is that when using it,
    we are guaranteed that SessionClient is not None.
    r   Úclientr
   Úsessionr   ÚintÚscript_run_countN)r   r   r   r   Ú__annotations__r%   r   r   r   r   r!   .   s
   
 r!   c                   @  sB   e Zd ZU dZded< ded< dZded< ddd„Zddd„ZdS )ÚSessionInfozËType containing data related to an AppSession.

    For each AppSession, the Runtime tracks that session's
    script_run_count. This is used to track the age of messages in
    the ForwardMsgCache.
    zSessionClient | Noner"   r
   r#   r   r$   r%   r   Úboolc                 C  s
   | j d uS )N)r"   ©r   r   r   r   Ú	is_activeH   s   
zSessionInfo.is_activer!   c                 C  s   |   ¡ sJ dƒ‚td| ƒS )Nz.A SessionInfo with no client cannot be active!r!   )r*   r   r)   r   r   r   Ú	to_activeK   s   
zSessionInfo.to_activeN)r   r(   )r   r!   )r   r   r   r   r&   r%   r*   r+   r   r   r   r   r'   ;   s   
 
r'   c                   @  r   )ÚSessionStorageErrorzßException class for errors raised by SessionStorage.

    The original error that causes a SessionStorageError to be (re)raised will generally
    be an I/O error specific to the concrete SessionStorage implementation.
    Nr   r   r   r   r   r,   T   r   r,   c                   @  sD   e Zd Zeddd„ƒZedd
d„ƒZeddd„ƒZeddd„ƒZdS )ÚSessionStorageÚ
session_idÚstrr   úSessionInfo | Nonec                 C  r   )a.  Return the SessionInfo corresponding to session_id, or None if one does not
        exist.

        Parameters
        ----------
        session_id
            The unique ID of the session being fetched.

        Returns
        -------
        SessionInfo or None

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while attempting to fetch the session. This will
            generally happen if there is an error with the underlying storage backend
            (e.g. if we lose our connection to it).
        r   ©r   r.   r   r   r   Úget]   ó   zSessionStorage.getÚsession_infor'   r   c                 C  r   )a  Save the given session.

        Parameters
        ----------
        session_info
            The SessionInfo being saved.

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while saving the given session.
        r   )r   r4   r   r   r   Úsavet   ó   zSessionStorage.savec                 C  r   )a½  Mark the session corresponding to session_id for deletion and stop tracking
        it.

        Note that:
          - Calling delete on an ID corresponding to a nonexistent session is a no-op.
          - Calling delete on an ID should cause the given session to no longer be
            tracked by this SessionStorage, but exactly when and how the session's data
            is eventually cleaned up is a detail left up to the implementation.

        Parameters
        ----------
        session_id
            The unique ID of the session to delete.

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while attempting to delete the session.
        r   r1   r   r   r   Údelete„   r3   zSessionStorage.deleteúlist[SessionInfo]c                 C  r   )zùList all sessions tracked by this SessionStorage.

        Returns
        -------
        List[SessionInfo]

        Raises
        ------
        SessionStorageError
            Raised if an error occurs while attempting to list sessions.
        r   r)   r   r   r   Úlist›   s   zSessionStorage.listN©r.   r/   r   r0   )r4   r'   r   r   ©r.   r/   r   r   ©r   r8   )r   r   r   r   r2   r5   r7   r9   r   r   r   r   r-   \   s    r-   c                   @  s˜   e Zd ZdZed4dd„ƒZe		d5d6dd„ƒZed7dd„ƒZed8dd „ƒZed9d"d#„ƒZ	d:d%d&„Z
d7d'd(„Zd;d*d+„Zd<d-d.„Zd=d0d1„Zd:d2d3„ZdS )>ÚSessionManagera7  SessionManagers are responsible for encapsulating all session lifecycle behavior
    that the Streamlit Runtime may care about.

    A SessionManager must define the following required methods:
      - __init__
      - connect_session
      - close_session
      - get_session_info
      - list_sessions

    SessionManager implementations may also choose to define the notions of active and
    inactive sessions. The precise definitions of active/inactive are left to the
    concrete implementation. SessionManagers that wish to differentiate between active
    and inactive sessions should have the required methods listed above operate on *all*
    sessions. Additionally, they should define the following methods for working with
    active sessions:
      - disconnect_session
      - get_active_session_info
      - is_active_session
      - list_active_sessions

    When active session-related methods are left undefined, their default
    implementations are the naturally corresponding required methods.

    The Runtime, unless there's a good reason to do otherwise, should generally work
    with the active-session versions of a SessionManager's methods. There isn't currently
    a need for us to be able to operate on inactive sessions stored in SessionStorage
    outside of the SessionManager itself. However, it's highly likely that we'll
    eventually have to do so, which is why the abstractions allow for this now.

    Notes
    -----
    Threading: All SessionManager methods are *not* threadsafe -- they must be called
    from the runtime's eventloop thread.
    Úsession_storager-   Úuploaded_file_managerr   Úscript_cacher   Úmessage_enqueued_callbackúCallable[[], None] | Noner   r   c                 C  r   )a  Initialize a SessionManager with the given SessionStorage.

        Parameters
        ----------
        session_storage
            The SessionStorage instance backing this SessionManager.

        uploaded_file_manager
            Used to manage files uploaded by users via the Streamlit web client.

        script_cache
            ScriptCache instance. Caches user script bytecode.

        message_enqueued_callback
            A callback invoked after a message is enqueued to be sent to a web client.
        r   )r   r>   r?   r@   rA   r   r   r   Ú__init__Ð   s   zSessionManager.__init__Nr"   r   Úscript_datar   Ú	user_infoúdict[str, str | bool | None]Úexisting_session_idú
str | NoneÚsession_id_overrider/   c                 C  r   )a¢  Create a new session or connect to an existing one.

        Parameters
        ----------
        client
            A concrete SessionClient implementation for communicating with
            the session's client.
        script_data
            Contains parameters related to running a script.
        user_info
            A dict that contains information about the session's user. For now,
            it only (optionally) contains the user's email address.

            {
                "email": "example@example.com"
            }
        existing_session_id
            The ID of an existing session to reconnect to. If one is not provided, a new
            session is created. Note that whether a SessionManager supports reconnecting
            to an existing session is left up to the concrete SessionManager
            implementation. Those that do not support reconnection should simply ignore
            this argument.
        session_id_override
            The ID to assign to a new session being created with this method. Setting
            this can be useful when the service that a Streamlit Runtime is running in
            wants to tie the lifecycle of a Streamlit session to some other session-like
            object that it manages. Only one of existing_session_id and
            session_id_override should be set.

        Returns
        -------
        str
            The session's unique string ID.
        r   )r   r"   rD   rE   rG   rI   r   r   r   Úconnect_sessionê   s   +zSessionManager.connect_sessionr.   c                 C  r   )a*  Close and completely delete the session with the given id.

        This function may be called multiple times for the same session,
        which is not an error. (Subsequent calls just no-op.)

        Parameters
        ----------
        session_id
            The session's unique ID.
        r   r1   r   r   r   Úclose_session  s   zSessionManager.close_sessionr0   c                 C  r   )zøReturn the SessionInfo for the given id, or None if no such session
        exists.

        Parameters
        ----------
        session_id
            The session's unique ID.

        Returns
        -------
        SessionInfo or None
        r   r1   r   r   r   Úget_session_info%  r6   zSessionManager.get_session_infor8   c                 C  r   )z‹Return the SessionInfo for all sessions managed by this SessionManager.

        Returns
        -------
        List[SessionInfo]
        r   r)   r   r   r   Úlist_sessions5  s   zSessionManager.list_sessionsr$   c                 C  ó   t |  ¡ ƒS )zùReturn the number of sessions tracked by this SessionManager.

        Subclasses of SessionManager shouldn't provide their own implementation of this
        method without a *very* good reason.

        Returns
        -------
        int
        )ÚlenrM   r)   r   r   r   Únum_sessions?  ó   
zSessionManager.num_sessionsc                 C  s   |   |¡ dS )z°Disconnect the given session.

        This method should be idempotent.

        Parameters
        ----------
        session_id
            The session's unique ID.
        N)rK   r1   r   r   r   Údisconnect_sessionP  s   
z!SessionManager.disconnect_sessionúActiveSessionInfo | Nonec                 C  s&   |   |¡}|du s| ¡ sdS | ¡ S )a/  Return the ActiveSessionInfo for the given id, or None if either no such
        session exists or the session is not active.

        Parameters
        ----------
        session_id
            The active session's unique ID.

        Returns
        -------
        ActiveSessionInfo or None
        N)rL   r*   r+   )r   r.   r#   r   r   r   Úget_active_session_info\  s   
z&SessionManager.get_active_session_infor(   c                 C  s   |   |¡duS )z~Return True if the given session exists and is active, False otherwise.

        Returns
        -------
        bool
        N)rT   r1   r   r   r   Úis_active_sessionn  s   z SessionManager.is_active_sessionúlist[ActiveSessionInfo]c                 C  s   dd„ |   ¡ D ƒS )z™Return the session info for all active sessions tracked by this SessionManager.

        Returns
        -------
        List[ActiveSessionInfo]
        c                 S  s   g | ]}|  ¡ ‘qS r   )r+   )Ú.0Úsr   r   r   Ú
<listcomp>~  s    z7SessionManager.list_active_sessions.<locals>.<listcomp>)rM   r)   r   r   r   Úlist_active_sessionsw  s   z#SessionManager.list_active_sessionsc                 C  rN   )a   Return the number of active sessions tracked by this SessionManager.

        Subclasses of SessionManager shouldn't provide their own implementation of this
        method without a *very* good reason.

        Returns
        -------
        int
        )rO   rZ   r)   r   r   r   Únum_active_sessions€  rQ   z"SessionManager.num_active_sessions)
r>   r-   r?   r   r@   r   rA   rB   r   r   )NN)r"   r   rD   r   rE   rF   rG   rH   rI   rH   r   r/   r;   r:   r<   )r   r$   )r.   r/   r   rS   )r.   r/   r   r(   )r   rV   )r   r   r   r   r   rC   rJ   rK   rL   rM   rP   rR   rT   rU   rZ   r[   r   r   r   r   r=   «   s(    $ú,
	



		r=   N)Ú
__future__r   Úabcr   Údataclassesr   Útypingr   r   r   r   Ústreamlit.proto.ForwardMsg_pb2r	   Ústreamlit.runtime.app_sessionr
   Ústreamlit.runtime.script_datar   Ú+streamlit.runtime.scriptrunner.script_cacher   Ú'streamlit.runtime.uploaded_file_managerr   Ú	Exceptionr   r   r!   r'   r,   r-   r=   r   r   r   r   Ú<module>   s&   O