o
    Zh6=                     @  s  U d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZmZmZmZ 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 erQd d
lmZ eeedZded< edZdddddddZ ded< dZ!ded< e	dee dZ"ed		dCdDd(d)Z#dEd+d,Z$e
			-dFdGd2d3Z%e
			-dFdHd5d3Z%e
			-dFdId8d3Z%e
			-dFdJd9d3Z%e
		dCdKd<d3Z%e
		dCdLd=d3Z%e
		dCdMd>d3Z%e
			dNdOdAd3Z%			dNdBd3Z%dS )P    )annotationsN)TYPE_CHECKINGAnyFinalLiteralTypeVaroverload)BaseConnectionSnowflakeConnectionSnowparkConnectionSQLConnection)deprecate_obj_name)StreamlitAPIException)cache_resource)gather_metrics)secrets_singleton)	timedelta)	snowflakesnowparksqlz+Final[dict[str, type[BaseConnection[Any]]]]_FIRST_PARTY_CONNECTIONSzNo module named \'(.+)\'mysqlclientzpsycopg2-binary
sqlalchemyzsnowflake-connector-pythonzsnowflake-snowpark-python)MySQLdbpsycopg2r   r   zsnowflake.connectorzsnowflake.snowparkzFinal[dict[str, str]]_MODULES_TO_PYPI_PACKAGESzenv:r   _USE_ENV_PREFIXConnectionClass)bound
connectionnamestrconnection_classtype[ConnectionClass]max_entries
int | Nonettlfloat | timedelta | Nonekwargsr   returnc                 K  sn   dd	d
}t |tst| dt|dd}|j d| d| |_t|d|d|}|| |fi |S )ap  Create an instance of connection_class with the given name and kwargs.

    The weird implementation of this function with the @cache_resource annotated
    function defined internally is done to:
      - Always @gather_metrics on the call even if the return value is a cached one.
      - Allow the user to specify ttl and max_entries when calling st.connection.
    r    r!   r"   r#   r(   r   r)   r   c                 [  s   |dd| i|S )Nconnection_name r+   )r    r"   r(   r+   r+   e/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/runtime/connection_factory.py__create_connectionO   s   z/_create_connection.<locals>.__create_connectionz% is not a subclass of BaseConnection!._zRunning `st.connection(...)`.)r$   show_spinnerr&   N)r    r!   r"   r#   r(   r   r)   r   )
issubclassr	   r   r!   replace__qualname__r   )r    r"   r$   r&   r(   r-   ttl_strr+   r+   r,   _create_connection?   s$   

r5   type[BaseConnection[Any]]c                 C  s$   | t v rt |  S td|  dt  )NzInvalid connection 'z!'. Supported connection classes: )r   r   )r"   r+   r+   r,   _get_first_party_connectionk   s   r7   FLiteral['sql']
autocommitboolr   c                 K     d S Nr+   r    r$   r&   r9   r(   r+   r+   r,   connection_factoryu      r>   typec                 K  r;   r<   r+   r    r@   r$   r&   r9   r(   r+   r+   r,   r>         	Literal['snowflake']r
   c                 K  r;   r<   r+   r=   r+   r+   r,   r>      r?   c                 K  r;   r<   r+   rA   r+   r+   r,   r>      rB   Literal['snowpark']r   c                 K  r;   r<   r+   )r    r$   r&   r(   r+   r+   r,   r>      s   c                 K  r;   r<   r+   r    r@   r$   r&   r(   r+   r+   r,   r>      r?   c                 K  r;   r<   r+   rE   r+   r+   r,   r>      r?   
str | NoneBaseConnection[Any]c                 K  r;   r<   r+   rE   r+   r+   r,   r>      r?   c              
   K  sH  |  tr| ttd }tj| } |}|du r-| tv r!t| }nt  td |  d }t	|t
