blob: 0bcab033e13d32bc007dcf74339a9ae83585cdbb [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):
20 self.filename = filename
21
22 def get(self):
23 """Retrieve Credential from file.
24
25 Returns:
26 apiclient.oauth.Credentials
27 """
Joe Gregoriodeeb0202011-02-15 14:49:57 -050028 try:
29 f = open(self.filename, 'r')
30 credentials = pickle.loads(f.read())
31 f.close()
32 credentials.set_store(self.put)
33 except:
34 credentials = None
Joe Gregorio695fdc12011-01-16 16:46:55 -050035 return credentials
36
37 def put(self, credentials):
38 """Write a pickled Credentials to file.
39
40 Args:
41 credentials: Credentials, the credentials to store.
42 """
43 f = open(self.filename, 'w')
44 f.write(pickle.dumps(credentials))
45 f.close()