blob: 12dee91dfd8b556849624c347f8c95b9d175193a [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001# Copyright 2010 Google Inc. All Rights Reserved.
2
3"""Utilities for OAuth.
4
5Utilities for making it easier to work with OAuth 2.0
6credentials.
7"""
8
9__author__ = 'jcgregorio@google.com (Joe Gregorio)'
10
11import pickle
12
Joe Gregoriodeeb0202011-02-15 14:49:57 -050013from client import Storage as BaseStorage
Joe Gregorio695fdc12011-01-16 16:46:55 -050014
Joe Gregoriodeeb0202011-02-15 14:49:57 -050015
16class Storage(BaseStorage):
Joe Gregorio695fdc12011-01-16 16:46:55 -050017 """Store and retrieve a single credential to and from a file."""
18
19 def __init__(self, filename):
Joe Gregorio7c22ab22011-02-16 15:32:39 -050020 self._filename = filename
Joe Gregorio695fdc12011-01-16 16:46:55 -050021
22 def get(self):
23 """Retrieve Credential from file.
24
25 Returns:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050026 oauth2client.client.Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050027 """
Joe Gregoriodeeb0202011-02-15 14:49:57 -050028 try:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050029 f = open(self._filename, 'r')
Joe Gregoriodeeb0202011-02-15 14:49:57 -050030 credentials = pickle.loads(f.read())
31 f.close()
32 credentials.set_store(self.put)
33 except:
34 credentials = None
Joe Gregorio7c22ab22011-02-16 15:32:39 -050035
Joe Gregorio695fdc12011-01-16 16:46:55 -050036 return credentials
37
38 def put(self, credentials):
39 """Write a pickled Credentials to file.
40
41 Args:
42 credentials: Credentials, the credentials to store.
43 """
Joe Gregorio7c22ab22011-02-16 15:32:39 -050044 f = open(self._filename, 'w')
Joe Gregorio695fdc12011-01-16 16:46:55 -050045 f.write(pickle.dumps(credentials))
46 f.close()