ķ
ž^Yc           @   s[   d  Z  d d l Z d d l Z d d l m Z d d l m Z d e j f d     YZ d S(   sY   Utilities for OAuth.

Utilities for making it easier to work with OAuth 2.0
credentials.
i˙˙˙˙N(   t   _helpers(   t   clientt   Storagec           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s:   Store and retrieve a single credential to and from a file.c         C   s,   t  t |   j d t j    | |  _ d  S(   Nt   lock(   t   superR   t   __init__t	   threadingt   Lockt	   _filename(   t   selft   filename(    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyR      s    c         C   s   d } t j |  j  y, t |  j d  } | j   } | j   Wn t k
 rV | SXy# t j	 j
 |  } | j |   Wn t k
 r n X| S(   sĒ   Retrieve Credential from file.

        Returns:
            oauth2client.client.Credentials

        Raises:
            IOError if the file is a symbolic link.
        t   rbN(   t   NoneR    t   validate_fileR   t   opent   readt   closet   IOErrorR   t   Credentialst   new_from_jsont	   set_storet
   ValueError(   R	   t   credentialst   ft   content(    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyt
   locked_get#   s    	c         C   sV   t  j j |  j  sR t  j d  } z t |  j d  j   Wd t  j |  Xn  d S(   sŊ   Create an empty file if necessary.

        This method will not initialize the file. Instead it implements a
        simple version of "touch" to ensure the file has been created.
        i   s   a+bN(   t   ost   patht   existsR   t   umaskR   R   (   R	   t	   old_umask(    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyt   _create_file_if_needed=   s
    c         C   sM   |  j    t j |  j  t |  j d  } | j | j    | j   d S(   sˇ   Write Credentials to file.

        Args:
            credentials: Credentials, the credentials to store.

        Raises:
            IOError if the file is a symbolic link.
        t   wN(   R   R    R   R   R   t   writet   to_jsonR   (   R	   R   R   (    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyt
   locked_putJ   s
    	
c         C   s   t  j |  j  d S(   sp   Delete Credentials file.

        Args:
            credentials: Credentials, the credentials to store.
        N(   R   t   unlinkR   (   R	   (    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyt   locked_deleteY   s    (   t   __name__t
   __module__t   __doc__R   R   R   R#   R%   (    (    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyR      s   				(   R(   R   R   t   oauth2clientR    R   R   (    (    (    s7   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/file.pyt   <module>   s
   