
    @};d                     R    d dl Z d dlZ d dlZd dlZd dlZd dlmZ dZ G d de      Z	y)    N)RBiQ c                   :    e Zd Zd	dZd Zd Zd
dZd Zd Zd Z	y)URLCachec                     || _         |j                  d      r|| _        n7t        j                  j	                  t        j                         |      | _        || _        || _        || _	        y)a#  
        Creates a new cache.  'name' is a symbolic name for the cache.
        'path' is either an absolute path to the cache directory, or a
        path relative to the user cache directory.
        'refresh' is the length of time for which cache entries are always
        considered valid.  'lifetime' is the maximum time an entry can live
        in the cache.  'discard' is the length of time for which a cache entry
        can go unused before being discarded.  These are all specified in days,
        with -1 meaning unlimited.
        /N)
name
startswithpathosjoinr   user_cache_dirrefreshdiscardlifetime)selfr   r
   r   r   r   s         :/usr/lib/x86_64-linux-gnu/rhythmbox/plugins/rb/URLCache.py__init__zURLCache.__init__&   sR     	??3DIR%6%6%8$?DI     c                 T   t        j                          }t        j                  j                  | j                        dk(  rt	        d| j                  z         yt	        d| j                  z         t        j
                  | j                        D ]>  }	 t        j                  j                  | j                  |      }t        j                  |      }| j                  dk7  rn|j                  | j                  t        z  z   |k  rKt	        d| j                  d|dt        ||j                  z
        d	       t        j                  |       | j                  dk7  ro|j                  | j                  t        z  z   |k  rLt	        d| j                  d|dt        ||j                  z
        d
       t        j                  |       ?A t	        d| j                  z         y# t         $ r2}t	        d| j                  d|dt#        |             Y d}~d}~ww xY w)a.  
        This sweeps all entries stored in the cache, removing entries that
        are past the cache lifetime limit, or have not been used for longer
        than the cache discard time.  This should be called on plugin activation,
        and perhaps periodically (infrequently) after that.
        Fz!cache directory %s does not existNzcleaning cache directory %szremoving stale cache file :z: age z (past lifetime limit)z (past discard limit)z!error while checking cache entry : z$finished cleaning cache directory %s)timer   r
   existsprintlistdirr   statr   st_ctimeSECS_PER_DAYr   intunlinkr   st_atime	Exceptionstr)r   nowfr
   r   es         r   cleanzURLCache.clean;   s    iik77>>$))$-5		AB 	+dii78DII& 	]A]ww||DIIq1wwt}==B&}}(DEKaeajajlmorsvy}  zG  zG  tG  pH  I  J		$ <<2%}}|(CDsJ`d`i`iklnqrux|  yF  yF  sF  oG  H  I		$ !	]( 	4tyy@A  ]diiQRTWXYTZ[\\]s    B;G,A<G,,	H'5'H""H'c                 z    |j                  dd      }t        j                  j                  | j                  |      S )zW
        Constructs the full path of the file used to store a given cache key.
        r   _)replacer   r
   r   )r   keyfkeys      r   	cachefilezURLCache.cachefile_   s-     {{3$ww||DIIt,,r   c           	      x   t        j                          }	 | j                  |      }t        j                  |      }d}|r4| j                  dk7  r%|j
                  | j                  t        z  z   |k  rd}| j                  dk7  r%|j
                  | j                  t        z  z   |k  rd}|r1t        d| j                  d|       t        j                  |       y|S # t        $ rS}t        |d      du s|j                  t        j                  k7  rt        d| j                  d|d	|       Y d}~yd}~ww xY w)
ah  
        Checks for a fresh cache entry with a given key.
        If can_refresh is True, only cache entries that are within the
        refresh time will be considered.
        If can_refresh is False, cache entries that are older than the
        refresh time, but not past the lifetime limit or discard period,
        will also be considered.
        The intent is to allow older cache entries to be used if a network
        connection is not available or if the origin site is down.

        If successful, this returns the name of the file storing the cached data.
        Otherwise, it returns None.
        Fr   Tzremoving stale cache entry r   Nerrnozerror checking cache for r   )r   r.   r   r   r   r   r   r   r   r   r!   r#   hasattrr0   ENOENT)r   r,   can_refreshr%   r
   r   staler'   s           r   checkzURLCache.checkf   s     iik	>>#&D774=D Et||r1==DLL<$?@3F E}}"==DMML$@ACG EDIIsKL		$K 	q'"e+5<<0GdiiaPQ	s   CC C 	D9&A	D44D9c           	         	 t         j                  j                  | j                        s!t        j                  | j                  d       | j	                  |      }t        |d      }|j                  |       |j                          t        d| j                  d|       y# t        $ r(}t        d| j                  d|d|       Y d}~yd}~ww xY w)	z/
        Stores an entry in the cache.
        i  )modewbzstored cache data r   zexception storing cache data r   N)r   r
   r   makedirsr.   openwritecloser   r   r#   )r   r,   datar
   r&   r'   s         r   storezURLCache.store   s    	R77>>$)),DIIE2>>#&D T4 AGGDMGGItyy#>? 	RdiiaPQQ	Rs   B#B& &	C/CCc                    || j                  |d      }|it        |d      }|j                         }|j                           ||g| du r<t	        d| j
                  d|d       t        j                  |       y  |d g|  y y  ||g| du rt	        d| j
                  d|d       y | j                  ||       y )NFrbcache entry r    invalidated by callback)	r5   r:   readr<   r   r   r   r!   r>   )r   r=   urlr,   callbackargsr.   r&   s           r   
__fetch_cbzURLCache.__fetch_cb   s    <

3.I$D)vvx	D(4(E1TWXYIIi(%%	 2 $t$-TYYPSTU

3%r   c           	         t        d| j                  d|d|d       | j                  |d      }|it        |d      }|j	                         }|j                           ||g| duryt        d	| j                  d|d
       t        j                  |       t        j                         }|j                  || j                  ||||       y)a  
        Retrieve the specified URL, satisfying the request from the cache
        if possible, and refreshing the cache if necessary.

        The callback function may return False to indicate that the data
        passed to it is invalid.  Generally this should only happen if the
        data cannot be parsed and it is likely that a later attempt to fetch
        from the origin site will result in valid data.
        zfetching cache entry r   z []TNr@   FrA   rB   )r   r   r5   r:   rC   r<   r   r!   r@   Loaderget_url_URLCache__fetch_cb)	r   r,   rD   rE   rF   r.   r&   r=   lds	            r   fetchzURLCache.fetch   s     	499c3GHJJsD)	 Y%A668DGGI$t$E1CPQIIi YY[


3c8TBr   N)r   r   r   )T)
__name__
__module____qualname__r   r(   r.   r5   r>   rL   rN    r   r   r   r   %   s*    !*"BH-'TR(&$Cr   r   )
r   os.pathr   r0   r@   gi.repositoryr   r   objectr   rR   r   r   <module>rV      s.   6 
    	 jCv jCr   