From f6c56d51a202d9604469cd7e92a0cd7bbdeec8eb Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Wed, 4 Nov 2015 14:59:23 +0800 Subject: [PATCH] Use the oslo.utils.reflection to extract the class name The oslo.utils reflection module/code handles more variations of where a class name may come from (on python 2 and python 3) so its usage allows getting more accurate class names so we might as well use it. Change-Id: I94a1f522755ec76b9b48b881c42b65a7c4c3860f --- keystone/common/kvs/core.py | 9 +++++++-- keystone/common/ldap/core.py | 5 ++++- keystone/common/manager.py | 5 +++-- keystone/common/utils.py | 9 +++++++-- keystone/models/token_model.py | 5 ++++- keystone/notifications.py | 4 +++- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/keystone/common/kvs/core.py b/keystone/common/kvs/core.py index 12176cb263..9f2d35cc87 100644 --- a/keystone/common/kvs/core.py +++ b/keystone/common/kvs/core.py @@ -25,6 +25,7 @@ from dogpile.core import nameregistry from oslo_config import cfg from oslo_log import log from oslo_utils import importutils +from oslo_utils import reflection from keystone import exception from keystone.i18n import _ @@ -147,12 +148,16 @@ class KeyValueStore(object): if issubclass(pxy, proxy.ProxyBackend): proxies.append(pxy) else: + pxy_cls_name = reflection.get_class_name( + pxy, fully_qualified=False) LOG.warning(_LW('%s is not a dogpile.proxy.ProxyBackend'), - pxy.__name__) + pxy_cls_name) for proxy_cls in reversed(proxies): + proxy_cls_name = reflection.get_class_name( + proxy_cls, fully_qualified=False) LOG.info(_LI('Adding proxy \'%(proxy)s\' to KVS %(name)s.'), - {'proxy': proxy_cls.__name__, + {'proxy': proxy_cls_name, 'name': self._region.name}) self._region.wrap(proxy_cls) diff --git a/keystone/common/ldap/core.py b/keystone/common/ldap/core.py index d321c7552b..e102ca4949 100644 --- a/keystone/common/ldap/core.py +++ b/keystone/common/ldap/core.py @@ -24,6 +24,7 @@ import ldap.controls import ldap.filter import ldappool from oslo_log import log +from oslo_utils import reflection import six from six.moves import map, zip @@ -71,8 +72,10 @@ def utf8_encode(value): elif isinstance(value, six.binary_type): return value else: + value_cls_name = reflection.get_class_name( + value, fully_qualified=False) raise TypeError("value must be basestring, " - "not %s" % value.__class__.__name__) + "not %s" % value_cls_name) _utf8_decoder = codecs.getdecoder('utf-8') diff --git a/keystone/common/manager.py b/keystone/common/manager.py index bbe9a89560..41f481227e 100644 --- a/keystone/common/manager.py +++ b/keystone/common/manager.py @@ -17,6 +17,7 @@ import functools from oslo_log import log from oslo_log import versionutils from oslo_utils import importutils +from oslo_utils import reflection import stevedore from keystone.i18n import _ @@ -125,14 +126,14 @@ def create_legacy_driver(driver_class): """ module_name = driver_class.__module__ - class_name = driver_class.__name__ + class_name = reflection.get_class_name(driver_class) class Driver(driver_class): @versionutils.deprecated( as_of=versionutils.deprecated.LIBERTY, what='%s.Driver' % module_name, - in_favor_of='%s.%s' % (module_name, class_name), + in_favor_of=class_name, remove_in=+2) def __init__(self, *args, **kwargs): super(Driver, self).__init__(*args, **kwargs) diff --git a/keystone/common/utils.py b/keystone/common/utils.py index 66ae6050b4..08618f32d8 100644 --- a/keystone/common/utils.py +++ b/keystone/common/utils.py @@ -27,6 +27,7 @@ import uuid from oslo_config import cfg from oslo_log import log from oslo_serialization import jsonutils +from oslo_utils import reflection from oslo_utils import strutils from oslo_utils import timeutils import passlib.hash @@ -315,8 +316,10 @@ def get_unix_user(user=None): elif user is None: user_info = pwd.getpwuid(os.geteuid()) else: + user_cls_name = reflection.get_class_name(user, + fully_qualified=False) raise TypeError('user must be string, int or None; not %s (%r)' % - (user.__class__.__name__, user)) + (user_cls_name, user)) return user_info.pw_uid, user_info.pw_name @@ -373,8 +376,10 @@ def get_unix_group(group=None): elif group is None: group_info = grp.getgrgid(os.getegid()) else: + group_cls_name = reflection.get_class_name(group, + fully_qualified=False) raise TypeError('group must be string, int or None; not %s (%r)' % - (group.__class__.__name__, group)) + (group_cls_name, group)) return group_info.gr_gid, group_info.gr_name diff --git a/keystone/models/token_model.py b/keystone/models/token_model.py index c4c2df8295..32e6b36549 100644 --- a/keystone/models/token_model.py +++ b/keystone/models/token_model.py @@ -14,6 +14,7 @@ from keystoneclient.common import cms from oslo_config import cfg +from oslo_utils import reflection from oslo_utils import timeutils import six @@ -64,7 +65,9 @@ class KeystoneToken(dict): def __repr__(self): desc = ('<%(type)s (audit_id=%(audit_id)s, ' 'audit_chain_id=%(audit_chain_id)s) at %(loc)s>') - return desc % {'type': self.__class__.__name__, + self_cls_name = reflection.get_class_name(self, + fully_qualified=False) + return desc % {'type': self_cls_name, 'audit_id': self.audit_id, 'audit_chain_id': self.audit_chain_id, 'loc': hex(id(self))} diff --git a/keystone/notifications.py b/keystone/notifications.py index 29ac3d2736..ef56d2865f 100644 --- a/keystone/notifications.py +++ b/keystone/notifications.py @@ -23,6 +23,7 @@ import socket from oslo_config import cfg from oslo_log import log import oslo_messaging +from oslo_utils import reflection import pycadf from pycadf import cadftaxonomy as taxonomy from pycadf import cadftype @@ -254,7 +255,8 @@ def _get_callback_info(callback): module_name = getattr(callback, '__module__', None) func_name = callback.__name__ if inspect.ismethod(callback): - class_name = callback.__self__.__class__.__name__ + class_name = reflection.get_class_name(callback.__self__, + fully_qualified=False) return [module_name, class_name, func_name] else: return [module_name, func_name]