Package oauth2client :: Module file
[hide private]
[frames] | no frames]

Source Code for Module oauth2client.file

  1  # Copyright 2014 Google Inc. All rights reserved. 
  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 2.0 
 18  credentials. 
 19  """ 
 20   
 21  __author__ = 'jcgregorio@google.com (Joe Gregorio)' 
 22   
 23  import os 
 24  import threading 
 25   
 26  from oauth2client.client import Credentials 
 27  from oauth2client.client import Storage as BaseStorage 
 28   
 29   
30 -class CredentialsFileSymbolicLinkError(Exception):
31 """Credentials files must not be symbolic links."""
32 33
34 -class Storage(BaseStorage):
35 """Store and retrieve a single credential to and from a file.""" 36
37 - def __init__(self, filename):
38 self._filename = filename 39 self._lock = threading.Lock()
40
41 - def _validate_file(self):
42 if os.path.islink(self._filename): 43 raise CredentialsFileSymbolicLinkError( 44 'File: %s is a symbolic link.' % self._filename)
45
46 - def acquire_lock(self):
47 """Acquires any lock necessary to access this Storage. 48 49 This lock is not reentrant.""" 50 self._lock.acquire()
51
52 - def release_lock(self):
53 """Release the Storage lock. 54 55 Trying to release a lock that isn't held will result in a 56 RuntimeError. 57 """ 58 self._lock.release()
59
60 - def locked_get(self):
61 """Retrieve Credential from file. 62 63 Returns: 64 oauth2client.client.Credentials 65 66 Raises: 67 CredentialsFileSymbolicLinkError if the file is a symbolic link. 68 """ 69 credentials = None 70 self._validate_file() 71 try: 72 f = open(self._filename, 'rb') 73 content = f.read() 74 f.close() 75 except IOError: 76 return credentials 77 78 try: 79 credentials = Credentials.new_from_json(content) 80 credentials.set_store(self) 81 except ValueError: 82 pass 83 84 return credentials
85
86 - def _create_file_if_needed(self):
87 """Create an empty file if necessary. 88 89 This method will not initialize the file. Instead it implements a 90 simple version of "touch" to ensure the file has been created. 91 """ 92 if not os.path.exists(self._filename): 93 old_umask = os.umask(0o177) 94 try: 95 open(self._filename, 'a+b').close() 96 finally: 97 os.umask(old_umask)
98
99 - def locked_put(self, credentials):
100 """Write Credentials to file. 101 102 Args: 103 credentials: Credentials, the credentials to store. 104 105 Raises: 106 CredentialsFileSymbolicLinkError if the file is a symbolic link. 107 """ 108 109 self._create_file_if_needed() 110 self._validate_file() 111 f = open(self._filename, 'w') 112 f.write(credentials.to_json()) 113 f.close()
114
115 - def locked_delete(self):
116 """Delete Credentials file. 117 118 Args: 119 credentials: Credentials, the credentials to store. 120 """ 121 122 os.unlink(self._filename)
123