
    7                         d 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 ddl	m
Z
 dZdeded	ed
edej                  f
dZdeded	ed
edee   f
dZdeded	ed
ededdfdZy)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                     t        j                  |j                  d            j                         }t	        j
                  | |||t              S )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                 j   t        | |||      }t        j                  j                  |      st	        j
                  d|       y	 t        |d      5 }|j                         }ddd       j                  d      S # 1 sw Y   xY w# t        $ r!}t	        j                  d||       Y d}~yd}~ww xY w)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=tQGs0   B A<#B <BB 	B2B--B2discovery_documentc                 $   t        | |||      }|j                  }|j                         ry|j                  dd       t	        j
                  |      5 }t        j                  |      dz  }|j                  d      5 }	|	j                  t        j                  |d             |	j                          t        j                  |	j                                ddd       |j!                  |       ddd       y# 1 sw Y   #xY w# 1 sw Y   yxY w)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   