
    ]j=                      B   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mZm	Z	m
Z
 ddlmZ ddlmZmZ ddlZddlmZ  ej&                  e      Zda ed      Zd	efd
Zd	efdZ G d d      Zdej8                  dededz  d	efdZdeeee	f   z  d	efdZ ded	dfdZ!y)zHelpers.    N)CallableHashableIterable)Path)TypeVarcast)FileLockFTreturnc                     	 ddl m}  d| z   }t        j                  j                  t        j                        }t        j                  t        j                  j                  d      d      j                         dd	 }d
j                  t        j                  dd D cg c]  }t        |       c}      }||||g}dj                  |      }|S # t        $ r d} Y w xY wc c}w )zGenerate 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-8FusedforsecurityN   .__)tldextract._versionr   ImportErrorospathbasenamesysprefixhashlibmd5encode	hexdigestjoinversion_infostr)r   tldextract_versionpython_env_namepython_binary_path_short_hashvpython_versionidentifier_partspkg_identifiers           :/root/env/lib/python3.12/site-packages/tldextract/cache.pyget_pkg_unique_identifierr,      s    / '0gg&&szz2O$+KK

'"E%ik"1%! XXs/?/?/DE!s1vEFN%	 YY/0N%   Fs   C C"CCc                     t         j                  j                  dd      } | | S t        j                  dd      }|-t        j                  dd      }|rt	        t        |d            }|t	        t        |dt                           S t	        t        t         j                  j                  t              d            S )zGet 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,   r   dirname__file__)	cache_dirxdg_cache_home	user_homes      r+   get_cache_dirr9   7   s     

148IYY/6NIIfd+	 i!:;N!!46O6QR
 	

 tBGGOOH-?@@    c            
          e Zd ZdZddedz  defdZdedeeeef   z  de	fd	Z
dedeeeef   z  d
e	ddfdZddZdedeeeef   z  defdZdedef   dedeeef   dee   def
dZdej&                  dedeez  dz  defdZy)	DiskCachez0Disk _cache that only works for jsonable values.r6   Nlock_timeoutc                     t        |      | _        t        j                  j	                  t        |      xs d      | _        || _        d| _        y)z.Construct a disk cache in the given directory. z.tldextract.jsonN)	boolenabledr   r   
expanduserr#   r6   r=   file_ext)selfr6   r=   s      r+   __init__zDiskCache.__init__S   s=    I++C	N,@bA( +r:   	namespacekeyr   c                    | j                   st        d      | j                  ||      }t        j                  j                  |      st        d|z   dz   t        |      z         	 t        |      5 }t        j                  |      cddd       S # 1 sw Y   yxY w# t        t        f$ r#}t        d|z   dz   t        |      z         |d}~ww xY w)z%Retrieve a value from the disk cache.zCache is disabledznamespace: z key: N)rA   KeyError_key_to_cachefile_pathr   r   isfilerepropenjsonloadOSError
ValueError)rD   rF   rG   cache_filepath
cache_fileexcs         r+   r2   zDiskCache.get\   s    ||.//44YDww~~n-=94x?$s)KLL	Vn% -yy,- - -$ 	V=94x?$s)KLRUU	Vs6   'B 2B	B BB B C,C

Cvaluec                 @   | j                   sy| j                  ||      }	 t        |       t        |d      5 }t	        j
                  ||       ddd       y# 1 sw Y   yxY w# t        $ r0}t        s t        j                  d||||       daY d}~yY d}~yd}~ww xY w)zSet a value in the disk cache.N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)
rA   rJ   	_make_dirrM   rN   dumprP   _DID_LOG_UNABLE_TO_CACHELOGwarning)rD   rF   rG   rU   rR   rS   ioes          r+   setzDiskCache.setj   s     ||44YD	0n%nc* -j		%,- - - 	0+G "	 ,0( ,	0s4   A$ AA$ A!A$ !A$ $	B-!BBc           
         t        j                  | j                        D ]p  \  }}}|D ]e  }|j                  | j                        s|j                  | j                  dz         s=	 t        j
                  t        t        ||                   g r y# t        $ r Y vt        $ r(}|j                  t        j                  k7  r Y d}~d}~ww xY w)zClear the disk cache..lockN)r   walkr6   endswithrC   unlinkr#   r   FileNotFoundErrorrP   errnoENOENT)rD   root_filesfilenamerT   s         r+   clearzDiskCache.clear   s     ggdnn5 	"ND!U! "$$T]]3x7H7HMMG+8"		#d4&:";<"	" - " " 994! 5"s   '(B	CC'C

