Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 1 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | """Utilities for OAuth. |
| 4 | |
| 5 | Utilities for making it easier to work with OAuth 1.0 |
| 6 | credentials. |
| 7 | """ |
| 8 | |
| 9 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 10 | |
| 11 | import pickle |
| 12 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame^] | 13 | from apiclient.oauth import Storage as BaseStorage |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 14 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame^] | 15 | |
| 16 | class Storage(BaseStorage): |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 17 | """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 | """ |
| 28 | try: |
| 29 | f = open(self._filename, 'r') |
| 30 | credentials = pickle.loads(f.read()) |
| 31 | f.close() |
| 32 | except: |
| 33 | credentials = None |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame^] | 34 | credentials.set_store(self.put) |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 35 | |
| 36 | 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 | """ |
| 44 | f = open(self._filename, 'w') |
| 45 | f.write(pickle.dumps(credentials)) |
| 46 | f.close() |