
    !}ga                        d Z ddlm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dZddZ G d d          ZddZddZddZdS ) zHelpers.    )annotationsN)CallableHashableIterable)Path)TypeVarcast)FileLockFTreturnstrc                    	 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
                    d t          j        dd         D                       }||||g}d                    |          }|S )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   .c                ,    g | ]}t          |          S  )r   ).0vs     P/var/www/py-google-trends/myenv/lib/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hashlibmd5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_identifierr2      s    ///////    '0g&&sz22O$+K
'""E% % %ikk"1"%! XXEEs/?/DEEEFFN%	 YY/00Ns   	 c                    t           j                            dd          } | | S t          j        dd          }|4t          j        dd          }|rt	          t          |d                    }|*t	          t          |dt                                          S t	          t          t           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   r2   r"   dirname__file__)	cache_dirxdg_cache_home	user_homes      r   get_cache_dirr?   9   s     
1488IY/66NIfd++	 	< i!:!:;;N!!46O6Q6QRR
 
 	

 tBGOOH--??@@@r   c                  L    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"S )+	DiskCachez0Disk _cache that only works for jsonable values.   r<   
str | Nonelock_timeoutintc                    t          |          | _        t          j                            t          |          pd          | _        || _        d| _        dS )z.Construct a disk cache in the given directory. z.tldextract.jsonN)	boolenabledr!   r"   
expanduserr   r<   rD   file_ext)selfr<   rD   s      r   __init__zDiskCache.__init__U   sF    I++C	NN,@bAA( +r   	namespacer   keystr | dict[str, Hashable]r   objectc                   | 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$ 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)rI   KeyError_key_to_cachefile_pathr!   r"   isfilerepropenjsonloadOSError
ValueError)rL   rN   rO   cache_filepath
cache_fileexcs         r   r8   zDiskCache.get^   s:   | 	0.///44YDDw~~n-- 	M=94x?$s))KLLL	Vn%% -y,,- - - - - - - - - - - - - - - - - -$ 	V 	V 	V=94x?$s))KLLRUU	Vs<   2B/ B"B/ "B&&B/ )B&*B/ /C+ &C&&C+valueNonec                j   | j         sdS |                     ||          }	 t          |           t          |d          5 }t	          j        ||           ddd           dS # 1 swxY w Y   dS # t          $ r8}t          s&t          	                    d||||           daY d}~dS Y d}~dS 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)
rI   rT   	_make_dirrW   rX   dumprZ   _DID_LOG_UNABLE_TO_CACHELOGwarning)rL   rN   rO   r_   r\   r]   ioes          r   setzDiskCache.setl   sG    | 	F44YDD	0n%%%nc** -j	%,,,- - - - - - - - - - - - - - - - - - 	0 	0 	0+ 0G "	 	 	 ,0(((((((0 0 0 0 0 0	0s;   A0  A#A0 #A''A0 *A'+A0 0
B2:'B--B2c           
        t          j        | j                  D ]\  }}}|D ]}|                    | j                  s|                    | j        dz             ri	 t          j        t          t          ||                               j# t          $ r Y vt          $ r }|j
        t          j        k    r Y d}~d}~ww xY wdS )zClear the disk cache..lockN)r!   walkr<   endswithrK   unlinkr   r   FileNotFoundErrorrZ   errnoENOENT)rL   root_filesfilenamer^   s         r   clearzDiskCache.clear   s    gdn55 	" 	"ND!U! " "$$T]33 "x7H7HMG+8 8 ""	#d4&:&:";";<<<<,   " " " " 944! 54444"""	" 	"s   /B
C	C!B<<Cc                    t          t          | j        |                    }t          |          }t          t          ||| j        z                       }|S )N)r   r   r<   _make_cache_keyrK   )rL   rN   rO   namespace_path
hashed_key
cache_paths         r   rT   z DiskCache._key_to_cachefile_path   sN     T$.)<<==$S))
nj4=.HIIJJ
r   funcCallable[..., T]kwargsdict[str, Hashable]hashed_argnamesIterable[str]r   c                b   | 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  	 t          t          |                     ||                    }	n0# t          $ r#  |d	i |}	|                     |||	           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 response.c                $    i | ]\  }}|v 	||S r   r   )r   kr   r   s      r   
<dictcomp>z+DiskCache.run_and_cache.<locals>.<dictcomp>   s)    LLLTQqO7K7KAq7K7K7Kr   rl   rc   TNtimeout)rN   rO   )rN   rO   r_   r   )rI   itemsrT   rd   rZ   rf   rg   rh   r
   rD   r	   r   r8   rS   rj   )
rL   r}   rN   r   r   key_argsr\   	lock_pathri   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G "	 	 	 ,0(4>>&>>!!!!!!	"" i):;;; 	 	JaI8!L!LMM J J J9(&IIIIIJ 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sT   A 
B '.BB B 9D$;*C&%D$&*DD$DD$$D(+D(sessionrequests.Sessionurlr   float | int | Nonec                H    |                      t          d|||ddg          S )r   urls)r   r   r   r   )r}   rN   r   r   )r   
_fetch_url)rL   r   r   r   s       r   cached_fetch_urlzDiskCache.cached_fetch_url   s9     !!&swGG"G	 " 
 
 	
r   N)rB   )r<   rC   rD   rE   )rN   r   rO   rP   r   rQ   )rN   r   rO   rP   r_   rQ   r   r`   )r   r`   )rN   r   rO   rP   r   r   )
r}   r~   rN   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__rM   r8   rj   rw   rT   r   r   r   r   r   rA   rA   R   s        ::+ + + + +V V V V0 0 0 08" " " ""   ( ( ( (T	
 	
 	
 	
 	
 	
r   rA   r   r   r   r   
int | Nonec                    |                      ||          }|                                 |j        }t          |t                    st	          |d          }|S )Nr   r   )r8   raise_for_statustext
isinstancer   )r   r   r   responser   s        r   r   r      sV    {{3{00H=DdC   "4!!Kr   inputsrP   c                    t          |           }t          j        |                    d          d                                          S )Nutf8Fr   )rV   r&   r'   r(   r)   )r   rO   s     r   ry   ry      s:    
v,,C;szz&))5AAAKKMMMr   rv   r`   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 exist.N)r!   r"   existsr:   makedirsrZ   rq   EEXIST)rv   r^   s     r   rd   rd      s    7>>"'//(3344 	K1122222 	 	 	yEL(( )(((((	 s   1A1 1
B;BB)r   r   )r   r   r   r   r   r   r   r   )r   rP   r   r   )rv   r   r   r`   ) r   
__future__r   rq   r&   rX   loggingr!   r$   collections.abcr   r   r   pathlibr   typingr   r	   requestsfilelockr
   	getLoggerr   rg   rf   r   r2   r?   rA   r   ry   rd   r   r   r   <module>r      s    " " " " " "     				 



 8 8 8 8 8 8 8 8 8 8             
       g!!  GCLL   :A A A A2D
 D
 D
 D
 D
 D
 D
 D
N   N N N N
     r   