
    ؄_                     @   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e          ZeZ	 d
 Zej        D ]Z eee ee                     d ZdD ]Z eee ee                     d ZdD ]Z eee ee                     dS )   )class_mapper)exc)Session   )ScopedRegistry)ThreadLocalRegistry)warnscoped_sessionc                   :    e Zd ZdZdZ	 ddZd Zd Zd ZddZ	dS )	r
   ztProvides scoped management of :class:`.Session` objects.

    See :ref:`unitofwork_contextual` for a tutorial.

    Nc                 n    || _         |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   	scopefuncs      [/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/sqlalchemy/orm/scoping.py__init__zscoped_session.__init__!   s=       / 	A*?IFFDMMM/@@DMMM    c                     |rV| j                                         rt          j        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.N )r   hassa_excInvalidRequestErrorr   set)r   kwsesss      r   __call__zscoped_session.__call__8   s      	#}  "" 09  
 ,t+11b11!!$'''==??"r   c                     | 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   s    r   removezscoped_session.removeP   sN     = 	$MMOO!!###r   c                 z    | j                                         rt          d            | j        j        di | dS )zreconfigure 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   kwargss     r   r"   zscoped_session.configurea   sU     = 	(   	'&0000000r   c                 F      G  fddt                     } |            S )a0  return a class property which produces a :class:`_query.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                       e Zd Z fdZdS ),scoped_session.query_property.<locals>.queryc                     	 t          |          }|rHr |                                          S                                                     |          S d S # t          j        $ r Y d S w xY w)N)session)r   r   queryorm_excUnmappedClassError)sinstanceownermapper	query_clsr   s       r   __get__z4scoped_session.query_property.<locals>.query.__get__   s    
 )%00F A$ A#,9VT]]__#M#M#MM $(==??#8#8#@#@@A A 1      44 s   1A &A A10A1N)__name__
__module____qualname__r1   )r0   r   s   r   r)   r&      s3                       r   r)   )object)r   r0   r)   s   `` r   query_propertyzscoped_session.query_propertyr   sJ    4	  	  	  	  	  	  	  	 F 	  	  	  uwwr   N)
r2   r3   r4   __doc__r   r   r   r    r"   r6   r   r   r   r
   r
      s          O
A A A A.# # #0  "1 1 1"( ( ( ( ( (r   c                       fd}|S )Nc                 T     t          |                                           |i |S r7   getattrr   )r   argsr#   names      r   dozinstrument.<locals>.do   s*    -wt}}--t>v>>>r   r   r>   r?   s   ` r   
instrumentrA      s#    ? ? ? ? ? Ir   c                 8      fd} fd}t          ||          S )Nc                 N    t          |                                 |           d S r7   )setattrr   )r   attrr>   s     r   set_zmakeprop.<locals>.set_   s#    t,,,,,r   c                 H    t          |                                           S r7   r;   )r   r>   s    r   getzmakeprop.<locals>.get   s    t}}---r   )property)r>   rF   rH   s   `  r   makeproprJ      sG    - - - - -. . . . . Cr   )
binddirtydeletednewidentity_map	is_active	autoflushno_autoflushinfo
autocommitc                 ,      fd}t          |          S )Nc                 :     t          t                    |i |S r7   )r<   r   )clsr=   r#   r>   s      r   r?   zclslevel.<locals>.do   s"    %ww%%t6v666r   )classmethodr@   s   ` r   clslevelrY      s*    7 7 7 7 7 r??r   )	close_allobject_sessionidentity_keyN) r   r   r*   r(   r   r   utilr   r   r	   __all__r5   r
   ScopedSessionrA   public_methodsmethrD   rJ   proprY   r   r   r   <module>rd      s                           ! ! ! ! ! ! & & & & & &       
F F F F FV F F FR  +   " 4 4DGND**T"2"23333   2 2D GND((4..1111   < 2 2DGND((4..11112 2r   