1# Licensed under the Apache License, Version 2.0 (the "License"); 2# you may not use this file except in compliance with the License. 3# You may obtain a copy of the License at 4# 5# http://www.apache.org/licenses/LICENSE-2.0 6# 7# Unless required by applicable law or agreed to in writing, software 8# distributed under the License is distributed on an "AS IS" BASIS, 9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 10# implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14from keystoneclient.i18n import _ 15from keystoneclient.v3.contrib.oauth1 import access_tokens 16from keystoneclient.v3.contrib.oauth1 import consumers 17from keystoneclient.v3.contrib.oauth1 import request_tokens 18 19 20def create_oauth_manager(self): 21 # NOTE(stevemar): Attempt to import the oauthlib package at this point. 22 try: 23 import oauthlib # noqa 24 # NOTE(stevemar): Return an object instead of raising an exception here, 25 # this will allow users to see an exception only when trying to access the 26 # oauth portions of client. Otherwise an exception would be raised 27 # when the client is created. 28 except ImportError: 29 return OAuthManagerOptionalImportProxy() 30 else: 31 return OAuthManager(self) 32 33 34class 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 41class 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) 67