1
2
3
4
5
6
7
8
9
10
11
12
13
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 stat
25 import threading
26
27 from anyjson import simplejson
28 from client import Storage as BaseStorage
29 from client import Credentials
30
31
33 """Store and retrieve a single credential to and from a file."""
34
38
40 """Acquires any lock necessary to access this Storage.
41
42 This lock is not reentrant."""
43 self._lock.acquire()
44
46 """Release the Storage lock.
47
48 Trying to release a lock that isn't held will result in a
49 RuntimeError.
50 """
51 self._lock.release()
52
74
76 """Create an empty file if necessary.
77
78 This method will not initialize the file. Instead it implements a
79 simple version of "touch" to ensure the file has been created.
80 """
81 if not os.path.exists(self._filename):
82 old_umask = os.umask(0177)
83 try:
84 open(self._filename, 'a+b').close()
85 finally:
86 os.umask(old_umask)
87
89 """Write Credentials to file.
90
91 Args:
92 credentials: Credentials, the credentials to store.
93 """
94
95 self._create_file_if_needed()
96 f = open(self._filename, 'wb')
97 f.write(credentials.to_json())
98 f.close()
99
101 """Delete Credentials file.
102
103 Args:
104 credentials: Credentials, the credentials to store.
105 """
106
107 os.unlink(self._filename)
108