rUd|v rQ|d}| }ddl}	|	d|}
t|
|}nt|}zt| |f||d|}t	|trnt|ddd	}|W S  ty } z't
|}tt|}d
}|rt|d}|rd| d}t| d| d}~ww )a3  Create a new connection to a data store or API, or return an existing one.

    Configuration options, credentials, and secrets for connections are
    combined from the following sources:

    - The keyword arguments passed to this command.
    - The app's ``secrets.toml`` files.
    - Any connection-specific configuration files.

    The connection returned from ``st.connection`` is internally cached with
    ``st.cache_resource`` and is therefore shared between sessions.

    Parameters
    ----------
    name : str
        The connection name used for secrets lookup in ``secrets.toml``.
        Streamlit uses secrets under ``[connections.<name>]`` for the
        connection. ``type`` will be inferred if ``name`` is one of the
        following: ``"snowflake"``, ``"snowpark"``, or ``"sql"``.

    type : str, connection class, or None
        The type of connection to create. This can be one of the following:

        - ``None`` (default): Streamlit will infer the connection type from
          ``name``. If the type is not inferable from ``name``, the type must
          be specified in ``secrets.toml`` instead.
        - ``"snowflake"``: Streamlit will initialize a connection with
          |SnowflakeConnection|_.
        - ``"snowpark"``: Streamlit will initialize a connection with
          |SnowparkConnection|_. This is deprecated.
        - ``"sql"``: Streamlit will initialize a connection with
          |SQLConnection|_.
        - A string path to an importable class: This must be a dot-separated
          module path ending in the importable class. Streamlit will import the
          class and initialize a connection with it. The class must extend
          ``st.connections.BaseConnection``.
        - An imported class reference: Streamlit will initialize a connection
          with the referenced class, which must extend
          ``st.connections.BaseConnection``.

        .. |SnowflakeConnection| replace:: ``SnowflakeConnection``
        .. _SnowflakeConnection: https://docs.streamlit.io/develop/api-reference/connections/st.connections.snowflakeconnection
        .. |SnowparkConnection| replace:: ``SnowparkConnection``
        .. _SnowparkConnection: https://docs.streamlit.io/develop/api-reference/connections/st.connections.snowparkconnection
        .. |SQLConnection| replace:: ``SQLConnection``
        .. _SQLConnection: https://docs.streamlit.io/develop/api-reference/connections/st.connections.sqlconnection

    max_entries : int or None
        The maximum number of connections to keep in the cache.
        If this is ``None`` (default), the cache is unbounded. Otherwise, when
        a new entry is added to a full cache, the oldest cached entry is
        removed.
    ttl : float, timedelta, or None
        The maximum number of seconds to keep results in the cache.
        If this is ``None`` (default), cached results do not expire with time.
    **kwargs : any
        Connection-specific keyword arguments that are passed to the
        connection's ``._connect()`` method. ``**kwargs`` are typically
        combined with (and take precedence over) key-value pairs in
        ``secrets.toml``. To learn more, see the specific connection's
        documentation.

    Returns
    -------
    Subclass of BaseConnection
        An initialized connection object of the specified ``type``.

    Examples
    --------
    **Example 1: Inferred connection type**

    The easiest way to create a first-party (SQL, Snowflake, or Snowpark) connection is
    to use their default names and define corresponding sections in your ``secrets.toml``
    file. The following example creates a ``"sql"``-type connection.

    ``.streamlit/secrets.toml``:

    >>> [connections.sql]
    >>> dialect = "xxx"
    >>> host = "xxx"
    >>> username = "xxx"
    >>> password = "xxx"

    Your app code:

    >>> import streamlit as st
    >>> conn = st.connection("sql")

    **Example 2: Named connections**

    Creating a connection with a custom name requires you to explicitly
    specify the type. If ``type`` is not passed as a keyword argument, it must
    be set in the appropriate section of ``secrets.toml``. The following
    example creates two ``"sql"``-type connections, each with their own
    custom name. The first defines ``type`` in the ``st.connection`` command;
    the second defines ``type`` in ``secrets.toml``.

    ``.streamlit/secrets.toml``:

    >>> [connections.first_connection]
    >>> dialect = "xxx"
    >>> host = "xxx"
    >>> username = "xxx"
    >>> password = "xxx"
    >>>
    >>> [connections.second_connection]
    >>> type = "sql"
    >>> dialect = "yyy"
    >>> host = "yyy"
    >>> username = "yyy"
    >>> password = "yyy"

    Your app code:

    >>> import streamlit as st
    >>> conn1 = st.connection("first_connection", type="sql")
    >>> conn2 = st.connection("second_connection")

    **Example 3: Using a path to the connection class**

    Passing the full module path to the connection class can be useful,
    especially when working with a custom connection. Although this is not the
    typical way to create first party connections, the following example
    creates the same type of connection as one with ``type="sql"``. Note that
    ``type`` is a string path.

    ``.streamlit/secrets.toml``:

    >>> [connections.my_sql_connection]
    >>> url = "xxx+xxx://xxx:xxx@xxx:xxx/xxx"

    Your app code:

    >>> import streamlit as st
    >>> conn = st.connection(
    ...     "my_sql_connection", type="streamlit.connections.SQLConnection"
    ... )

    **Example 4: Importing the connection class**

    You can pass the connection class directly to the ``st.connection``
    command. Doing so allows static type checking tools such as ``mypy`` to
    infer the exact return type of ``st.connection``. The following example
    creates the same connection as in Example 3.

    ``.streamlit/secrets.toml``:

    >>> [connections.my_sql_connection]
    >>> url = "xxx+xxx://xxx:xxx@xxx:xxx/xxx"

    Your app code:

    >>> import streamlit as st
    >>> from streamlit.connections import SQLConnection
    >>> conn = st.connection("my_sql_connection", type=SQLConnection)

    Nconnectionsr@   r.   r   )r$   r&   zconnection("snowpark")zconnection("snowflake")z
