B
    .`                 @   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xejD ]Zeeeee q~W dd ZxdD ]Zeeeee qW dd ZxdD ]Zeeeee qW dS )   )class_mapper)exc)Session   )ScopedRegistry)ThreadLocalRegistry)warnscoped_sessionc               @   s@   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||| _n
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
   Z	scopefunc r   E/home/dcms/DCMS/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py__init__!   s    zscoped_session.__init__c             K   sD   |r8| j  rtdq@| jf |}| j | |S n|   S d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_excZ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f | 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.N)r   r   r   r
   	configure)r   kwargsr   r   r   r   a   s    
zscoped_session.configurec                s   G  fd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                   s   e Zd Z fddZdS )z,scoped_session.query_property.<locals>.queryc                sN   y2t |}|r0 r" | dS  |S W n tjk
rH   d S X d S )N)session)r   r   queryorm_excZUnmappedClassError)sinstanceownerZmapper)	query_clsr   r   r   __get__   s    z4scoped_session.query_property.<locals>.query.__get__N)__name__
__module____qualname__r"   r   )r!   r   r   r   r      s   r   )object)r   r!   r   r   )r!   r   r   query_propertyr   s    zscoped_session.query_property)N)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 |   ||S )N)getattrr   )r   argsr   )namer   r   do   s    zinstrument.<locals>.dor   )r+   r,   r   )r+   r   
instrument   s    r-   c                s"    fdd} fdd}t ||S )Nc                s   t |   | d S )N)setattrr   )r   attr)r+   r   r   set_   s    zmakeprop.<locals>.set_c                s   t |   S )N)r)   r   )r   )r+   r   r   get   s    zmakeprop.<locals>.get)property)r+   r0   r1   r   )r+   r   makeprop   s    r3   )
bindZdirtyZdeletednewZidentity_mapZ	is_activeZ	autoflushZno_autoflushinfoZ
autocommitc                s    fdd}t |S )Nc                s   t t ||S )N)r)   r   )clsr*   r   )r+   r   r   r,      s    zclslevel.<locals>.do)classmethod)r+   r,   r   )r+   r   clslevel   s    r9   )Z	close_allZobject_sessionZidentity_keyN) r   r   r   r   r   r   utilr   r   r   __all__r&   r	   ZScopedSessionr-   Zpublic_methodsmethr.   r3   propr9   r   r   r   r   <module>   s(    


