ó
ZËTc           @   sá   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 m Z d d l m Z d e f d „  ƒ  YZ d	 d d d e d d „ Z d d	 d e d e e d „ Z d d d „ Z d „  Z d S(   s)   Caching decorator, took as is from pylonsiÿÿÿÿN(   t   asbool(   t	   NoDefaultt   EmptyContext(   t   im_funct   im_class(   t   wrapst   cached_propertyc           B   s,   e  Z d  Z d „  Z d „  Z d d „ Z RS(   sh  
    Works like python @property but the decorated function only gets
    executed once, successive accesses to the property will just
    return the value previously stored into the object.

    The ``@cached_property`` decorator can be executed within a
    provided context, for example to make the cached property
    thread safe a Lock can be provided::

        from threading import Lock
        from tg.caching import cached_property

        class MyClass(object):
            @cached_property
            def my_property(self):
                return 'Value!'
            my_property.context = Lock()

    c         C   s=   | j  |  _  | j |  _ | j |  _ | |  _ t ƒ  |  _ d  S(   N(   t   __name__t
   __module__t   __doc__t   funcR   t   context(   t   selfR
   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   __init__   s
    	c         C   sJ   | j  j |  j t ƒ } | t k rF |  j | ƒ } | | j  |  j <n  | S(   N(   t   __dict__t   getR   R   R
   (   R   t   objt   value(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt
   _get_value#   s
    c         C   s1   | d  k r |  S|  j  |  j | ƒ SWd  QXd  S(   N(   t   NoneR   R   (   R   R   t   type(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   __get__*   s    
N(   R   R   R	   R   R   R   R   (    (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyR      s   		t   nevers   content-types   content-lengthc      	      sx  t  j j d ‰ ˆ j j d d ƒ } t | ƒ s> ˆ ˆ  ˆ Ž  S|
 pG i  }
 d } ˆ  rc ˆ  d } n  t | | | ƒ \ } } | rŽ | |
 d <n  t ˆ d d ƒ } | sµ t	 d ƒ ‚ n  | j
 | |
  } | d k rÜ d } n | } ‡  ‡ ‡ ‡ f d	 †  } | j | d
 | d | d | ƒ} |	 rpˆ j } g  | d D]" } | d j ƒ  | k r5| ^ q5| _ | d | _ n  | d S(   sÇ  
    Optional arguments:

    ``key_func``
        Function used to genereate the cache key, the function name
        and class will be used as the base for the cache key. If None
        the ``func`` itself will be used. It's usually handy when
        creating caches for decorated functions, for which we want the
        cache key to be generated on the decorated function and not on
        the decorator.
    ``key_dict``
        Arguments used to generate the cache key, only the arguments
        listed into this dictionary will be used to generate the
        cache key together with the key_func.
    ``expire``
        Time in seconds before cache expires, or the string "never".
        Defaults to "never"
    ``type``
        Type of cache to use: dbm, memory, file, memcached, or None for
        Beaker's default
    ``cache_headers``
        A tuple of header names indicating response headers that
        will also be cached.
    ``invalidate_on_startup``
        If True, the cache will be invalidated each time the application
        starts or is restarted.
    ``cache_response``
        Determines whether the response at the time beaker_cache is used
        should be cached or not, defaults to True.

        .. note::
            When cache_response is set to False, the cache_headers
            argument is ignored as none of the response is cached.

    If cache_enabled is set to False in the .ini file, then cache is
    disabled globally.
    s	   tg.localst   cache_enabledt   Truei    R   t   caches!   TurboGears Cache object not foundR   c       	      sO   ˆ ˆ  ˆ Ž  }  ˆ j  } | j } | j } t d | d | d d  d |  ƒ } | S(   Nt   headerst   statust   cookiest   content(   t   responset
   headerlistR   t   dictR   (   t   resultt   glob_responseR   R   t   full_response(   t   argsR
   t   kwargst	   tg_locals(    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   create_funcw   s    			t
   createfunct
   expiretimet	   starttimeR   R   R   N(   t   tgt   requestt   environt   configR   R    R   t   create_cache_keyt   getattrt	   Exceptiont	   get_cachet	   get_valueR   t   lowerR   R   (   R
   R$   R%   t   key_funct   key_dictt   expireR   R*   t   cache_headerst   cache_responset   cache_extra_argst   enabledR   t	   namespacet	   cache_keyt	   cache_objt   my_cachet   cache_expireR'   R   R"   t   header(    (   R$   R
   R%   R&   sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   _cached_call2   s:    *				%t   cache_defaultc   	         sO   | r t  j  ƒ  ‰ n d ‰ t ˆ ƒ ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  } | S(   s  Cache decorator utilizing Beaker. Caches a
    function that returns a pickle-able object as a result.

    Optional arguments:

    ``key``
        None - No variable key, uses function name as key
        "cache_default" - Uses all function arguments as the key
        string - Use kwargs[key] as key
        list - Use [kwargs[k] for k in list] as key
    ``expire``
        Time in seconds before cache expires, or the string "never".
        Defaults to "never"
    ``type``
        Type of cache to use: dbm, memory, file, memcached, or None for
        Beaker's default
    ``query_args``
        Uses the query arguments as the key, defaults to False
    ``cache_headers``
        A tuple of header names indicating response headers that
        will also be cached.
    ``invalidate_on_startup``
        If True, the cache will be invalidated each time the application
        starts or is restarted.
    ``cache_response``
        Determines whether the response at the time beaker_cache is used
        should be cached or not, defaults to True.

        .. note::
            When cache_response is set to False, the cache_headers
            argument is ignored as none of the response is cached.

    If cache_enabled is set to False in the .ini file, then cache is
    disabled globally.

    c      
      s7   t  ˆ  ƒ ‡ ‡ ‡ ‡ ‡  ‡ ‡ ‡ ‡ f	 d †  ƒ } | S(   Nc             sÌ   ˆ rž | j  ƒ  ‰  ˆ  j t ˆ |  | ƒ ƒ ˆ rM ˆ  j t j j j ƒ  ƒ n  ˆ d k r¤ t ˆ t ƒ r‡ t	 ‡  f d †  ˆ Dƒ ƒ ‰  q› i ˆ  ˆ ˆ 6‰  q¤ n d  ‰  t ˆ |  | ˆ ˆ  ˆ ˆ	 ˆ ˆ ˆ ˆ ƒ S(   NRC   c         3   s   |  ] } | ˆ  | f Vq d  S(   N(    (   t   .0t   k(   R6   (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pys	   <genexpr>Ç   s    (   t   copyt   updatet   _make_dict_from_argsR+   R,   t   GETt   mixedt
   isinstancet   listR    R   RB   (   R$   R%   (	   t   b_kwargsR8   R9   R7   R
   t   keyt
   query_argsR*   R   (   R6   sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   beaker_cached_call½   s    	(   R   (   R
   RP   (   RM   R8   R9   R7   RN   RO   R*   R   (   R
   sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   beaker_cache_decorate¼   s    3N(   t   timeR   t   set(	   RN   R7   R   RO   R8   t   invalidate_on_startupR9   RM   RQ   (    (   RM   R8   R9   R7   RN   RO   R*   R   sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   beaker_cache   s    )$c         C   sÂ   d } t |  ƒ } | r6 t |  ƒ } | }  |  j } n	 |  j } | ro | d d j d „  | j ƒ  Dƒ ƒ 7} n  | r‘ | r‘ t | d d ƒ } n  | r± d | j | j f | f S|  j | f Sd S(   s  Get a cache namespace and key used by the beaker_cache decorator.

    Example::
        from tg import cache
        from tg.caching import create_cache_key
        namespace, key = create_cache_key(MyController.some_method)
        cache.get_cache(namespace).remove(key)

    t    c         s   s%   |  ] \ } } d  | | f Vq d S(   s   %s=%sN(    (   RD   RE   t   v(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pys	   <genexpr>ê   s   t	   __class__s   %s.%sN(   R   R   R   R   t   joint   itemsR0   R   (   R
   R6   R   t   klst   imfuncR=   (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyR/   ×   s    
	c         C   sw   i  } xj t  t j |  ƒ d ƒ D]O \ } } | d k r  y | | | | <Wqo t k
 rk | | | | <qo Xq  q  W| S(   s"   Inspects function for name of argsi    R   (   t	   enumeratet   inspectt
   getargspect
   IndexError(   R
   R$   R%   t	   args_keyst   it   arg(    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyRH   ö   s    &(   s   content-types   content-length(   s   content-types   content-length(   R	   R+   R^   RR   t   tg.support.convertersR    t
   tg.supportR   R   t
   tg._compatR   R   t	   functoolsR   t   objectR   R   R   RB   t   FalseRU   R/   RH   (    (    (    sh   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/TurboGears2-2.3.3-py2.7.egg/tg/caching.pyt   <module>   s    $+	X	F