2024-04-01z@You may be missing a dependency required to use this connection.   zYou need to install the 'z!' package to use this connection.z. )
startswithr   lenosenvironr   r7   r   load_if_toml_exists
isinstancer!   splitpop	importlibimport_modulejoingetattrr5   r   r   ModuleNotFoundErrorresearch_MODULE_EXTRACTION_REGEXr   getgroup)r    r@   r$   r&   r(   envvar_namer"   parts	classnamerR   connection_moduleconne
err_stringmissing_module
extra_infopypi_packager+   r+   r,   r>      sZ    
&




)NN)r    r!   r"   r#   r$   r%   r&   r'   r(   r   r)   r   )r"   r!   r)   r6   )NNF)r    r8   r$   r%   r&   r'   r9   r:   r(   r   r)   r   )r    r!   r@   r8   r$   r%   r&   r'   r9   r:   r(   r   r)   r   )r    rC   r$   r%   r&   r'   r9   r:   r(   r   r)   r
   )r    r!   r@   rC   r$   r%   r&   r'   r9   r:   r(   r   r)   r
   )
r    rD   r$   r%   r&   r'   r(   r   r)   r   )r    r!   r@   rD   r$   r%   r&   r'   r(   r   r)   r   )r    r!   r@   r#   r$   r%   r&   r'   r(   r   r)   r   )NNN)r    r!   r@   rF   r$   r%   r&   r'   r(   r   r)   rG   )&
__future__r   rL   rW   typingr   r   r   r   r   r   streamlit.connectionsr	   r
   r   r   streamlit.deprecation_utilr   streamlit.errorsr   streamlit.runtime.cachingr   streamlit.runtime.metrics_utilr   streamlit.runtime.secretsr   datetimer   r   __annotations__compilerY   r   r   r   r5   r7   r>   r+   r+   r+   r,   <module>   s    

+


	

