ó
`¾Tc           @€  sè   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z d d	 l m Z m Z d
 e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sµ   
    flask.ctx
    ~~~~~~~~~

    Implements the objects required to keep the context.

    :copyright: (c) 2011 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿ(   t   with_statementN(   t   update_wrapper(   t   HTTPExceptioni   (   t   _request_ctx_stackt   _app_ctx_stack(   t   blueprint_is_module(   t   appcontext_pushedt   appcontext_poppedt   _AppCtxGlobalsc           B€  s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s   A plain object.c         C€  s   |  j  j | | ƒ S(   N(   t   __dict__t   get(   t   selft   namet   default(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR
      s    c         C€  s   | |  j  k S(   N(   R	   (   R   t   item(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   __contains__   s    c         C€  s   t  |  j ƒ S(   N(   t   iterR	   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   __iter__!   s    c         C€  s0   t  j } | d  k	 r# d | j j St j |  ƒ S(   Ns   <flask.g of %r>(   R   t   topt   Nonet   appR   t   objectt   __repr__(   R   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR   $   s    	N(   t   __name__t
   __module__t   __doc__R   R
   R   R   R   (    (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR      s
   		c         C€  s   t  j j j |  ƒ |  S(   s³  Executes a function after this request.  This is useful to modify
    response objects.  The function is passed the response object and has
    to return the same or a new one.

    Example::

        @app.route('/')
        def index():
            @after_this_request
            def add_header(response):
                response.headers['X-Foo'] = 'Parachute'
                return response
            return 'Hello World!'

    This is more useful if a function other than the view function wants to
    modify a response.  For instance think of a decorator that wants to add
    some headers without converting the return value into a response object.

    .. versionadded:: 0.9
    (   R   R   t   _after_request_functionst   append(   t   f(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   after_this_request+   s    c         €  sO   t  j } | d k r$ t d ƒ ‚ n  | j ƒ  ‰ ‡ ‡  f d †  } t | ˆ  ƒ S(   sà  A helper function that decorates a function to retain the current
    request context.  This is useful when working with greenlets.  The moment
    the function is decorated a copy of the request context is created and
    then pushed when the function is called.

    Example::

        import gevent
        from flask import copy_current_request_context

        @app.route('/')
        def index():
            @copy_current_request_context
            def do_some_work():
                # do some work here, it can access flask.request like you
                # would otherwise in the view function.
                ...
            gevent.spawn(do_some_work)
            return 'Regular response'

    .. versionadded:: 0.10
    s|   This decorator can only be used at local scopes when a request context is on the stack.  For instance within view functions.c          €  s   ˆ   ˆ |  | Ž  SWd  QXd  S(   N(    (   t   argst   kwargs(   t   reqctxR   (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   wrappera   s    N(   R   R   R   t   RuntimeErrort   copyR   (   R   R   R!   (    (   R   R    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   copy_current_request_contextD   s    	c           C€  s   t  j d k	 S(   sú  If you have code that wants to test if a request context is there or
    not this function can be used.  For instance, you may want to take advantage
    of request information if the request object is available, but fail
    silently if it is unavailable.

    ::

        class User(db.Model):

            def __init__(self, username, remote_addr=None):
                self.username = username
                if remote_addr is None and has_request_context():
                    remote_addr = request.remote_addr
                self.remote_addr = remote_addr

    Alternatively you can also just test any of the context bound objects
    (such as :class:`request` or :class:`g` for truthness)::

        class User(db.Model):

            def __init__(self, username, remote_addr=None):
                self.username = username
                if remote_addr is None and request:
                    remote_addr = request.remote_addr
                self.remote_addr = remote_addr

    .. versionadded:: 0.7
    N(   R   R   R   (    (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   has_request_contextg   s    c           C€  s   t  j d k	 S(   sÀ   Works like :func:`has_request_context` but for the application
    context.  You can also just do a boolean check on the
    :data:`current_app` object instead.

    .. versionadded:: 0.9
    N(   R   R   R   (    (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   has_app_context‡   s    t
   AppContextc           B€  s>   e  Z d  Z d „  Z d „  Z d d „ Z d „  Z d „  Z RS(   s]  The application context binds an application object implicitly
    to the current thread or greenlet, similar to how the
    :class:`RequestContext` binds request information.  The application
    context is also implicitly created if a request context is created
    but the application is not on top of the individual application
    context.
    c         C€  s7   | |  _  | j d  ƒ |  _ | j ƒ  |  _ d |  _ d  S(   Ni    (   R   t   create_url_adapterR   t   url_adaptert   app_ctx_globals_classt   gt   _refcnt(   R   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   __init__š   s    	c         C€  s0   |  j  d 7_  t j |  ƒ t j |  j ƒ d S(   s-   Binds the app context to the current context.i   N(   R,   R   t   pushR   t   sendR   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR.   £   s    c         C€  s’   |  j  d 8_  |  j  d k rP | d k r= t j ƒ  d } n  |  j j | ƒ n  t j ƒ  } | |  k s~ t d | |  f ƒ ‚ t	 j
 |  j ƒ d S(   s   Pops the app context.i   i    s-   Popped wrong app context.  (%r instead of %r)N(   R,   R   t   syst   exc_infoR   t   do_teardown_appcontextR   t   popt   AssertionErrorR   R/   (   R   t   exct   rv(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR3   ©   s    c         C€  s   |  j  ƒ  |  S(   N(   R.   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt	   __enter__µ   s    
c         C€  s   |  j  | ƒ d  S(   N(   R3   (   R   t   exc_typet	   exc_valuet   tb(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   __exit__¹   s    N(	   R   R   R   R-   R.   R   R3   R7   R;   (    (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR'   ‘   s   				t   RequestContextc           B€  sŒ   e  Z d  Z d d „ Z d „  Z d „  Z e e e ƒ Z [ [ d „  Z	 d „  Z
 d „  Z d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z RS(   sæ  The request context contains all request relevant information.  It is
    created at the beginning of the request and pushed to the
    `_request_ctx_stack` and removed at the end of it.  It will create the
    URL adapter and request object for the WSGI environment provided.

    Do not attempt to use this class directly, instead use
    :meth:`~flask.Flask.test_request_context` and
    :meth:`~flask.Flask.request_context` to create this object.

    When the request context is popped, it will evaluate all the
    functions registered on the application for teardown execution
    (:meth:`~flask.Flask.teardown_request`).

    The request context is automatically popped at the end of the request
    for you.  In debug mode the request context is kept around if
    exceptions happen so that interactive debuggers have a chance to
    introspect the data.  With 0.4 this can also be forced for requests
    that did not fail and outside of `DEBUG` mode.  By setting
    ``'flask._preserve_context'`` to `True` on the WSGI environment the
    context will not pop itself at the end of the request.  This is used by
    the :meth:`~flask.Flask.test_client` for example to implement the
    deferred cleanup functionality.

    You might find this helpful for unittests where you need the
    information from the context local around for a little longer.  Make
    sure to properly :meth:`~werkzeug.LocalStack.pop` the stack yourself in
    that situation, otherwise your unittests will leak memory.
    c         C€  sÝ   | |  _  | d  k r' | j | ƒ } n  | |  _ | j |  j ƒ |  _ d  |  _ d  |  _ g  |  _ t	 |  _
 d  |  _ g  |  _ |  j ƒ  |  j j } | d  k	 rÙ | j j | ƒ } | d  k	 rÙ t | ƒ rÙ t |  j _ qÙ n  d  S(   N(   R   R   t   request_classt   requestR(   R)   t   flashest   sessiont   _implicit_app_ctx_stackt   Falset	   preservedt   _preserved_excR   t   match_requestt	   blueprintt
   blueprintsR
   R   t   Truet   _is_old_module(   R   R   t   environR>   RF   t   bp(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR-   Û   s"    								
c         C€  s
   t  j j S(   N(   R   R   R+   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   _get_g  s    c         C€  s   | t  j _ d  S(   N(   R   R   R+   (   R   t   value(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   _set_g  s    c         C€  s%   |  j  |  j d |  j j d |  j ƒS(   sr  Creates a copy of this request context with the same request object.
        This can be used to move a request context to a different greenlet.
        Because the actual request object is the same this cannot be used to
        move a request context to a different thread unless access to the
        request object is locked.

        .. versionadded:: 0.10
        RJ   R>   (   t	   __class__R   R>   RJ   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR#   
  s    	c         C€  sW   y1 |  j  j d t ƒ \ } |  j _ | |  j _ Wn t k
 rR } | |  j _ n Xd S(   sZ   Can be overridden by a subclass to hook into the matching
        of the request.
        t   return_ruleN(   R)   t   matchRH   R>   t	   view_argst   url_ruleR   t   routing_exception(   R   RS   t   e(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyRE     s
    !c         C€  sá   t  j } | d k	 r1 | j r1 | j | j ƒ n  t j } | d k sX | j |  j k r„ |  j j ƒ  } | j	 ƒ  |  j
 j | ƒ n |  j
 j d ƒ t  j	 |  ƒ |  j j |  j ƒ |  _ |  j d k rÝ |  j j ƒ  |  _ n  d S(   s1   Binds the request context to the current context.N(   R   R   R   RC   R3   RD   R   R   t   app_contextR.   RA   R   t   open_sessionR>   R@   t   make_null_session(   R   R   t   app_ctx(    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR.   #  s    
		
c         C€  s  |  j  j ƒ  } t } |  j  s¯ t |  _ d |  _ | d k rO t j ƒ  d } n  |  j j	 | ƒ t
 t d ƒ r{ t j ƒ  n  t |  j d d ƒ } | d k	 r¦ | ƒ  n  t } n  t j ƒ  } | |  k sÝ t d | |  f ƒ ‚ | rö d | j j d <n  | d k	 r| j | ƒ n  d S(   s  Pops the request context and unbinds it by doing that.  This will
        also trigger the execution of functions registered by the
        :meth:`~flask.Flask.teardown_request` decorator.

        .. versionchanged:: 0.9
           Added the `exc` argument.
        i   t	   exc_cleart   closes1   Popped wrong request context.  (%r instead of %r)s   werkzeug.requestN(   RA   R3   RB   RC   R   RD   R0   R1   R   t   do_teardown_requestt   hasattrRZ   t   getattrR>   RH   R   R4   RJ   (   R   R5   RY   t   clear_requestt   request_closeR6   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR3   E  s*    			
	c         C€  sS   |  j  j j d ƒ s- | d  k	 rB |  j j rB t |  _ | |  _ n |  j	 | ƒ d  S(   Ns   flask._preserve_context(
   R>   RJ   R
   R   R   t   preserve_context_on_exceptionRH   RC   RD   R3   (   R   R5   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   auto_popp  s
    	c         C€  s   |  j  ƒ  |  S(   N(   R.   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR7   x  s    
c         C€  s   |  j  | ƒ d  S(   N(   Rb   (   R   R8   R9   R:   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR;   |  s    c         C€  s,   d |  j  j |  j j |  j j |  j j f S(   Ns   <%s '%s' [%s] of %s>(   RO   R   R>   t   urlt   methodR   R   (   R   (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR   „  s
    			N(   R   R   R   R   R-   RL   RN   t   propertyR+   R#   RE   R.   R3   Rb   R7   R;   R   (    (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyR<   ½   s   (					"+			(   R   t
   __future__R    R0   t	   functoolsR   t   werkzeug.exceptionsR   t   globalsR   R   t   moduleR   t   signalsR   R   R   R   R   R$   R%   R&   R'   R<   (    (    (    sO   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/ctx.pyt   <module>
   s   		#	 	
,