From 2633156659bd767b5b206fba9fbf68d2dcb62c51 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Mon, 2 Jul 2012 11:33:09 -0400 Subject: Sync latest importutils from openstack-common. This patch pulls in the latest version of importutils from openstack-common. It includes a formatting change and one new function: import_object_ns(). Change-Id: Ifc1533fe1e33a447e4c501d759a6c838133d1063 --- nova/openstack/common/importutils.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'nova/openstack') diff --git a/nova/openstack/common/importutils.py b/nova/openstack/common/importutils.py index 7654af5b9..67d94ad5f 100644 --- a/nova/openstack/common/importutils.py +++ b/nova/openstack/common/importutils.py @@ -30,7 +30,7 @@ def import_class(import_str): return getattr(sys.modules[mod_str], class_str) except (ImportError, ValueError, AttributeError), exc: raise ImportError('Class %s cannot be found (%s)' % - (class_str, str(exc))) + (class_str, str(exc))) def import_object(import_str, *args, **kwargs): @@ -38,6 +38,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) -- cgit