
    %bJ                          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
  ej        e          ZdadefdZdefdZ G d	 d
          Zd Zd Zd ZdS )zHelpers     N)md5)FileLockFreturnc                     	 ddl m}  n# t          $ r d} Y nw xY wd| z   }t          j                            t          j                  }t          j	        t          j        
                    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                 ,    g | ]}t          |          S  )str).0vs     0lib/python3.11/site-packages/tldextract/cache.py
<listcomp>z-get_pkg_unique_identifier.<locals>.<listcomp>%   s    EEE!s1vvEEE    __)tldextract._versionr   ImportErrorospathbasenamesysprefixhashlibr   encode	hexdigestjoinversion_info)r   tldextract_versionpython_env_namepython_binary_path_short_hashpython_versionidentifier_partspkg_identifiers          r   get_pkg_unique_identifierr(      s    ///////    '0g&&sz22O$+K
0A0A'0J0J$K$K$U$U$W$W
%! XXEEs/?/DEEEFFN%	 YY/00Ns   	 c                     t           j                            dd          } | | S t          j        dd          }|7t          j        dd          }|r t           j                            |d          }|-t           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
    TLDEXTRACT_CACHENXDG_CACHE_HOMEHOMEz.cachezpython-tldextractz.suffix_cache/)	r   environgetgetenvr   r    r(   dirname__file__)	cache_dirxdg_cache_home	user_homes      r   get_cache_dirr5   1   s     
1488IY/66NIfd++	 	?W\\)X>>N!w||/1J1L1L
 
 	

 7<<113CDDDr   c                   >    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
S )	DiskCachez/Disk _cache that only works for jsonable values   c                     t          |          | _        t          j                            t          |          pd          | _        || _        d| _        d S )N z.tldextract.json)	boolenabledr   r   
expanduserr   r2   lock_timeoutfile_ext)selfr2   r>   s      r   __init__zDiskCache.__init__N   sF    I++C	NN,@bAA( +r   c                    | j         st          d          |                     ||          }t          j                            |          s%t          d|z   dz   t          |          z             	 t          |          5 }t          j	        |          cddd           S # 1 swxY w Y   dS # t          t          f$ rG}t                              d||           t          d|z   dz   t          |          z             dd}~ww xY w)z$Retrieve a value from the disk cachezCache is disabledznamespace: z key: Nz#error reading TLD cache file %s: %s)r<   KeyError_key_to_cachefile_pathr   r   isfilerepropenjsonloadOSError
ValueErrorLOGerror)r@   	namespacekeycache_filepath
cache_fileexcs         r   r.   zDiskCache.getV   sQ   | 	0.///44YDDw~~n-- 	M=94x?$s))KLLL	Wn%% -y,,- - - - - - - - - - - - - - - - - -$ 	W 	W 	WII;^SQQQ=94x?$s))KLLRVV	Ws=   2B/ B"B/ "B&&B/ )B&*B/ /D ADDc                 \   | j         sdS |                     ||          }	 t          |           t          |d          5 }t	          j        ||           ddd           n# 1 swxY w Y   n># t          $ r1}t          s t          	                    d||||           daY d}~nd}~ww xY wdS )zSet a value in the disk cacheFwNunable 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=False` to silence this warning. %sT)
r<   rD   	_make_dirrG   rH   dumprJ   _DID_LOG_UNABLE_TO_CACHErL   warning)r@   rN   rO   valuerP   rQ   ioes          r   setzDiskCache.setf   s   | 	544YDD	0n%%%nc** -j	%,,,- - - - - - - - - - - - - - - 	0 	0 	0+ 0L
 "   ,0(	0" ts;   A.  A"A. "A&&A. )A&*A. .
