σ
H`ΎTc        	   @   s  d  d l  m Z d  d l m Z m Z m Z d d l  m Z 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   exc(   t   ScopedRegistryt   ThreadLocalRegistryt   warni   (   t   class_mapperR    (   t   Sessiont   scoped_sessionc           B   sA   e  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(    (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   __init__   s    	c         K   s   | r{ | j  d t  } | d k	 rk |  j j   rE t j d   qx |  j |   } |  j j |  | Sq |  j |   Sn
 |  j   Sd S(   s  Return the current :class:`.Session`, creating it
        using the session factory if not present.

        :param \**kw: Keyword arguments will be passed to the
         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.

        t   scopesE   Scoped session is already present; no new arguments may be specified.N(	   t   popt   Falset   NoneR   t   hast   sa_exct   InvalidRequestErrorR   t   set(   R	   t   kwR   t   sess(    (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   __call__.   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	   (    (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   removeI   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(    (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR   Z   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   UnmappedClassErrorR   (   t   st   instancet   ownert   mapper(   t	   query_clsR	   (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   __get__   s    (   t   __name__t
   __module__R%   (    (   R	   R$   (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR      s   (   t   object(   R	   R$   R   (    (   R	   R$   s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   query_propertyi   s    N(	   R&   R'   t   __doc__R   R   R   R   R   R)   (    (    (    s\   /var/www/send.findwatt.com/datamanager/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(    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   do   s    (    (   R-   R.   (    (   R-   s\   /var/www/send.findwatt.com/datamanager/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-   (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR      s    c            s   t  |  j      S(   N(   R+   R   (   R	   (   R-   (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   get‘   s    (   t   property(   R-   R   R2   (    (   R-   s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   makeprop   s    t   bindt   dirtyt   deletedt   newt   identity_mapt	   is_activet	   autoflusht   no_autoflusht   infoc            s     f d   } t  |  S(   Nc            s   t  t    | |   S(   N(   R+   R   (   t   clsR,   R   (   R-   (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyR.   ¬   s    (   t   classmethod(   R-   R.   (    (   R-   s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   clslevel«   s    t	   close_allt   object_sessiont   identity_keyN(	   R5   R6   R7   s   newR9   R:   R;   R<   s   info(   RA   RB   RC   (   t    R    R   t   utilR   R   R   R   R   R   R   t   __all__R(   R   t   ScopedSessionR/   t   public_methodst   methR0   R4   t   propR@   (    (    (    s\   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/orm/scoping.pyt   <module>   s    				
	