o
     Þh¦  ã                   @   sè   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zddlm	Z	 W n e
yA   zddlm	Z	 W n e
y>   e
dƒ‚w Y nw ddlmZ dd	lmZ e e¡Zd
Ze ddd¡Zdd„ Zdd„ ZG dd„ dejƒZeed�ZdS )a  File based cache for the discovery document.

The cache is stored in a single file so that multiple processes can
share the same cache. It locks the file whenever accessing to the
file. When the cache content is corrupted, it will be initialized with
an empty cache.
é    )ÚdivisionN)Ú
LockedFilezIfile_cache is unavailable when using oauth2client >= 4.0.0 or google-authé   )Úbaseé   )ÚDISCOVERY_DOC_MAX_AGEz,google-api-python-client-discovery-doc.cachei²  c                 C   sL   z| t   ¡ W S  ty%   | t  }|j|j|jd d  d  d  Y S w )Né   i  i@B )ÚEPOCHÚtotal_secondsÚAttributeErrorÚmicrosecondsÚsecondsÚdays)ÚdateÚdelta© r   új/var/www/html/premium_crap/venv/lib/python3.10/site-packages/googleapiclient/discovery_cache/file_cache.pyÚ_to_timestamp4   s   þür   c                 C   sh   |   ¡  d¡ z
t |   ¡ ¡}W |S  ty3   i }|   ¡  d¡ |   ¡  d¡ t ||   ¡ ¡ Y |S w ©Nr   )Úfile_handleÚseekÚjsonÚloadÚ	ExceptionÚtruncateÚdump)ÚfÚcacher   r   r   Ú_read_or_initialize_cache@   s   ùùr   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCachez/A file based cache for the discovery documents.c              
   C   s    || _ tj t ¡ t¡| _t| jddƒ}z6z| 	¡  | 
¡ r"t|ƒ W n ty< } ztj|dd� W Y d}~nd}~ww W | ¡  dS W | ¡  dS | ¡  w )zTConstructor.

        Args:
          max_age: Cache expiration in seconds.
        za+ÚrT©Úexc_infoN)Ú_max_ageÚosÚpathÚjoinÚtempfileÚ
gettempdirÚFILENAMEÚ_filer   Úopen_and_lockÚ	is_lockedr   r   ÚLOGGERÚwarningÚunlock_and_close)ÚselfÚmax_ager   Úer   r   r   Ú__init__Q   s    €€ÿ€þzCache.__init__c              
   C   sÞ   t | jddƒ}zbzA| ¡  | ¡ r=t|ƒ}||v r5| |d¡\}}ttj ¡ ƒ|| j	 k r5|W W | 
¡  S W W | 
¡  d S t d¡ W W | 
¡  d S  tyb } ztj|dd� W Y d }~nd }~ww W | 
¡  d S | 
¡  w )Núr+r    r   ú+Could not obtain a lock for the cache file.Tr!   )r   r*   r+   r,   r   Úgetr   ÚdatetimeÚnowr#   r/   r-   Údebugr   r.   )r0   Úurlr   r   ÚcontentÚtr2   r   r   r   r6   e   s*   
ù
ûý€ÿz	Cache.getc           	   
   C   s  t | jddƒ}zyzQ| ¡  | ¡ rSt|ƒ}|ttj ¡ ƒf||< t| 	¡ ƒD ]\}\}}ttj ¡ ƒ|| j
 kr;||= q&| ¡  d¡ | ¡  d¡ t || ¡ ¡ nt d¡ W n tyr } ztj|dd� W Y d }~nd }~ww W | ¡  d S W | ¡  d S | ¡  w )Nr4   r    r   r5   Tr!   )r   r*   r+   r,   r   r   r7   r8   ÚlistÚitemsr#   r   r   r   r   r   r-   r9   r   r.   r/   )	r0   r:   r;   r   r   ÚkÚ_Ú	timestampr2   r   r   r   Úsetx   s2   ÿ€
€€ÿ€þz	Cache.setN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r6   rB   r   r   r   r   r   N   s
    r   )r1   )rF   Ú
__future__r   r7   r   Úloggingr$   r'   Ú oauth2client.contrib.locked_filer   ÚImportErrorÚoauth2client.locked_fileÚ r   Údiscovery_cacher   Ú	getLoggerrC   r-   r)   r	   r   r   r   r   r   r   r   r   Ú<module>   s8   ÿþÿý

C