
                         h    d Z ddlZddlZddlmZ dZ G d de      Z G d dej                        Zy)	zYUtilities for OAuth.

Utilities for making it easier to work with OAuth 2.0
credentials.
    N)clientz$jcgregorio@google.com (Joe Gregorio)c                       e Zd ZdZy) CredentialsFileSymbolicLinkErrorz-Credentials files must not be symbolic links.N)__name__
__module____qualname____doc__     $lib/third_party/oauth2client/file.pyr   r      s    7r   r   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )Storagez:Store and retrieve a single credential to and from a file.c                 `    t         t        |   t        j                                || _        y )N)lock)superr   __init__	threadingLock	_filename)selffilename	__class__s     r   r   zStorage.__init__%   s#    gt%9>>+;%<!r   c                     t         j                  j                  | j                        r$t	        dj                  | j                              y )NzFile: {0} is a symbolic link.)ospathislinkr   r   formatr   s    r   _validate_filezStorage._validate_file)   s<    77>>$..)2/66t~~FH H *r   c                 <   d}| j                          	 t        | j                  d      }|j                         }|j	                          	 t        j                  j                  |      }|j                  |        |S # t
        $ r |cY S w xY w# t        $ r Y |S w xY w)zRetrieve Credential from file.

        Returns:
            oauth2client.client.Credentials

        Raises:
            CredentialsFileSymbolicLinkError if the file is a symbolic link.
        Nrb)r   openr   readcloseIOErrorr   Credentialsnew_from_json	set_store
ValueError)r   credentialsfcontents       r   
locked_getzStorage.locked_get.   s     	T^^T*AffhGGGI	 ,,::7CK!!$'   		  		s#   6A= 0B =B
B	BBc                 *   t         j                  j                  | j                        sPt        j                  d      }	 t        | j                  d      j                          t        j                  |       yy# t        j                  |       w xY w)zCreate 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.
           za+bN)r   r   existsr   umaskr"   r$   )r   	old_umasks     r   _create_file_if_neededzStorage._create_file_if_neededH   sb     ww~~dnn-I$T^^U+113# .
 #s    $A; ;Bc                     | j                          | j                          t        | j                  d      }|j	                  |j                                |j                          y)zWrite Credentials to file.

        Args:
            credentials: Credentials, the credentials to store.

        Raises:
            CredentialsFileSymbolicLinkError if the file is a symbolic link.
        wN)r3   r   r"   r   writeto_jsonr$   )r   r*   r+   s      r   
locked_putzStorage.locked_putU   sJ     	##%%	##%&		r   c                 B    t        j                  | j                         y)zpDelete Credentials file.

        Args:
            credentials: Credentials, the credentials to store.
        N)r   unlinkr   r   s    r   locked_deletezStorage.locked_deleted   s     			$..!r   )r   r   r   r	   r   r   r-   r3   r8   r;   __classcell__)r   s   @r   r   r   "   s%    D"H
4$"r   r   )	r	   r   r   oauth2clientr   
__author__	Exceptionr   r   r
   r   r   <module>r@      s:    
   4
8y 8H"fnn H"r   