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 | |
| 13 | |
| 14 | class Storage(object): |
| 15 | """Store and retrieve a single credential to and from a file.""" |
| 16 | |
| 17 | def __init__(self, filename): |
| 18 | self._filename = filename |
| 19 | |
| 20 | def get(self): |
| 21 | """Retrieve Credential from file. |
| 22 | |
| 23 | Returns: |
| 24 | apiclient.oauth.Credentials |
| 25 | """ |
| 26 | try: |
| 27 | f = open(self._filename, 'r') |
| 28 | credentials = pickle.loads(f.read()) |
| 29 | f.close() |
| 30 | except: |
| 31 | credentials = None |
| 32 | |
| 33 | return credentials |
| 34 | |
| 35 | def put(self, credentials): |
| 36 | """Write a pickled Credentials to file. |
| 37 | |
| 38 | Args: |
| 39 | credentials: Credentials, the credentials to store. |
| 40 | """ |
| 41 | f = open(self._filename, 'w') |
| 42 | f.write(pickle.dumps(credentials)) |
| 43 | f.close() |