| OLD | NEW |
| (Empty) |
| 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. | |
| 14 | |
| 15 """Utilities for OAuth. | |
| 16 | |
| 17 Utilities for making it easier to work with OAuth 1.0 credentials. | |
| 18 """ | |
| 19 | |
| 20 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | |
| 21 | |
| 22 import pickle | |
| 23 import threading | |
| 24 | |
| 25 from apiclient.oauth import Storage as BaseStorage | |
| 26 | |
| 27 | |
| 28 class Storage(BaseStorage): | |
| 29 """Store and retrieve a single credential to and from a file.""" | |
| 30 | |
| 31 def __init__(self, filename): | |
| 32 self._filename = filename | |
| 33 self._lock = threading.Lock() | |
| 34 | |
| 35 def get(self): | |
| 36 """Retrieve Credential from file. | |
| 37 | |
| 38 Returns: | |
| 39 apiclient.oauth.Credentials | |
| 40 """ | |
| 41 self._lock.acquire() | |
| 42 try: | |
| 43 f = open(self._filename, 'r') | |
| 44 credentials = pickle.loads(f.read()) | |
| 45 f.close() | |
| 46 credentials.set_store(self.put) | |
| 47 except: | |
| 48 credentials = None | |
| 49 self._lock.release() | |
| 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 """ | |
| 59 self._lock.acquire() | |
| 60 f = open(self._filename, 'w') | |
| 61 f.write(pickle.dumps(credentials)) | |
| 62 f.close() | |
| 63 self._lock.release() | |
| OLD | NEW |