| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
from keystoneclient.i18n import _ |
| 15 |
from keystoneclient.v3.contrib.oauth1 import access_tokens |
| 16 |
from keystoneclient.v3.contrib.oauth1 import consumers |
| 17 |
from keystoneclient.v3.contrib.oauth1 import request_tokens |
| 18 |
|
| 19 |
|
| 20 |
def create_oauth_manager(self): |
| 21 |
|
| 22 |
try: |
| 23 |
import oauthlib |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
except ImportError: |
| 29 |
return OAuthManagerOptionalImportProxy() |
| 30 |
else: |
| 31 |
return OAuthManager(self) |
| 32 |
|
| 33 |
|
| 34 |
class OAuthManager(object): |
| 35 |
def __init__(self, api): |
| 36 |
self.access_tokens = access_tokens.AccessTokenManager(api) |
| 37 |
self.consumers = consumers.ConsumerManager(api) |
| 38 |
self.request_tokens = request_tokens.RequestTokenManager(api) |
| 39 |
|
| 40 |
|
| 41 |
class OAuthManagerOptionalImportProxy(object): |
| 42 |
"""Act as a proxy manager in case oauthlib is no installed. |
| 43 |
|
| 44 |
This class will only be created if oauthlib is not in the system, |
| 45 |
trying to access any of the attributes in name (access_tokens, |
| 46 |
consumers, request_tokens), will result in a NotImplementedError, |
| 47 |
and a message. |
| 48 |
|
| 49 |
>>> manager.access_tokens.blah |
| 50 |
NotImplementedError: To use 'access_tokens' oauthlib must be installed |
| 51 |
|
| 52 |
Otherwise, if trying to access an attribute other than the ones in name, |
| 53 |
the manager will state that the attribute does not exist. |
| 54 |
|
| 55 |
>>> manager.dne.blah |
| 56 |
AttributeError: 'OAuthManagerOptionalImportProxy' object has no |
| 57 |
attribute 'dne' |
| 58 |
""" |
| 59 |
|
| 60 |
def __getattribute__(self, name): |
| 61 |
"""Return error when name is related to oauthlib and not exist.""" |
| 62 |
if name in ('access_tokens', 'consumers', 'request_tokens'): |
| 63 |
raise NotImplementedError( |
| 64 |
_('To use %r oauthlib must be installed') % name) |
| 65 |
return super(OAuthManagerOptionalImportProxy, |
| 66 |
self).__getattribute__(name) |