σ
Ώb]c        
   @   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 d e f d	     YZ e Z d
   Z x' e j D] Z e e e e e   q¨ Wd   Z x$ d D] Z e e e e e   qΨ Wd   Z x$ d D] Z e e e e e   qWd S(   i   (   t   class_mapper(   t   exc(   t   Sessioni   (   t   ScopedRegistry(   t   ThreadLocalRegistry(   t   warnt   scoped_sessionc           B   sG   e  Z d  Z d Z d d  Z d   Z d   Z d   Z d d  Z	 RS(   st   Provides scoped management of :class:`.Session` objects.

    See :ref:`unitofwork_contextual` for a tutorial.

    c         C   s7   | |  _  | r$ t | |  |  _ n t |  |  _ d S(   sΡ  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(   t   session_factoryR   t   registryR   (   t   selfR   t	   scopefunc(    (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   __init__!   s    	c         K   s[   | rM |  j  j   r' t j d   qW |  j |   } |  j  j |  | Sn
 |  j    Sd S(   sΞ  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.

        sE   Scoped session is already present; no new arguments may be specified.N(   R   t   hast   sa_exct   InvalidRequestErrorR   t   set(   R	   t   kwt   sess(    (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   __call__8   s    c         C   s3   |  j  j   r" |  j    j   n  |  j  j   d S(   sζ  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   t   closet   clear(   R	   (    (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   removeP   s    c         K   s0   |  j  j   r t d  n  |  j j |   d S(   s   reconfigure the :class:`.sessionmaker` used by this
        :class:`.scoped_session`.

        See :meth:`.sessionmaker.configure`.

        st   At least one scoped session is already present.  configure() can not affect sessions that have already been created.N(   R   R   R   R   t	   configure(   R	   t   kwargs(    (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR   a   s    
c            s&   d t  f    f d     Y} |   S(   s"  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.

        t   queryc              s   e  Z    f d    Z RS(   c            sd   yH t  |  } | rG   r1   | d  j   S j   j |  Sn  Wn t j k
 r_ d  SXd  S(   Nt   session(   R    R   R   t   orm_exct   UnmappedClassErrort   None(   t   st   instancet   ownert   mapper(   t	   query_clsR	   (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   __get__   s    (   t   __name__t
   __module__R"   (    (   R!   R	   (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR      s   (   t   object(   R	   R!   R   (    (   R!   R	   s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   query_propertyr   s    N(
   R#   R$   t   __doc__R   R   R   R   R   R   R&   (    (    (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR      s   			c            s     f d   } | S(   Nc            s   t  |  j      | |   S(   N(   t   getattrR   (   R	   t   argsR   (   t   name(    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   do‘   s    (    (   R*   R+   (    (   R*   s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt
   instrument    s    c            s+     f d   }   f d   } t  | |  S(   Nc            s   t  |  j     |  d  S(   N(   t   setattrR   (   R	   t   attr(   R*   (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   set_¬   s    c            s   t  |  j      S(   N(   R(   R   (   R	   (   R*   (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   get―   s    (   t   property(   R*   R/   R0   (    (   R*   s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   makeprop«   s    t   bindt   dirtyt   deletedt   newt   identity_mapt	   is_activet	   autoflusht   no_autoflusht   infot
   autocommitc            s     f d   } t  |  S(   Nc            s   t  t    | |   S(   N(   R(   R   (   t   clsR)   R   (   R*   (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR+   Ε   s    (   t   classmethod(   R*   R+   (    (   R*   s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   clslevelΔ   s    t	   close_allt   object_sessiont   identity_keyN(
   R3   R4   R5   R6   R7   R8   R9   R:   R;   R<   (   R@   RA   RB   (   t    R    R   R   R   R   R   t   utilR   R   R   t   __all__R%   R   t   ScopedSessionR,   t   public_methodst   methR-   R2   t   propR?   (    (    (    s5   lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   <module>   s8   			
         
	