a
    xdJ"                     @   s  d Z ddlZddlZ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dl	m
Z
mZmZmZmZmZmZ ddlmZ ddlZeeZdaedZedd	d
ZedddZG dd dZejeee edddZeeeeef f edddZ eddddZ!dS )zHelpers     N)md5)CallableDictHashableIterableOptionalTypeVarUnion)FileLockFTreturnc                  C   s   zddl m}  W n ty&   d} Y n0 d|  }tjtj}t	tj
d dd }dd	d
 tjdd D }||||g}d|}|S )z
    Generate an identifier unique to the python version, tldextract version, and python instance

    This will prevent interference between virtualenvs and issues that might arise when installing
    a new version of tldextract
    r   )versiondevztldextract-utf-8N   .c                 S   s   g | ]}t |qS  )str).0vr   r   H/var/www/html/Ranjet/env/lib/python3.9/site-packages/tldextract/cache.py
<listcomp>)       z-get_pkg_unique_identifier.<locals>.<listcomp>__)Ztldextract._versionr   ImportErrorospathbasenamesysprefixhashlibr   encode	hexdigestjoinversion_info)r   Ztldextract_versionZpython_env_nameZpython_binary_path_short_hashpython_versionZidentifier_partsZpkg_identifierr   r   r   get_pkg_unique_identifier   s"    

r(   c                  C   s|   t jdd} | dur| S t dd}|du rLt dd}|rLt j|d}|durft j|dt S t jt jtdS )z
    Get a cache dir that we have permission to write to

    Try to follow the XDG standard, but if that doesn't work fallback to the package directory
    http://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
    ZTLDEXTRACT_CACHENXDG_CACHE_HOMEHOMEz.cachezpython-tldextractz.suffix_cache/)	r   environgetgetenvr   r%   r(   dirname__file__)	cache_dirZxdg_cache_homeZ	user_homer   r   r   get_cache_dir5   s    r1   c                   @   s   e Zd ZdZdee edddZeeee	ee
f f edddZeeee	ee
f f ed	d
ddZd	dddZeeee	ee
f f edddZedef ee	ee
f ee edddZejeeeed	f edddZd	S )	DiskCachez/Disk _cache that only works for jsonable values   )r0   lock_timeoutc                 C   s0   t || _tjt|pd| _|| _d| _d S )N z.tldextract.json)	boolenabledr   r   
expanduserr   r0   r4   file_ext)selfr0   r4   r   r   r   __init__R   s    
zDiskCache.__init__)	namespacekeyr   c              
   C   s   | j std| ||}tj|s>td| d t| z8t|}t	|W  d   W S 1 sj0    Y  W nP t
tfy } z4td|| td| d t| dW Y d}~n
d}~0 0 dS )z$Retrieve a value from the disk cachezCache is disabledznamespace: z key: Nz#error reading TLD cache file %s: %s)r7   KeyError_key_to_cachefile_pathr   r   isfilerepropenjsonloadOSError
ValueErrorLOGerror)r:   r<   r=   cache_filepath
cache_fileexcr   r   r   r,   Z   s    
.zDiskCache.getN)r<   r=   valuer   c              
   C   s   | j s
dS | ||}zBt| t|d}t|| W d   n1 sL0    Y  W n> ty } z&tst	d|||| daW Y d}~n
d}~0 0 dS )zSet a value in the disk cacheNwunable to cache %s.%s in %s. This could refresh the Public Suffix List over HTTP every app startup. Construct your `TLDExtract` with a writable `cache_dir` or set `cache_dir=None` to silence this warning. %sT)
r7   r?   	_make_dirrB   rC   dumprE   _DID_LOG_UNABLE_TO_CACHErG   warning)r:   r<   r=   rL   rI   rJ   ioer   r   r   setj   s"    .zDiskCache.setr   c                 C   s   t | jD ]\}}}|D ]|}|| js:|| jd rzt t j|| W q tyd   Y q t	y } z|j
t
jkr W Y d}~qd}~0 0 qqdS )zClear the disk cache.lockN)r   walkr0   endswithr9   unlinkr   r%   FileNotFoundErrorrE   errnoENOENT)r:   root_filesfilenamerK   r   r   r   clear   s    zDiskCache.clearc                 C   s0   t j| j|}t|}t j||| j }|S )N)r   r   r%   r0   _make_cache_keyr9   )r:   r<   r=   namespace_pathZ
hashed_key
cache_pathr   r   r   r?      s    z DiskCache._key_to_cachefile_path.)funcr<   kwargshashed_argnamesr   c           
   
      s  | j s|f i |S  fdd| D }| ||}|d }zt| W nL ty } z4tsttd|||| da|f i |W  Y d}~S d}~0 0 t|| j	dX z| j
||d}	W n0 ty   |f i |}	| j|||	d	 Y n0 |	W  d   S 1 s0    Y  dS )
 Get a url but cache the responsec                    s   i | ]\}}| v r||qS r   r   )r   kr   rf   r   r   
<dictcomp>   r   z+DiskCache.run_and_cache.<locals>.<dictcomp>rU   rN   TNtimeout)r<   r=   )r<   r=   rL   )r7   itemsr?   rO   rE   rQ   rG   rR   r
   r4   r,   r>   rT   )
r:   rd   r<   re   rf   Zkey_argsrI   Z	lock_pathrS   resultr   ri   r   run_and_cache   s2    $zDiskCache.run_and_cachesessionurlrl   r   c                 C   s   | j td|||ddgdS )rg   urls)rq   rr   rl   rr   )rd   r<   re   rf   )ro   
_fetch_url)r:   rq   rr   rl   r   r   r   cached_fetch_url   s    
zDiskCache.cached_fetch_url)r3   )__name__
__module____qualname____doc__r   r   intr;   r	   r   r   r   r,   rT   r`   r?   r   r   ro   requestsSessionfloatru   r   r   r   r   r2   O   s&   "

/r2   rp   c                 C   s4   | j ||d}|  |j}t|ts0t|d}|S )Nrk   r   )r,   raise_for_statustext
isinstancer   )rq   rr   rl   responser   r   r   r   rt      s    

rt   )inputsr   c                 C   s   t | }t|d S )Nutf8)rA   r   r#   r$   )r   r=   r   r   r   ra      s    ra   )r_   r   c              
   C   sb   t jt j| s^zt t j|  W n2 ty\ } z|jtjkrH W Y d}~n
d}~0 0 dS )z,Make a directory if it doesn't already existN)r   r   existsr.   makedirsrE   rZ   EEXIST)r_   rK   r   r   r   rO      s    rO   )"ry   rZ   r"   rC   loggingr   os.pathr    r   typingr   r   r   r   r   r   r	   Zfilelockr
   r{   	getLoggerrv   rG   rQ   r   r   r(   r1   r2   r|   rz   rt   ra   rO   r   r   r   r   <module>   s*   $
  