ó
¾^Yc           @   sK   d  Z  d d l Z d d l Z d d l m Z d e j f d     YZ d S(   sR   A keyring based Storage.

A Storage for Credentials that uses the keyring module.
i’’’’N(   t   clientt   Storagec           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   sM  Store and retrieve a single credential to and from the keyring.

    To use this module you must have the keyring module installed. See
    <http://pypi.python.org/pypi/keyring/>. This is an optional module and is
    not installed with oauth2client by default because it does not work on all
    the platforms that oauth2client supports, such as Google App Engine.

    The keyring module <http://pypi.python.org/pypi/keyring/> is a
    cross-platform library for access the keyring capabilities of the local
    system. The user will be prompted for their keyring password when this
    module is used, and the manner in which the user is prompted will vary per
    platform.

    Usage::

        from oauth2client import keyring_storage

        s = keyring_storage.Storage('name_of_application', 'user1')
        credentials = s.get()

    c         C   s5   t  t |   j d t j    | |  _ | |  _ d S(   sī   Constructor.

        Args:
            service_name: string, The name of the service under which the
                          credentials are stored.
            user_name: string, The name of the user to store credentials for.
        t   lockN(   t   superR   t   __init__t	   threadingt   Lockt   _service_namet
   _user_name(   t   selft   service_namet	   user_name(    (    sJ   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/keyring_storage.pyR   2   s    	c         C   sh   d } t j |  j |  j  } | d k	 rd y# t j j |  } | j |   Wqd t	 k
 r` qd Xn  | S(   se   Retrieve Credential from file.

        Returns:
            oauth2client.client.Credentials
        N(
   t   Nonet   keyringt   get_passwordR   R   R    t   Credentialst   new_from_jsont	   set_storet
   ValueError(   R	   t   credentialst   content(    (    sJ   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/keyring_storage.pyt
   locked_get>   s    c         C   s#   t  j |  j |  j | j    d S(   sr   Write Credentials to file.

        Args:
            credentials: Credentials, the credentials to store.
        N(   R   t   set_passwordR   R   t   to_json(   R	   R   (    (    sJ   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/keyring_storage.pyt
   locked_putP   s    c         C   s   t  j |  j |  j d  d S(   sp   Delete Credentials file.

        Args:
            credentials: Credentials, the credentials to store.
        t    N(   R   R   R   R   (   R	   (    (    sJ   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/keyring_storage.pyt   locked_deleteY   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   (    (    (    sJ   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/keyring_storage.pyR      s
   				(   R   R   R   t   oauth2clientR    R   (    (    (    sJ   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/keyring_storage.pyt   <module>   s   