o
    y¬´\ù  ã                   @   sÜ   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 ddlm	Z	 ddlm
Z
 dgZG d	d„ deƒZeZ	 d
d„ ZejD ]
Zeeeeeƒƒ q?dd„ ZdD ]
Zeeeeeƒƒ qPdd„ ZdD ]
Zeeeeeƒƒ qadS )é   )Úclass_mapper)Úexc)ÚSessioné   )ÚScopedRegistry)ÚThreadLocalRegistry)ÚwarnÚscoped_sessionc                   @   sB   e Zd ZdZdZ	 ddd„Zdd„ Zdd„ Zd	d
„ Zddd„Z	dS )r	   ztProvides scoped management of :class:`.Session` objects.

    See :ref:`unitofwork_contextual` for a tutorial.

    Nc                 C   s(   || _ |rt||ƒ| _dS t|ƒ| _dS )aÑ  Construct a new :class:`.scoped_session`.

        :param session_factory: a factory to create new :class:`.Session`
         instances. This is usually, but not necessarily, an instance
         of :class:`.sessionmaker`.
        :param scopefunc: optional function which defines
         the current scope.   If not passed, the :class:`.scoped_session`
         object assumes "thread-local" scope, and will use
         a Python ``threading.local()`` in order to maintain the current
         :class:`.Session`.  If passed, the function should return
         a hashable token; this token will be used as the key in a
         dictionary in order to store and retrieve the current
         :class:`.Session`.

        N)Úsession_factoryr   Úregistryr   )Úselfr
   Ú	scopefunc© r   úH/var/www/html/env/lib/python3.10/site-packages/sqlalchemy/orm/scoping.pyÚ__init__!   s   zscoped_session.__init__c                 K   s@   |r| j  ¡ rt d¡‚| jdi |¤Ž}| j  |¡ |S |   ¡ S )aÎ  Return the current :class:`.Session`, creating it
        using the :attr:`.scoped_session.session_factory` if not present.

        :param \**kw: Keyword arguments will be passed to the
         :attr:`.scoped_session.session_factory` callable, if an existing
         :class:`.Session` is not present.  If the :class:`.Session` is present
         and keyword arguments have been passed,
         :exc:`~sqlalchemy.exc.InvalidRequestError` is raised.

        zEScoped session is already present; no new arguments may be specified.Nr   )r   ÚhasÚsa_excÚInvalidRequestErrorr
   Úset)r   ÚkwÚsessr   r   r   Ú__call__8   s   
ÿzscoped_session.__call__c                 C   s$   | j  ¡ r|   ¡  ¡  | j  ¡  dS )aæ  Dispose of the current :class:`.Session`, if present.

        This will first call :meth:`.Session.close` method
        on the current :class:`.Session`, which releases any existing
        transactional/connection resources still being held; transactions
        specifically are rolled back.  The :class:`.Session` is then
        discarded.   Upon next usage within the same scope,
        the :class:`.scoped_session` will produce a new
        :class:`.Session` object.

        N)r   r   ÚcloseÚclear©r   r   r   r   ÚremoveP   s   
zscoped_session.removec                 K   s(   | j  ¡ r	tdƒ | jjdi |¤Ž dS )z�reconfigure the :class:`.sessionmaker` used by this
        :class:`.scoped_session`.

        See :meth:`.sessionmaker.configure`.

        ztAt least one scoped session is already present.  configure() can not affect sessions that have already been created.Nr   )r   r   r   r
   Ú	configure)r   Úkwargsr   r   r   r   a   s
   
ÿzscoped_session.configurec                    s   G ‡ ‡fdd„dt ƒ}|ƒ S )a"  return a class property which produces a :class:`.Query` object
        against the class and the current :class:`.Session` when called.

        e.g.::

            Session = scoped_session(sessionmaker())

            class MyClass(object):
                query = Session.query_property()

            # after mappers are defined
            result = MyClass.query.filter(MyClass.name=='foo').all()

        Produces instances of the session's configured query class by
        default.  To override and use a custom implementation, provide
        a ``query_cls`` callable.  The callable will be invoked with
        the class's mapper as a positional argument and a session
        keyword argument.

        There is no limit to the number of query properties placed on
        a class.

        c                       s   e Zd Z‡ ‡fdd„ZdS )z,scoped_session.query_property.<locals>.queryc                    sP   zt |ƒ}|rˆ rˆ |ˆ ¡ d�W S ˆ ¡  |¡W S W d S  tjy'   Y d S w )N)Úsession)r   r   ÚqueryÚorm_excÚUnmappedClassError)ÚsÚinstanceÚownerÚmapper©Ú	query_clsr   r   r   Ú__get__Œ   s   úÿz4scoped_session.query_property.<locals>.query.__get__N)Ú__name__Ú
__module__Ú__qualname__r(   r   r&   r   r   r   ‹   s    r   )Úobject)r   r'   r   r   r&   r   Úquery_propertyr   s   zscoped_session.query_property©N)
r)   r*   r+   Ú__doc__r
   r   r   r   r   r-   r   r   r   r   r	      s    
c                    s   ‡ fdd„}|S )Nc                    s   t |  ¡ ˆ ƒ|i |¤ŽS r.   ©Úgetattrr   )r   Úargsr   ©Únamer   r   Údo¡   s   zinstrument.<locals>.dor   ©r4   r5   r   r3   r   Ú
instrument    s   r7   c                    s"   ‡ fdd„}‡ fdd„}t ||ƒS )Nc                    s   t |  ¡ ˆ |ƒ d S r.   )Úsetattrr   )r   Úattrr3   r   r   Úset_¬   ó   zmakeprop.<locals>.set_c                    s   t |  ¡ ˆ ƒS r.   r0   r   r3   r   r   Úget¯   s   zmakeprop.<locals>.get)Úproperty)r4   r:   r<   r   r3   r   Úmakeprop«   s   
r>   )
ÚbindÚdirtyÚdeletedÚnewÚidentity_mapÚ	is_activeÚ	autoflushÚno_autoflushÚinfoÚ
autocommitc                    s   ‡ fdd„}t |ƒS )Nc                    s   t tˆ ƒ|i |¤ŽS r.   )r1   r   )Úclsr2   r   r3   r   r   r5   Å   r;   zclslevel.<locals>.do)Úclassmethodr6   r   r3   r   ÚclslevelÄ   s   rK   )Ú	close_allÚobject_sessionÚidentity_keyN)Ú r   r   r    r   r   r   Úutilr   r   r   Ú__all__r,   r	   ÚScopedSessionr7   Úpublic_methodsÚmethr8   r>   ÚproprK   r   r   r   r   Ú<module>   s,    	

ÿ