
    &e0                    f   U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZ d dl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eedZ ej         d          Z!dddddddZ"de#d<    edee                   Z$ ed          	 	 d9d:d"            Z%d;d#Z&e	 	 	 d<d=d)            Z'e	 	 	 d<d>d+            Z'e	 	 	 d<d?d.            Z'e	 	 	 d<d@d/            Z'e	 	 d9dAd2            Z'e	 	 d9dBd3            Z'e	 	 d9dCd4            Z'e	 	 	 dDdEd7            Z'	 	 	 dDd8Z'dS )F    )annotationsN)	timedelta)AnyDictTypeTypeVaroverload)FinalLiteral)BaseConnectionSnowflakeConnectionSnowparkConnectionSQLConnection)deprecate_obj_name)StreamlitAPIException)cache_resource)gather_metrics)secrets_singleton)	snowflakesnowparksqlz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_PACKAGESConnectionClass)bound
connectionnamestrconnection_classType[ConnectionClass]max_entries
int | Nonettlfloat | timedelta | Nonereturnc                   dd}t          |t                    st          | d          t          |                              d	d
          }|j         d
| d
| |_         t          |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   c                     |dd| i|S )Nconnection_name r,   )r    r"   kwargss      Dlib/python3.11/site-packages/streamlit/runtime/connection_factory.py__create_connectionz/_create_connection.<locals>.__create_connectionO   s       ??????    z% is not a subclass of BaseConnection!._zRunning `st.connection(...)`.)r$   show_spinnerr&   )r    r!   r"   r#   r(   r   )
issubclassr   r   r!   replace__qualname__r   )r    r"   r$   r&   r-   r/   ttl_strs          r.   _create_connectionr8   ?   s     @ @ @ @
 &77 
#FFF
 
 	
 #hhS G +EEgEEEE $.4   	  t%5@@@@@r0   c                b    | t           v rt           |          S t          d|  dt                      )NzInvalid connection 'z!'. Supported connection classes: )FIRST_PARTY_CONNECTIONSr   )r"   s    r.   _get_first_party_connectionr;   k   sP    222&'788
	C/ 	C 	C)@	C 	C  r0   FLiteral['sql']
autocommitboolr   c                    d S Nr,   r    r$   r&   r=   r-   s        r.   connection_factoryrB   u   	     	Dr0   typec                    d S r@   r,   r    rD   r$   r&   r=   r-   s         r.   rB   rB      	     	Dr0   Literal['snowflake']r   c                    d S r@   r,   rA   s        r.   rB   rB      rC   r0   c                    d S r@   r,   rF   s         r.   rB   rB      rG   r0   Literal['snowpark']r   c                    d S r@   r,   )r    r$   r&   r-   s       r.   rB   rB      s	     	Dr0   c                    d S r@   r,   r    rD   r$   r&   r-   s        r.   rB   rB      rC   r0   c                    d S r@   r,   rN   s        r.   rB   rB      rC   r0   
str | NoneBaseConnection[Any]c                    d S r@   r,   rN   s        r.   rB   rB      rC   r0   c                   d}|                      |          r)| t          |          d         }t          j        |         } |E| t          v rt          |           }n,t          j                     t          d         |          d         }|}t          |t                    rtd|v ra|
                    d          }|                                }	t          j        d                    |                    }
