summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Dague <sdague@linux.vnet.ibm.com>2012-06-14 17:06:48 -0400
committerSean Dague <sdague@linux.vnet.ibm.com>2012-06-14 17:15:24 -0400
commit1fb23610eb47e4b6d181901fff59b4b30ac2aaad (patch)
tree89dd03b30a9bb7f33a351523920900176bcfadba
parent23d78dddf216cba0471b092fa615e6da6b1b4ebb (diff)
downloadoslo-1fb23610eb47e4b6d181901fff59b4b30ac2aaad.tar.gz
oslo-1fb23610eb47e4b6d181901fff59b4b30ac2aaad.tar.xz
oslo-1fb23610eb47e4b6d181901fff59b4b30ac2aaad.zip
add import_object_ns function
To support bp:virt-driver-cleanup, there is a desire to have shorter driver strings (i.e. libvirt.LibvirtDriver instead of nova.virt.libvirt.LibvirtDriver). One way to support this is with a new import_object variant that takes a namespace to load from. If the code fails to load the driver in the prefered namespace it will also try loading the full driver path, before throwing an exception. Change-Id: Ib97c92f38685ca89f29890f8015fc413acc744f8
-rw-r--r--openstack/common/importutils.py13
-rw-r--r--tests/unit/test_importutils.py43
2 files changed, 56 insertions, 0 deletions
diff --git a/openstack/common/importutils.py b/openstack/common/importutils.py
index 7654af5..933b54d 100644
--- a/openstack/common/importutils.py
+++ b/openstack/common/importutils.py
@@ -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)
diff --git a/tests/unit/test_importutils.py b/tests/unit/test_importutils.py
index acdb217..c41f13b 100644
--- a/tests/unit/test_importutils.py
+++ b/tests/unit/test_importutils.py
@@ -58,6 +58,49 @@ class ImportUtilsTest(unittest.TestCase):
first_arg=False)
self.assertTrue(obj.__class__.__name__, 'FakeDriver2')
+ # namespace tests
+ def test_import_object_ns_optional_arg_not_present(self):
+ obj = importutils.import_object_ns('tests.unit', 'fake.FakeDriver')
+ self.assertTrue(obj.__class__.__name__, 'FakeDriver')
+
+ def test_import_object_ns_optional_arg_present(self):
+ obj = importutils.import_object_ns('tests.unit', 'fake.FakeDriver',
+ first_arg=False)
+ self.assertTrue(obj.__class__.__name__, 'FakeDriver')
+
+ def test_import_object_ns_required_arg_not_present(self):
+ # arg 1 isn't optional here
+ self.assertRaises(TypeError, importutils.import_object_ns,
+ 'tests.unit', 'fake.FakeDriver2')
+
+ def test_import_object_ns_required_arg_present(self):
+ obj = importutils.import_object_ns('tests.unit', 'fake.FakeDriver2',
+ first_arg=False)
+ self.assertTrue(obj.__class__.__name__, 'FakeDriver2')
+
+ # namespace tests
+ def test_import_object_ns_full_optional_arg_not_present(self):
+ obj = importutils.import_object_ns('tests.unit2',
+ 'tests.unit.fake.FakeDriver')
+ self.assertTrue(obj.__class__.__name__, 'FakeDriver')
+
+ def test_import_object_ns_full_optional_arg_present(self):
+ obj = importutils.import_object_ns('tests.unit2',
+ 'tests.unit.fake.FakeDriver',
+ first_arg=False)
+ self.assertTrue(obj.__class__.__name__, 'FakeDriver')
+
+ def test_import_object_ns_full_required_arg_not_present(self):
+ # arg 1 isn't optional here
+ self.assertRaises(TypeError, importutils.import_object_ns,
+ 'tests.unit2', 'tests.unit.fake.FakeDriver2')
+
+ def test_import_object_ns_full_required_arg_present(self):
+ obj = importutils.import_object_ns('tests.unit2',
+ 'tests.unit.fake.FakeDriver2',
+ first_arg=False)
+ self.assertTrue(obj.__class__.__name__, 'FakeDriver2')
+
def test_import_object(self):
dt = importutils.import_object('datetime.time')
self.assertTrue(isinstance(dt, sys.modules['datetime'].time))