
    V/a                         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 Zd Z G d d	      Zd
 Zd Zd Zy)zHelpers     N)md5)FileLockFc                     	 ddl m}  d| z   }t        j                  j                  t        j                        }t        j                  t        j                  j                  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 )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   .__)tldextract._versionr   ImportErrorospathbasenamesysprefixhashlibr   encode	hexdigestjoinversion_infostr)r   tldextract_versionpython_env_namepython_binary_path_short_hashvpython_versionidentifier_partspkg_identifiers           2/usr/lib/python3/dist-packages/tldextract/cache.pyget_pkg_unique_identifierr"      s    / '0gg&&szz2O$+KK

0A0A'0J$K$U$U$W
%! 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      }|8t        j                  dd      }|r t         j                  j                  |d      }|)t         j                  j                  |dt                     S t         j                  j                  t         j                  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_dirr/   1   s     

148IYY/6NIIfd+	WW\\)X>N!ww||/1J1L
 	

 77<<13CDD    c                   <    e Zd ZdZd
dZd Zd Zd Zd Zd Z	d Z
y	)	DiskCachez/Disk _cache that only works for jsonable valuesc                     t        |      | _        t        j                  j	                  t        |      xs d      | _        || _        d| _        y )N z.tldextract.json)	boolenabledr   r   
expanduserr   r,   lock_timeoutfile_ext)selfr,   r8   s      r!   __init__zDiskCache.__init__N   s=    I++C	N,@bA( +r0   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$ r9}t        j                  d||       t        d|z   dz   t        |      z         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)r6   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   s    ||.//44YDww~~n-=94x?$s)KLL	n% -yy,- - -$ 	II;^SQ	)H4tCy@ 	s6   'B 2B	B BB B C%,4C  C%c                 6   | j                   sy| j                  ||      }	 t        |       t        |d      5 }t	        j
                  ||       ddd       y# 1 sw Y   yxY w# t        $ r+}t        st        j                  d||||       daY d}~yd}~ww xY w)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)
r6   r>   	_make_dirrA   rB   dumprD   _DID_LOG_UNABLE_TO_CACHErF   warning)r:   rH   rI   valuerJ   rK   ioes          r!   setzDiskCache.setg   s    ||44YD	0n%nc* -j		%,-& '-& #  	0+L
 " ,0(#	0s4   A$ AA$ A!A$ !A$ $	B-!BBc                    t        j                  | j                        D ]{  \  }}}|D ]p  }|j                  | j                        s|j                  | j                  dz         s=	 t        j
                  t         j                  j                  ||             r } y# t        $ r Y t        $ r(}|j                  t        j                  k7  r Y d}~d}~ww xY w)zClear the disk cache.lockN)r   walkr,   endswithr9   unlinkr   r   FileNotFoundErrorrD   errnoENOENT)r:   root_filesfilenamerL   s         r!   clearzDiskCache.clear   s     ggdnn5 	"ND!U! "$$T]]3x7H7HMMG+8"		"'',,tX">?"	" - " " 994! 5"s   '3B	C*C2CCc                     t         j                  j                  | j                  |      }t	        |      }t         j                  j                  ||| j
                  z         }|S )N)r   r   r   r,   _make_cache_keyr9   )r:   rH   rI   namespace_path
hashed_key
cache_paths         r!   r>   z DiskCache._key_to_cachefile_path   sG    dnni@$S)
WW\\.*t}}2LM
r0   c                 &   | j                   s |d	i |S |j                         D ci c]  \  }}||v s|| }}}| j                  ||      }|dz   }		 t        |       t        |	| j                        5  	 | 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                  d||       Y ww xY w# 1 sw Y   yxY w)
 Get a url but cache the responserX   rO   TNtimeout)rH   rI   urls)rH   rI   rT    )r6   itemsr>   rP   rD   rR   rF   rS   r   r8   r(   r=   rV   )r:   funcrH   kwargshashed_argnameskr   key_argsrJ   	lock_pathrU   results               r!   run_and_cachezDiskCache.run_and_cache   s-   ||>&>!%+\\^LTQqO7KAqDLL44YI"W,		"n%( i):):; 	GI8D
 	 	1 M
  	"+L
 " ,0(>&>!#	",  G6xvFG	 	sX   BBB 7D9CD	C&(CCC%DDDDDc                 >    | j                  t        d|||ddg      S )rj   rm   )sessionurlrl   rz   )rp   rH   rq   rr   )rw   
_fetch_url)r:   ry   rz   rl   s       r!   cached_fetch_urlzDiskCache.cached_fetch_url   s/    !!&swG"G	 " 
 	
r0   N)   )__name__
__module____qualname____doc__r;   r(   rV   rc   r>   rw   r|   rn   r0   r!   r2   r2   K   s)    9+":""$L
r0   r2   c                     | j                  ||      }|j                          |j                  }t        |t              st	        |d      }|S )Nrk   r   )r(   raise_for_statustext
isinstancer   )ry   rz   rl   responser   s        r!   r{   r{      sD    {{3{0H==DdC 4!Kr0   c                     t        |       }	 t        |      j                         }|S # t        $ r, t        |j	                  d            j                         }Y |S w xY w)Nutf8)r@   r   r   	TypeErrorr   )inputsrI   s     r!   re   re      s[    
v,C2#h  " J  2#**V$%//1J2s   ( 1AA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 existN)r   r   existsr*   makedirsrD   r]   EEXIST)rb   rL   s     r!   rP   rP      sl    77>>"''//(34	KK12 5  	yyELL( )	s   2A2 2	B#;BB#)r   r]   r   rB   loggingr   os.pathr   r   filelockr   	getLoggerr~   rF   rR   r"   r/   r2   r{   re   rP   rn   r0   r!   <module>r      sb         	  
  g!  >E4
 
D	r0   