
    gT                        d dl mZ d dlZd dlmZmZ d dlmZmZm	Z	 d dl
mZmZ d dlmZ  e	d      Z G d d	eee         Zy)
    )annotationsN)ABCabstractmethod)AnyGenericTypeVar)AttrDictsecrets_singleton)calc_md5RawConnectionTc                  n    e Zd ZdZddZddZddZddZedd       Z	ddZ
edd       Zedd	       Zy
)BaseConnectiona  The abstract base class that all Streamlit Connections must inherit from.

    This base class provides connection authors with a standardized way to hook into the
    ``st.connection()`` factory function: connection authors are required to provide an
    implementation for the abstract method ``_connect`` in their subclasses.

    Additionally, it also provides a few methods/properties designed to make
    implementation of connections more convenient. See the docstrings for each of the
    methods of this class for more information

    .. note::
        While providing an implementation of ``_connect`` is technically all that's
        required to define a valid connection, connections should also provide the user
        with context-specific ways of interacting with the underlying connection object.
        For example, the first-party SQLConnection provides a ``query()`` method for
        reads and a ``session`` property for more complex operations.
    c                   || _         || _        t        t        j                  | j
                  j                                     | _        t        j                  j                  | j                          | j                  di || _        y)a  Create a BaseConnection.

        This constructor is called by the connection factory machinery when a user
        script calls ``st.connection()``.

        Subclasses of BaseConnection that want to overwrite this method should take care
        to also call the base class' implementation.

        Parameters
        ----------
        connection_name : str
            The name of this connection. This corresponds to the
            ``[connections.<connection_name>]`` config section in ``st.secrets``.
        kwargs : dict
            Any other kwargs to pass to this connection class' ``_connect`` method.

        Returns
        -------
        None
        N )_connection_name_kwargsr   jsondumps_secretsto_dict_config_section_hashr
   file_change_listenerconnect_on_secrets_changed_connect_raw_instance)selfconnection_namekwargss      Z/var/www/openai/venv/lib/python3.12/site-packages/streamlit/connections/base_connection.py__init__zBaseConnection.__init__.   sf    * !0$,TZZ8M8M8O-P$Q!..66t7O7OP4ADMM4KF4K    c                V    t         j                  j                  | j                         y )N)r
   r   
disconnectr   r   s    r    __del__zBaseConnection.__del__K   s    ..99$:R:RSr"   c                    	 t         j                  | |      S # t        $ r/}t        | j                  |      rt        d| d| d      |d }~ww xY w)N`z3` doesn't exist here, but you can call `._instance.z	` instead)object__getattribute__AttributeErrorhasattr	_instance)r   namees      r    r*   zBaseConnection.__getattribute__N   sb    	**466 	t~~t,$vPQUPVV_`  G	s    	A*AAc                    t        t        j                  | j                  j	                                     }|| j
                  k7  r|| _        | j                          yy)zReset the raw connection object when this connection's secrets change.

        We don't expect either user scripts or connection authors to have to use or
        overwrite this method.
        N)r   r   r   r   r   r   reset)r   _new_hashs      r    r   z"BaseConnection._on_secrets_changedZ   sJ     DJJt}}'<'<'>?@ t000(0D%JJL 1r"   c                    d}t        j                         rt        j                  d      }t        |      t        urt	        i       S |j                  | j
                  t	        i             S )a=  Get the secrets for this connection from the corresponding st.secrets section.

        We expect this property to be used primarily by connection authors when they
        are implementing their class' ``_connect`` method. User scripts should, for the
        most part, have no reason to use this property.
        Nconnections)r
   load_if_toml_existsgettyper	   r   )r   connections_sections     r    r   zBaseConnection._secretsh   s\     #002"3"7"7"F#$H4B<"&&t'<'<hrlKKr"   c                    d| _         y)aK  Reset this connection so that it gets reinitialized the next time it's used.

        This method can be useful when a connection has become stale, an auth token has
        expired, or in similar scenarios where a broken connection might be fixed by
        reinitializing it. Note that some connection methods may already use ``reset()``
        in their error handling code.

        Returns
        -------
        None

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> conn = st.connection("my_conn")
        >>>
        >>> # Reset the connection before using it if it isn't healthy
        >>> # Note: is_healthy() isn't a real method and is just shown for example here.
        >>> if not conn.is_healthy():
        ...     conn.reset()
        >>>
        >>> # Do stuff with conn...
        N)r   r%   s    r    r1   zBaseConnection.resety   s    2 "r"   c                t    | j                   ! | j                  di | j                  | _         | j                   S )zKGet an instance of the underlying connection, creating a new one if needed.r   )r   r   r   r%   s    r    r-   zBaseConnection._instance   s6     %!.!>!>D!!!r"   c                    t         )a  Create an instance of an underlying connection object.

        This abstract method is the one method that we require subclasses of
        BaseConnection to provide an implementation for. It is called when first
        creating a connection and when reconnecting after a connection is reset.

        Parameters
        ----------
        kwargs : dict

        Returns
        -------
        RawConnectionT
            The underlying connection object.
        )NotImplementedError)r   r   s     r    r   zBaseConnection._connect   s
    " "!r"   N)r   strreturnNone)r?   r@   )r.   r>   r?   r   )r?   r	   )r?   r   )__name__
__module____qualname____doc__r!   r&   r*   r   propertyr   r1   r-   r   r   r   r"   r    r   r      s`    $L:T L L "6 " " " "r"   r   )
__future__r   r   abcr   r   typingr   r   r   streamlit.runtime.secretsr	   r
   streamlit.utilr   r   r   r   r"   r    <module>rK      s<    #  # ( ( A #)*S"S'.1 S"r"   