Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame^] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 14 | |
| 15 | """Utilities for OAuth. |
| 16 | |
Joe Gregorio | 825d78d | 2011-02-18 15:07:17 -0500 | [diff] [blame] | 17 | Utilities for making it easier to work with OAuth 1.0 credentials. |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 18 | """ |
| 19 | |
| 20 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 21 | |
| 22 | import pickle |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 23 | import threading |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 24 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 25 | from apiclient.oauth import Storage as BaseStorage |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 26 | |
Joe Gregorio | a0a52e4 | 2011-02-17 17:13:26 -0500 | [diff] [blame] | 27 | |
| 28 | class Storage(BaseStorage): |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 29 | """Store and retrieve a single credential to and from a file.""" |
| 30 | |
| 31 | def __init__(self, filename): |
| 32 | self._filename = filename |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 33 | self._lock = threading.Lock() |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 34 | |
| 35 | def get(self): |
| 36 | """Retrieve Credential from file. |
| 37 | |
| 38 | Returns: |
| 39 | apiclient.oauth.Credentials |
| 40 | """ |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 41 | self._lock.acquire() |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 42 | try: |
| 43 | f = open(self._filename, 'r') |
| 44 | credentials = pickle.loads(f.read()) |
| 45 | f.close() |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 46 | credentials.set_store(self.put) |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 47 | except: |
| 48 | credentials = None |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 49 | self._lock.release() |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 50 | |
| 51 | return credentials |
| 52 | |
| 53 | def put(self, credentials): |
| 54 | """Write a pickled Credentials to file. |
| 55 | |
| 56 | Args: |
| 57 | credentials: Credentials, the credentials to store. |
| 58 | """ |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 59 | self._lock.acquire() |
Joe Gregorio | 9b57dd3 | 2011-02-17 15:53:50 -0500 | [diff] [blame] | 60 | f = open(self._filename, 'w') |
| 61 | f.write(pickle.dumps(credentials)) |
| 62 | f.close() |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 63 | self._lock.release() |