
    g	                     T    d dl Z d dlZd dlZd dlmZ  G d de      Z G d de      Zy)    N)md5c                       e Zd ZdZy)_FileCacheErrorz1Base exception class for FileCache related errorsN)__name__
__module____qualname____doc__     (/opt/Tautulli/lib/twitter/_file_cache.pyr   r   	   s    ;r   r   c                   N    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zy)
_FileCache   Nc                 &    | j                  |       y N)_InitializeRootDirectory)selfroot_directorys     r   __init__z_FileCache.__init__   s    %%n5r   c                     | j                  |      }t        j                  j                  |      r%t	        |      5 }|j                         cd d d        S y # 1 sw Y   y xY wr   )_GetPathospathexistsopenread)r   keyr   fs       r   Getz_FileCache.Get   sQ    }}S!77>>$d  qvvx       s   AA c                    | j                  |      }t        j                  j                  |      }t        j                  j	                  |      st        j
                  |       t        j                  j                  |      st        d|z        t        j                         \  }}t        j                  |d      }|j                  |       |j                          |j                  | j                        st        |d| j                        t        j                  j	                  |      rt        j                  |       t        j                   ||       y )Nz %s exists but is not a directoryw does not appear to live under )r   r   r   dirnamer   makedirsisdirr   tempfilemkstempfdopenwriteclose
startswith_root_directoryremoverename)r   r   datar   	directorytemp_fd	temp_pathtemp_fps           r   Setz_FileCache.Set   s    }}S!GGOOD)	ww~~i(KK	"ww}}Y'!"Dy"PQQ%--/))GS)dt334!#')=)=#? @ @77>>$IIdO
		)T"r   c                     | j                  |      }|j                  | j                        st        |d| j                        t        j
                  j                  |      rt	        j                  |       y y )Nr"   )r   r+   r,   r   r   r   r   r-   r   r   r   s      r   Removez_FileCache.Remove-   sa    }}S!t334!#')=)=#? @ @77>>$IIdO  r   c                     | j                  |      }t        j                  j                  |      rt        j                  j	                  |      S y r   )r   r   r   r   getmtimer6   s      r   GetCachedTimez_FileCache.GetCachedTime5   s8    }}S!77>>$77##D))r   c                     	 t        j                  d      xsF t        j                  d      xs/ t        j                  d      xs t        j                         xs dS # t        t        t
        f$ r Y yw xY w)z9Attempt to find the username in a cross-platform fashion.USERLOGNAMEUSERNAMEnobody)r   getenvgetloginAttributeErrorIOErrorOSError)r   s    r   _GetUsernamez_FileCache._GetUsername<   so    	99V$ 99Y'99Z( ;;= 	
 1 		s   AA A65A6c                     | j                         }d|z   }t        j                  j                  t	        j
                         |      S )Nzpython.cache_)rE   r   r   joinr&   
gettempdir)r   usernamecache_directorys      r   _GetTmpCachePathz_FileCache._GetTmpCachePathG   s8    $$&)H4ww||H//1?CCr   c                 V   |s| j                         }t        j                  j                  |      }	 t        j                  |       || _	        y # t
        $ rO}|j                  t        j                  k(  r t        j                  j                  |      rn Y d }~|| _	        y d }~ww xY wr   )
rK   r   r   abspathmkdirrD   errnoEEXISTr%   r,   )r   r   es      r   r   z#_FileCache._InitializeRootDirectoryL   s    !224N8	HH^$  .  	ww%,,&277==+H    .	s   A 	B(>B##B(c                 ,   	 t        |j                  d            j                         }t
        j                  j                  | j                  | j                  |      |      S # t        $ r& t        j                  |      j                         }Y hw xY w)Nzutf-8)
r   encode	hexdigest	TypeErrornewr   r   rG   r,   
_GetPrefix)r   r   
hashed_keys      r   r   z_FileCache._GetPath[   sx    	2SZZ01;;=J ww||D00 OOJ7&( 	(  	2//1J	2s   (A$ $,BBc                 v    t         j                  j                  j                  |dt        j
                         S )Nr   )r   r   seprG   r   DEPTH)r   rX   s     r   rW   z_FileCache._GetPrefixe   s(    ww{{
1Z-=-= >??r   r   )r   r   r   r[   r   r   r4   r7   r:   rE   rK   r   r   rW   r
   r   r   r   r      s;    E6#$	D
.(@r   r   )	rO   r   r&   hashlibr   	Exceptionr   objectr   r
   r   r   <module>r_      s-     	  <i <Y@ Y@r   