
    7                         S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
  SrS\S\S	\S
\S\R                  4
S jrS\S\S	\S
\S\\   4
S jrS\S\S	\S
\S\SS4S jrg)zRProvides Logic for Fetching and Storing Discovery Documents from an on-disc cache.    N)Optional)logging)
stringutilzapi_discovery.json
cache_rootdiscovery_urlapi_nameapi_versionreturnc                     [         R                  " UR                  S5      5      R                  5       n[        R
                  " U UUU[        5      $ )z:Returns the cache file name for the given api and version.utf-8)hashlibsha1encode	hexdigestpathlibPath_DISCOVERY_CACHE_FILE)r   r   r   r	   url_hashs        ;platform/bq/discovery_documents/discovery_document_cache.py_get_cache_file_namer      sF    
 \\-..w78BBD(	
     c                 ~   [        XX#5      n[        R                  R                  U5      (       d  [        R
                  " SU5        g [        US5       nUR                  5       nSSS5        WR                  S5      $ ! , (       d  f       N= f! [         a!  n[        R                  " SXG5         SnAgSnAff = f)a  Loads a discovery document from the on-disc cache using key `api` and `version`.

Args:
  cache_root: [str], a directory where all cache files are stored.
  discovery_url: [str], URL where the discovery document was fetched from.
  api_name: [str], Name of api `discovery_document` to be saved.
  api_version: [str], Version of document to get

Returns:
  Discovery document as a dict.
  None if any errors occur during loading, or parsing the document
zDiscovery doc not in cache. %sNrbr   z'Error loading discovery document %s: %s)r   ospathisfiler   infoopenreaddecode	Exceptionwarning)r   r   r   r	   filefcontentses           r   get_from_cacher'   !   s      
j	O$			LL148	dD	Qh 
??7## 
	 
 OO=tGs0   
B B 'B  
B
B 
B<B77B<discovery_documentc                 :   [        XX#5      nUR                  nUR                  5       (       a  gUR                  SSS9  [        R
                  " US9 n[        R                  " U5      S-  nUR                  S5       n	U	R                  [        R                  " US5      5        U	R                  5         [        R                  " U	R                  5       5        SSS5        UR!                  U5        SSS5        g! , (       d  f       N(= f! , (       d  f       g= f)a  Saves a discovery document to the on-disc cache with key `api` and `version`.

Args:
  cache_root: [str], a directory where all cache files are stored.
  discovery_url: [str], URL where the discovery document was fetched from.
  api_name: [str], Name of api `discovery_document` to be saved.
  api_version: [str], Version of `discovery_document`.
  discovery_document: [str]. Discovery document as a json string.

Raises:
  OSError: If an error occurs when the file is written.
NT)parentsexist_ok)dirztmp.jsonwbutf8)r   parentexistsmkdirtempfileTemporaryDirectoryr   r   r   writer   ensure_binaryflushr   fsyncfilenorename)
r   r   r   r	   r(   r#   	directorytmpdirtemp_file_pathr$   s
             r   save_to_cacher=   A   s    ( 
j	O$kk) 
[[]]
//$/. ""y1V\\&)J6N			T	"aggj&&'96BC ggihhqxxz 
# $ 21	"	" 21s%   +D>AC;D;
D		D
D)__doc__r   r   r   r2   typingr   abslr   pyglibr   r   strr   r   r'   r=    r   r   <module>rD      s    X  	      - $'36EH\\$'36EHc]@. . .  .  	. 
 .  
. r   