t          |
|	          }nt          |          }	 t!          | |f||d|}t          |t"                    rt%          |ddd	          }|S # t&          $ r}t          |          }t)          j        t,          |          }d
}|r5t.                              |                    d                    }|rd| d}t'          t          |           d|           d}~ww xY w)aZ  Create a new connection to a data store or API, or return an existing one.

    Config options, credentials, secrets, etc. for connections are taken from various
    sources:

    - Any connection-specific configuration files.
    - An app's ``secrets.toml`` files.
    - The kwargs passed to this function.

    Parameters
    ----------
    name : str
        The connection name used for secrets lookup in ``[connections.<name>]``.
        Type will be inferred from passing ``"sql"``, ``"snowflake"``, or ``"snowpark"``.
    type : str, connection class, or None
        The type of connection to create. It can be a keyword (``"sql"``, ``"snowflake"``,
        or ``"snowpark"``), a path to an importable class, or an imported class reference.
        All classes must extend ``st.connections.BaseConnection`` and implement the
        ``_connect()`` method. If the type kwarg is None, a ``type`` field must be set in
        the connection's section in ``secrets.toml``.
    max_entries : int or None
        The maximum number of connections to keep in the cache, or None
        for an unbounded cache. (When a new entry is added to a full cache,
        the oldest cached entry will be removed.) The default is None.
    ttl : float, timedelta, or None
        The maximum number of seconds to keep results in the cache, or
        None if cached results should not expire. The default is None.
    **kwargs : any
        Additional connection specific kwargs that are passed to the Connection's
        ``_connect()`` method. Learn more from the specific Connection's documentation.

    Returns
    -------
    Connection object
        An initialized Connection object of the specified type.

    Examples
    --------
    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.

    >>> import streamlit as st
    >>> conn = st.connection("sql") # Config section defined in [connections.sql] in secrets.toml.

    Creating a SQLConnection with a custom name requires you to explicitly specify the
    type. If type is not passed as a kwarg, it must be set in the appropriate section of
    ``secrets.toml``.

    >>> import streamlit as st
    >>> conn1 = st.connection("my_sql_connection", type="sql") # Config section defined in [connections.my_sql_connection].
    >>> conn2 = st.connection("my_other_sql_connection") # type must be set in [connections.my_other_sql_connection].

    Passing the full module path to the connection class that you want to use can be
    useful, especially when working with a custom connection:

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

    Finally, you can pass the connection class to use directly to this function. Doing
    so allows static type checking tools such as ``mypy`` to infer the exact return
    type of ``st.connection``.

    >>> import streamlit as st
    >>> from streamlit.connections import SQLConnection
    >>> conn = st.connection("my_sql_connection", type=SQLConnection)
    zenv:NconnectionsrD   r1   )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. )
startswithlenosenvironr:   r;   r   load_if_toml_exists
isinstancer!   splitpop	importlibimport_modulejoingetattrr8   r   r   ModuleNotFoundErrorresearchMODULE_EXTRACTION_REGEXr   getgroup)r    rD   r$   r&   r-   USE_ENV_PREFIXenvvar_namer"   parts	classnameconnection_moduleconne
err_stringmissing_module
extra_infopypi_packages                    r.   rB   rB      s!   T N~&& ' 3~..001z+&|*** /t44DD 1333$]3D9&AD
 "C(( M
 """$**3//E		I ) 7 H H&'8)DD:;KLL=!"
0;
 
HN
 
 d.// 	%()	 D  
= 
= 
=VV
#:JGGW
 	i3778L8LQ8O8OPPL ihhhh
!SVV";";z";";<<<
=s   9E 
G BGG )NN)
r    r!   r"   r#   r$   r%   r&   r'   r(   r   )r"   r!   )NNF)
r    r<   r$   r%   r&   r'   r=   r>   r(   r   )r    r!   rD   r<   r$   r%   r&   r'   r=   r>   r(   r   )
r    rH   r$   r%   r&   r'   r=   r>   r(   r   )r    r!   rD   rH   r$   r%   r&   r'   r=   r>   r(   r   )r    rK   r$   r%   r&   r'   r(   r   )
r    r!   rD   rK   r$   r%   r&   r'   r(   r   )
r    r!   rD   r#   r$   r%   r&   r'   r(   r   )NNN)
r    r!   rD   rP   r$   r%   r&   r'   r(   rQ   )(
__future__r   r^   rX   rc   datetimer   typingr   r   r   r   r	   typing_extensionsr
   r   streamlit.connectionsr   r   r   r   streamlit.deprecation_utilr   streamlit.errorsr   streamlit.runtime.cachingr   streamlit.runtime.metrics_utilr   streamlit.runtime.secretsr   r:   compilere   r   __annotations__r   r8   r;   rB   r,   r0   r.   <module>r      s   # " " " " " "     				 				       5 5 5 5 5 5 5 5 5 5 5 5 5 5 , , , , , , , ,            : 9 9 9 9 9 2 2 2 2 2 2 4 4 4 4 4 4 9 9 9 9 9 9 7 7 7 7 7 7 %"  
 %"*%@AA !-753 3      '+>#3FGGG  #$(	(A (A (A (A (AV    
 #$(		 	 	 	 
	 
 #$(	 	 	 	 
	 
 #$(		 	 	 	 
	 
 #$(	 	 	 	 
	 
 #$(	 	 	 	 
	 
 #$(		 	 	 	 
	 
 #$(		 	 	 	 
	 
 "$(		 	 	 	 
	 
	I= I= I= I= I= I=r0   