ó
“UoDc           @   s®  d  d l  m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d l Z d  d l	 Z	 d  d l
 Z
 d  d l Z y d  d l m Z Wn! e k
 r­ d  d l m Z n Xe ƒ  Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e	 j d e	 j ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e	 j d ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   stat(   t   timet   mktime(   t	   parsedate(   t   timegmN(   t   Lockt   Entryc           B   s   e  Z d  Z d „  Z RS(   s+    A cache entry, mostly an internal object. c         C   s/   t  j |  ƒ | |  _ t |  _ t ƒ  |  _ d  S(   N(   t   objectt   __init__t   _keyt   NOT_INITIALIZEDt   _valueR   t   _lock(   t   selft   key(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   )   s    		(   t   __name__t
   __module__t   __doc__R   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   '   s   t   Cachec           B   sª   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s+    An abstract, multi-threaded cache object. i    c         C   sh   t  j |  ƒ | |  _ i  |  _ t ƒ  |  _ |  j rd t d ƒ |  _ |  j |  j _	 |  j |  j _
 n  d S(   sÚ    Builds a cache with a limit of max_size entries.
            If this limit is exceeded, the Least Recently Used entry is discarded.
            if max_size==0, the cache is unbounded (no LRU rule is applied).
        N(   R   R   t   _maxsizet   _dictR   R   R   t   Nonet   _headt	   _previoust   _next(   R   t   max_size(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   2   s    			c         C   s^   |  j  | ƒ } |  j | ƒ } | j j ƒ  z |  j | | ƒ |  j ƒ  Wd | j j ƒ  Xd S(   s2    Populates the cache with a given name and value. N(   R   t
   _get_entryR   t   acquiret   _packt   committ   release(   R   t   namet   valueR   t   entry(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   __setitem__B   s    c         C   s   |  j  | ƒ d S(   s=    Gets a value from the cache, builds it if required.
        i   (   t
   _checkitem(   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   __getitem__O   s    c         C   s?   |  j  j ƒ  z |  j | ƒ } |  j | =Wd  |  j  j ƒ  Xd  S(   N(   R   R   R   R   R   (   R   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   __delitem__T   s
    c         C   sª   |  j  j ƒ  zˆ |  j j | ƒ } | sw t | ƒ } | |  j | <|  j r d  | _ | _ |  j	 | ƒ |  j
 ƒ  q n |  j r |  j	 | ƒ n  | SWd  |  j  j ƒ  Xd  S(   N(   R   R   R   t   getR   R   R   R   R   t   _accesst	   _checklruR   (   R   R   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   \   s    		c         C   s!  |  j  | ƒ } |  j | ƒ } | j j ƒ  zá |  j | ƒ } t } | t k rŸ |  j | | | ƒ } |  j | | | | ƒ } t	 } |  j
 | | ƒ |  j ƒ  n\ |  j | | | ƒ } | d k	 rû |  j | | | | ƒ } t	 } |  j
 | | ƒ |  j ƒ  n  | | | | f SWd | j j ƒ  Xd S(   s±    Gets a value from the cache, builds it if required.
            Returns a tuple is_new, key, value, entry.
            If is_new is True, the result had to be rebuilt.
        N(   R   R   R   R   t   _unpackt   FalseR
   t   checkt   buildt   TrueR   R   R   R   (   R   R   R   R!   R    t   is_newt   opened(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR#   m   s(    c         C   sC   |  j  r; |  j j ƒ  z |  j j j SWd |  j j ƒ  Xn d Sd S(   s$    Returns the Most Recently Used key N(   R   R   R   R   R   R	   R   R   (   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   mru‹   s    	c         C   sC   |  j  r; |  j j ƒ  z |  j j j SWd |  j j ƒ  Xn d Sd S(   s%    Returns the Least Recently Used key N(   R   R   R   R   R   R	   R   R   (   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   lru–   s    	c         C   s   | S(   sO    Override this method to extract a key from the name passed to the [] operator (    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   ¡   s    c         C   s   d S(   s}    Override this method if you want to do something each time the underlying dictionary is modified (e.g. make it persistent). N(    (   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   ¥   s    c         C   s]   |  j  j ƒ  z; |  j j ƒ  |  j rG |  j |  j _ |  j |  j _ n  Wd |  j  j ƒ  Xd S(   s    Clears the cache N(	   R   R   R   t   clearR   R   R   R   R   (   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR2   ©   s    	c         C   s   d S(   s«   Override this method to check whether the entry with the given name is stale. Return None if it is fresh
            or an opened resource if it is stale. The object returned will be passed to the 'build' method as the 'opened' parameter.
            Use the 'entry' parameter to store meta-data if required. Don't worry about multiple threads accessing the same name,
            as this method is properly isolated.
        N(   R   (   R   R   R   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR+   ´   s    c         C   s   t  ƒ  ‚ d S(   sô    Build the cached value with the given name from the given opened resource. Use entry to obtain or store meta-data if needed.
             Don't worry about multiple threads accessing the same name, as this method is properly isolated.
        N(   t   NotImplementedError(   R   R   R   R/   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR,   ¼   s    c         C   s    | j  |  j k	 rœ | j d k	 rB | j  | j _  | j | j  _ n  |  j j | _ | | j _  |  j | _  | | j  _ |  j j  |  j k rœ | |  j _  qœ n  d S(   sR    Internal use only, must be invoked within a cache lock. Updates the access list. N(   R   R   R   R   (   R   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR'   Â   s    c         C   sV   t  |  j ƒ |  j k rR |  j j } | j | j _ | j | j _ |  j | j =n  d S(   sZ    Internal use only, must be invoked within a cache lock. Removes the LRU entry if needed. N(   t   lenR   R   R   R   R   R	   (   R   R1   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR(   Ñ   s
    c         C   s   | | _  d S(   s    Store the value in the entry. N(   R   (   R   R!   R    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   Ù   s    c         C   s   | j  S(   sL    Recover the value from the entry, returns NOT_INITIALIZED if it is not OK. (   R   (   R   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR)   Ý   s    (   R   R   R   R   R"   R$   R%   R   R#   R0   R1   R   R   R2   R+   R,   R'   R(   R   R)   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   /   s$   															t	   WeakCachec           B   s    e  Z d  Z d „  Z d „  Z RS(   s   This cache holds weak references to the values it stores. Whenever a value is not longer
        normally referenced, it is removed from the cache. Useful for sharing the result of long
        computations but letting them go as soon as they are not needed by anybody.
    c            s%   t  j | ‡  ‡ f d †  ƒ ˆ  _ d  S(   Nc            s   ˆ j  ˆ  j ƒ S(   N(   R%   R	   (   t   ref(   R!   R   (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   <lambda>è   s    (   t   weakrefR6   R   (   R   R!   R    (    (   R!   R   s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   ç   s    c         C   s7   | j  t k r t S| j  ƒ  } | d  k r/ t S| Sd  S(   N(   R   R
   R   (   R   R!   R    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR)   ê   s    (   R   R   R   R   R)   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR5   á   s   	t	   FileCachec           B   s/   e  Z d  Z d d d „ Z d „  Z d „  Z RS(   s   A file cache. Returns the content of the files as a string, given their filename.
        Whenever the files are modified (according to their modification time) the cache is updated.
        Override the build method to obtain more interesting behaviour.
    i    t   rbc         C   s   t  j |  | ƒ | |  _ d  S(   N(   R   R   t   mode(   R   R   R;   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   ù   s    c         C   sg   t  | ƒ j } | j t k r7 | | _ t | |  j ƒ S| j | k r_ | | _ t | |  j ƒ Sd  Sd  S(   N(   R    t   st_mtimeR   R
   t
   _timestampt   fileR;   R   (   R   R   R   R!   t	   timestamp(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR+   ý   s    		c         C   s    z | j  ƒ  SWd | j ƒ  Xd S(   sQ    Return the content of the file as a string. Override this for better behaviour. N(   t   readt   close(   R   R   R   R/   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR,   
  s    (   R   R   R   R   R+   R,   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR9   ô   s   	c         C   s   t  t |  ƒ ƒ S(   N(   R   R   (   t   t(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   parseRFC822Time  s    s   max-age\s*=\s*(\d+)t
   HTTPEntityc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   entityt   metadata(   R   RE   RF   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR     s    	c         C   s   d t  |  j ƒ |  j f S(   Ns   HTTPEntity(%s, %s)(   t   reprRE   RF   (   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   __repr__  s    c         C   s   |  j  S(   N(   RE   (   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   __str__  s    (   R   R   R   RH   RI   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyRD     s   		t	   HTTPCachec           B   s    e  Z d  Z d „  Z d „  Z RS(   sÔ    An HTTP cache. Returns the entity found at the given URL.
        Uses Expires, ETag and Last-Modified headers to minimize bandwidth usage.
        Partial Cache-Control support (only max-age is supported).
    c         C   s  t  j | ƒ } y t ƒ  | j k  r( d  SWn t k
 r< n Xy  | j \ } } | | j | <Wn t k
 rp n Xd  } yNt  j | ƒ } | j	 ƒ  } t
 }	 yE t j | d ƒ }
 |
 rß t ƒ  t |
 j d ƒ ƒ | _ t }	 n  Wn t t f k
 rù n X|	 sUy> t | d ƒ } t | d ƒ } t ƒ  | | | _ t }	 WqUt k
 rQqUXn  t
 } y d | d f | _ t } Wn t k
 r‹n X| sÀy d | d f | _ WqÀt k
 r¼qÀXn  | SWnB t  j k
 r	} | rí| j ƒ  n  | j d	 k r d  S| ‚ n Xd  S(
   Ns   cache-controli   t   datet   expiress   If-None-Matcht   etags   If-Modified-Sinces   last-modifiedi0  (   t   urllib2t   RequestR   t   _expiresR   t   AttributeErrort
   _validatort   headerst   urlopent   infoR*   t
   re_max_aget   matcht   intt   groupR-   t   KeyErrort
   ValueErrorRC   t	   HTTPErrorRA   t   code(   R   R   R   R!   t   requestt   headerR    R/   RS   t
   expirationRW   RK   RL   t
   validationt   error(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR+   &  sb    

 c         C   s5   z# t  | j ƒ  t | j ƒ  ƒ ƒ SWd  | j ƒ  Xd  S(   N(   RD   R@   t   dictRU   RA   (   R   R   R   R/   R!   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR,   _  s    #(   R   R   R   R+   R,   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyRJ   !  s   	9s   \W+t   ModuleCachec           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s¿    A module cache. Give it a file name, it returns a module
        which results from the execution of the Python script it contains.
        This module is not inserted into sys.modules.
    i    c         C   s   t  j |  | d ƒ d  S(   Nt   r(   R9   R   (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   l  s    c         B   sI   z7 e  j e j d | ƒ ƒ } | | _ | | j U| SWd  | j ƒ  Xd  S(   Nt   _(   t   newt   modulet   re_not_wordt   subt   __file__t   __dict__RA   (   R   R   R   R/   R!   Rh   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR,   o  s    	(   R   R   R   R   R,   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyRd   g  s   t   HttpModuleCachec           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s¿    A module cache. Give it an HTTP URL, it returns a module
        which results from the execution of the Python script it contains.
        This module is not inserted into sys.modules.
    i    c         C   s   t  j |  | ƒ d  S(   N(   RJ   R   (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   }  s    c         B   ss   za e  j e j d | ƒ ƒ } | | _ | j ƒ  j d d ƒ } e | | d ƒ } | | j U| SWd  | j	 ƒ  Xd  S(   NRf   s   
s   
t   exec(
   Rg   Rh   Ri   Rj   Rk   R@   t   replacet   compileRl   RA   (   R   R   R   R/   R!   Rh   t   textR]   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR,   €  s    	(   R   R   R   R   R,   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyRm   x  s   t   FunctionCachec           B   s&   e  Z d  d „ Z d „  Z d „  Z RS(   i    c         C   s   t  j |  | ƒ | |  _ d  S(   N(   R   R   t   function(   R   Rs   R   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR   Œ  s    c         O   s8   | r& t  | j ƒ  ƒ } |  | | f S|  | d f Sd  S(   N(    (   t   tuplet	   iteritems(   R   t   argst   kw(    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   __call__  s    c         C   s"   | \ } } |  j  | t | ƒ Ž  S(   N(   Rs   Rc   (   R   R   R   R/   R!   Rv   Rw   (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyR,   ˜  s    (   R   R   R   Rx   R,   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyRr   ‹  s   	(    t   osR    R   R   t   rfc822R   t   calendarR   RN   t   reR8   Rg   t	   threadingR   t   ImportErrort   dummy_threadingR   R
   R   R   R5   R9   RC   Rp   t   IRV   RD   RJ   Ri   Rd   Rm   Rr   (    (    (    s4   /usr/lib/python2.7/dist-packages/mod_python/cache.pyt   <module>   s0   	²	D