Cc                     t        t        | j                  |            }t        |      }t        t        ||| j                  z               }|S )N)r#   r   r6   _make_cache_keyrC   )rD   rF   rG   namespace_path
hashed_key
cache_paths         r+   rJ   z DiskCache._key_to_cachefile_path   sC     T$..)<=$S)
nj4==.HIJ
r:   func.kwargshashed_argnamesc                 B   | j                   s |di |S |j                         D ci c]  \  }}||v s|| }}}| j                  ||      }|dz   }		 t        |       t        |	| j                        5  	 t        t        | j                  ||            }|cddd       S c c}}w # t        $ r3}
t
        st        j                  d||||
       da |di |cY d}
~
S d}
~
ww xY w# t        $ r  |di |}| j                  |||       Y ww xY w# 1 sw Y   yxY w)	!Get a url but cache the response.ra   rX   TNtimeout)rF   rG   )rF   rG   rU    )rA   itemsrJ   rY   rP   r[   r\   r]   r	   r=   r   r
   r2   rI   r_   )rD   rr   rF   rs   rt   kr'   key_argsrR   	lock_pathr^   results               r+   run_and_cachezDiskCache.run_and_cache   s6    ||>&>!%+\\^LTQqO7KAqDLL44YI"W,		"n%$ i):):; 	JaI8!LM
 	 	- M
  	"+G "	 ,0(>&>!	"(  J9(&IJ	 	sX   B%B%B+ 7D9!C*D+	C'4(C"C'"C'*%DDDDDsessionurlrx   c                 >    | j                  t        d|||ddg      S )rv   urls)r   r   rx   r   )rr   rF   rs   rt   )r   
_fetch_url)rD   r   r   rx   s       r+   cached_fetch_urlzDiskCache.cached_fetch_url   s1     !!&swG"G	 " 
 	
r:   )   )r   N)__name__
__module____qualname____doc__r#   intrE   dictr   objectr2   r_   rl   rJ   r   r
   r   r   requestsSessionfloatr   ry   r:   r+   r<   r<   P   s+   :+#* +C +VS VsT#x--@'@ VV V00#&c8m)<#<0EK0	08""#&c8m)<#<	(sAv( ( S(]#	(
 "#( 
(T	
''	
.1	
<ACK$<N	
		
r:   r<   r   r   rx   c                     | j                  ||      }|j                          |j                  }t        |t              st	        |d      }|S )Nrw   r   )r2   raise_for_statustext
isinstancer#   )r   r   rx   responser   s        r+   r   r      sD    {{3{0H==DdC 4!Kr:   inputsc                     t        |       }t        j                  |j                  d      d      j	                         S )Nutf8Fr   )rL   r   r   r   r    )r   rG   s     r+   rn   rn      s.    
v,C;;szz&)5AKKMMr:   rk   c                 L   t         j                  j                  t         j                  j                  |             s4	 t        j                  t         j                  j                  |              yy# t
        $ r(}|j                  t        j                  k7  r Y d}~yd}~ww xY w)z-Make a directory if it doesn't already exist.N)r   r   existsr4   makedirsrP   rf   EEXIST)rk   rT   s     r+   rY   rY      sl    77>>"''//(34	KK12 5  	yyELL( )	s   2A2 2	B#;BB#)"r   rf   r   rN   loggingr   r   collections.abcr   r   r   pathlibr   typingr   r   r   filelockr	   	getLoggerr   r\   r[   r
   r#   r,   r9   r<   r   r   r   r   rn   rY   ry   r:   r+   <module>r      s         	 
 8 8 
  g!  CL3 :As A2D
 D
N(( s S4Z C NC$sH}"55 N# N
  r:   