o
    š¨Êh	  ã                   @   sH   d dl Z d dlZd dlZd dlmZ G dd„ deƒZG dd„ deƒZdS )é    N)Úmd5c                   @   s   e Zd ZdZdS )Ú_FileCacheErrorz1Base exception class for FileCache related errorsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úE/var/www/html/env/lib/python3.10/site-packages/twitter/_file_cache.pyr   	   s    r   c                   @   sb   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )Ú
_FileCacheé   Nc                 C   s   |   |¡ d S ©N)Ú_InitializeRootDirectory)ÚselfÚroot_directoryr   r   r	   Ú__init__   s   z_FileCache.__init__c                 C   sN   |   |¡}tj |¡r%t|ƒ�}| ¡ W  d   ƒ S 1 sw   Y  d S d S r   )Ú_GetPathÚosÚpathÚexistsÚopenÚread)r   Úkeyr   Úfr   r   r	   ÚGet   s   

$ÿz_FileCache.Getc                 C   s²   |   |¡}tj |¡}tj |¡st |¡ tj |¡s"td| ƒ‚t 	¡ \}}t 
|d¡}| |¡ | ¡  | | j¡sFtd|| jf ƒ‚tj |¡rQt |¡ t ||¡ d S )Nz %s exists but is not a directoryÚwú#%s does not appear to live under %s)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_fpr   r   r	   ÚSet   s"   


ÿ
z_FileCache.Setc                 C   sF   |   |¡}| | j¡std|| jf ƒ‚tj |¡r!t |¡ d S d S )Nr   )r   r$   r%   r   r   r   r   r&   ©r   r   r   r   r   r	   ÚRemove-   s   
ÿÿz_FileCache.Removec                 C   s&   |   |¡}tj |¡rtj |¡S d S r   )r   r   r   r   Úgetmtimer.   r   r   r	   ÚGetCachedTime5   s   
z_FileCache.GetCachedTimec              
   C   sH   zt  d¡pt  d¡pt  d¡pt  ¡ pdW S  tttfy#   Y dS w )z9Attempt to find the username in a cross-platform fashion.ÚUSERÚLOGNAMEÚUSERNAMEÚnobody)r   ÚgetenvÚgetloginÚAttributeErrorÚIOErrorÚOSError)r   r   r   r	   Ú_GetUsername<   s   
ÿþýüÿz_FileCache._GetUsernamec                 C   s"   |   ¡ }d| }tj t ¡ |¡S )Nzpython.cache_)r;   r   r   Újoinr   Ú
gettempdir)r   ÚusernameÚcache_directoryr   r   r	   Ú_GetTmpCachePathG   s   z_FileCache._GetTmpCachePathc              
   C   sr   |s|   ¡ }tj |¡}zt |¡ W n  ty3 } z|jtjkr(tj |¡r(n‚ W Y d }~nd }~ww || _	d S r   )
r@   r   r   ÚabspathÚmkdirr:   ÚerrnoÚEEXISTr   r%   )r   r   Úer   r   r	   r   L   s   ý€ý
z#_FileCache._InitializeRootDirectoryc                 C   sP   zt | d¡ƒ ¡ }W n ty   t  |¡ ¡ }Y nw tj | j|  	|¡|¡S )Nzutf-8)
r   ÚencodeÚ	hexdigestÚ	TypeErrorÚnewr   r   r<   r%   Ú
_GetPrefix)r   r   Ú
hashed_keyr   r   r	   r   [   s   ÿ
þz_FileCache._GetPathc                 C   s   t jj |dtj… ¡S )Nr   )r   r   Úsepr<   r
   ÚDEPTH)r   rK   r   r   r	   rJ   e   s   z_FileCache._GetPrefixr   )r   r   r   rM   r   r   r-   r/   r1   r;   r@   r   r   rJ   r   r   r   r	   r
      s    

r
   )	rC   r   r   Úhashlibr   Ú	Exceptionr   Úobjectr
   r   r   r   r	   Ú<module>   s   