From 740259025528836c4bc7a1d3bc840cdebf70192a Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Wed, 5 Sep 2012 12:55:55 +0100 Subject: [PATCH] Sync importutils changes from openstack-common Syncs the following changes from stable/folsom: 769ec65 Don't trap then re-raise ImportError. 8c74b37 Improve exception from importutils.import_class(). 1fb2361 add import_object_ns function Change-Id: Ib6046181ec4712702c30c8a8e938fc9a21b1a594 --- glanceclient/openstack/common/importutils.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/glanceclient/openstack/common/importutils.py b/glanceclient/openstack/common/importutils.py index 7654af5b..f45372b4 100644 --- a/glanceclient/openstack/common/importutils.py +++ b/glanceclient/openstack/common/importutils.py @@ -20,6 +20,7 @@ Import related utilities and helper functions. """ import sys +import traceback def import_class(import_str): @@ -28,9 +29,10 @@ def import_class(import_str): try: __import__(mod_str) return getattr(sys.modules[mod_str], class_str) - except (ImportError, ValueError, AttributeError), exc: + except (ValueError, AttributeError), exc: raise ImportError('Class %s cannot be found (%s)' % - (class_str, str(exc))) + (class_str, + traceback.format_exception(*sys.exc_info()))) def import_object(import_str, *args, **kwargs): @@ -38,6 +40,19 @@ def import_object(import_str, *args, **kwargs): return import_class(import_str)(*args, **kwargs) +def import_object_ns(name_space, import_str, *args, **kwargs): + """ + Import a class and return an instance of it, first by trying + to find the class in a default namespace, then failing back to + a full path if not found in the default namespace. + """ + import_value = "%s.%s" % (name_space, import_str) + try: + return import_class(import_value)(*args, **kwargs) + except ImportError: + return import_class(import_str)(*args, **kwargs) + + def import_module(import_str): """Import a module.""" __import__(import_str)