ó
‚¾^Yc           @   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 d d l Z y d d l	 m
 Z
 WnE e k
 r¹ y d d l m
 Z
 Wqº e k
 rµ e d ƒ ‚ qº Xn Xd d l m Z d d	 l m Z e j e ƒ Z d
 Z e j j d ƒ Z d „  Z d „  Z d e j f d „  ƒ  YZ e d e ƒ Z d S(   s
  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 accesing to the
file. When the cache content is corrupted, it will be initialized with
an empty cache.
iÿÿÿÿ(   t   divisionN(   t
   LockedFiles:   file_cache is unavailable when using oauth2client >= 4.0.0i   (   t   basei   (   t   DISCOVERY_DOC_MAX_AGEs,   google-api-python-client-discovery-doc.cachei    c         C   sV   y |  t  j ƒ  SWn= t k
 rQ |  t  } | j | j | j d d d d SXd  S(   Ni   i  i
   i   i@B i@B (   t   EPOCHt   total_secondst   AttributeErrort   microsecondst   secondst   days(   t   datet   delta(    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyt   _to_timestamp4   s    
c         C   s†   |  j  ƒ  j d ƒ y t j |  j  ƒ  ƒ } WnS t k
 r i  } |  j  ƒ  j d ƒ |  j  ƒ  j d ƒ t j | |  j  ƒ  ƒ n X| S(   Ni    (   t   file_handlet   seekt   jsont   loadt	   Exceptiont   truncatet   dump(   t   ft   cache(    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyt   _read_or_initialize_cache?   s    t   Cachec           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s/   A file based cache for the discovery documents.c         C   s¢   | |  _  t j j t j ƒ  t ƒ |  _ t |  j d d ƒ } zT y' | j	 ƒ  | j
 ƒ  re t | ƒ n  Wn& t k
 rŽ } t j | d t ƒn XWd | j ƒ  Xd S(   sN   Constructor.

      Args:
        max_age: Cache expiration in seconds.
      s   a+t   rt   exc_infoN(   t   _max_aget   ost   patht   joint   tempfilet
   gettempdirt   FILENAMEt   _fileR   t   open_and_lockt	   is_lockedR   R   t   LOGGERt   warningt   Truet   unlock_and_close(   t   selft   max_ageR   t   e(    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyt   __init__P   s    	 
c         C   sÜ   t  |  j d d ƒ } zµ yˆ | j ƒ  | j ƒ  rŽ t | ƒ } | | k rŠ | j | d ƒ \ } } t t j j	 ƒ  ƒ | |  j
 k  rŠ | Sn  d  St j d ƒ d  SWn& t k
 rÈ } t j | d t ƒn XWd  | j ƒ  Xd  S(   Ns   r+R   i    s+   Could not obtain a lock for the cache file.R   (   Ni    (   R   R!   R"   R#   R   t   gett   NoneR   t   datetimet   nowR   R$   t   debugR   R%   R&   R'   (   R(   t   urlR   R   t   contentt   tR*   (    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyR,   d   s      
"c   	      C   s:  t  |  j d d ƒ } zyæ | j ƒ  | j ƒ  rð t | ƒ } | t t j j ƒ  ƒ f | | <xR t | j	 ƒ  ƒ D]> \ } \ } } t t j j ƒ  ƒ | |  j
 k ro | | =qo qo W| j ƒ  j d ƒ | j ƒ  j d ƒ t j | | j ƒ  ƒ n t j d ƒ Wn& t k
 r&} t j | d t ƒn XWd  | j ƒ  Xd  S(   Ns   r+R   i    s+   Could not obtain a lock for the cache file.R   (   R   R!   R"   R#   R   R   R.   R/   t   listt   itemsR   R   R   R   R   R   R$   R0   R   R%   R&   R'   (	   R(   R1   R2   R   R   t   kt   _t	   timestampR*   (    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyt   setw   s"     
%"(   t   __name__t
   __module__t   __doc__R+   R,   R9   (    (    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyR   M   s   		R)   (   R<   t
   __future__R    R.   R   t   loggingR   R   t	   threadingt    oauth2client.contrib.locked_fileR   t   ImportErrort   oauth2client.locked_filet    R   t   discovery_cacheR   t	   getLoggerR:   R$   R    t   utcfromtimestampR   R   R   R   R   (    (    (    s\   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/discovery_cache/file_cache.pyt   <module>   s0   		@