B)8'B$$B)c                    t          j        | j                  D ]\  }}}|D ]}|                    | j                  s|                    | j        dz             rl	 t          j        t           j                            ||                     m# t          $ r Y yt          $ r }|j
        t          j        k    r Y d}~d}~ww xY wdS )zClear the disk cache.lockN)r   walkr2   endswithr?   unlinkr   r    FileNotFoundErrorrJ   errnoENOENT)r@   root_filesfilenamerR   s         r   clearzDiskCache.clear   s    gdn55 	" 	"ND!U! " "$$T]33 "x7H7HMG+8 8 ""	"',,tX">">????,   " " " " 944! 54444"""	" 	"s   2B
C	C$B??Cc                     t           j                            | j        |          }t	          |          }t           j                            ||| j        z             }|S )N)r   r   r    r2   _make_cache_keyr?   )r@   rN   rO   namespace_path
hashed_key
cache_paths         r   rD   z DiskCache._key_to_cachefile_path   sH    dni@@$S))
W\\.*t}2LMM
r   c                 <   | j         s |d
i |S fd|                                D             }|                     ||          }|dz   }	 t          |           nF# t          $ r9}t
          s t                              d||||           da |d
i |cY d}~S d}~ww xY wt          || j	                  5  	 | 
                    ||          }	n0# t          $ r#  |d
i |}	|                     d||		           Y nw xY w|	cddd           S # 1 swxY w Y   dS ) Get a url but cache the responsec                 $    i | ]\  }}|v 	||S r   r   )r   kr   hashed_argnamess      r   
<dictcomp>z+DiskCache.run_and_cache.<locals>.<dictcomp>   s)    LLLTQqO7K7KAq7K7K7Kr   r^   rU   TNtimeout)rN   rO   urls)rN   rO   rZ   r   )r<   itemsrD   rV   rJ   rX   rL   rY   r   r>   r.   rC   r\   )
r@   funcrN   kwargsrs   key_argsrP   	lock_pathr[   results
       `     r   run_and_cachezDiskCache.run_and_cache   s   | 	"4>>&>>!LLLLV\\^^LLL44YII"W,		"n%%%% 	" 	" 	"+ 0L
 "   ,0(4>>&>>!!!!!!#	"* i):;;; 	 	GI8DD G G G6xvFFFFFG 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sT   A 
B '.BB B 9D;CD*D =D?D  DDDc                 H    |                      t          d|||ddg          S )rp   rw   )sessionurlrv   r   )ry   rN   rz   rs   )r~   
_fetch_url)r@   r   r   rv   s       r   cached_fetch_urlzDiskCache.cached_fetch_url   s7    !!&swGG"G	 " 
 
 	
r   N)r8   )__name__
__module____qualname____doc__rA   r.   r\   ri   rD   r~   r   r   r   r   r7   r7   K   s        99+ + + +W W W   <" " ""  & & &P
 
 
 
 
r   r7   c                     |                      ||          }|                                 |j        }t          |t                    st	          |d          }|S )Nru   r	   )r.   raise_for_statustext
isinstancer   )r   r   rv   responser   s        r   r   r      sV    {{3{00H=DdC   "4!!Kr   c                     t          |           }	 t          |                                          }nD# t          $ r7 t          |                    d                                                    }Y nw xY w|S )Nutf8)rF   r   r   	TypeErrorr   )inputsrO   s     r   rk   rk      st    
v,,C2#hh  "" 2 2 2#**V$$%%//112Js   !3 >A43A4c                 B   t           j                            t           j                            |                     sb	 t          j        t           j                            |                      dS # t
          $ r!}|j        t          j        k    r Y d}~dS d}~ww xY wdS )z,Make a directory if it doesn't already existN)r   r   existsr0   makedirsrJ   rc   EEXIST)rh   rR   s     r   rV   rV      s    7>>"'//(3344 	K1122222 	 	 	yEL(( )(((((	 s   1A1 1
B;BB)r   rc   r   rH   loggingr   os.pathr   r   filelockr   	getLoggerr   rL   rX   r   r(   r5   r7   r   rk   rV   r   r   r   <module>r      s(        				  



            g!!  3    >Es E E E E4A
 A
 A
 A
 A
 A
 A
 A
H	 	 	      r   