Re-enable _import* hidden methods in import_utils

There are many scenarios in different projects that end
up needing this support. We should gently nudge everyone
to move to Stevedore but can't mandate it by hiding
these methods.

Change-Id: Ie2783fabf52491a490c999728995da8e68e62cd8
This commit is contained in:
Davanum Srinivas 2014-08-26 12:22:30 -04:00
parent e0425691d9
commit 118f7ea84a
2 changed files with 36 additions and 36 deletions

View File

@ -21,7 +21,7 @@ import sys
import traceback
def _import_class(import_str):
def import_class(import_str):
"""Returns a class from a string including module and class."""
mod_str, _sep, class_str = import_str.rpartition('.')
__import__(mod_str)
@ -33,12 +33,12 @@ def _import_class(import_str):
traceback.format_exception(*sys.exc_info())))
def _import_object(import_str, *args, **kwargs):
def import_object(import_str, *args, **kwargs):
"""Import a class and return an instance of it."""
return _import_class(import_str)(*args, **kwargs)
return import_class(import_str)(*args, **kwargs)
def _import_object_ns(name_space, import_str, *args, **kwargs):
def import_object_ns(name_space, import_str, *args, **kwargs):
"""Tries to import object from default namespace.
Imports a class and return an instance of it, first by trying
@ -47,27 +47,27 @@ def _import_object_ns(name_space, import_str, *args, **kwargs):
"""
import_value = "%s.%s" % (name_space, import_str)
try:
return _import_class(import_value)(*args, **kwargs)
return import_class(import_value)(*args, **kwargs)
except ImportError:
return _import_class(import_str)(*args, **kwargs)
return import_class(import_str)(*args, **kwargs)
def _import_module(import_str):
def import_module(import_str):
"""Import a module."""
__import__(import_str)
return sys.modules[import_str]
def _import_versioned_module(version, submodule=None):
def import_versioned_module(version, submodule=None):
module = 'oslo.v%s' % version
if submodule:
module = '.'.join((module, submodule))
return _import_module(module)
return import_module(module)
def try_import(import_str, default=None):
"""Try to import a module and if it fails return default."""
try:
return _import_module(import_str)
return import_module(import_str)
except ImportError:
return default

View File

@ -27,85 +27,85 @@ class ImportUtilsTest(test_base.BaseTestCase):
# __import__ is the devil. Right now we just make
# sure we can import something from the stdlib
def test_import_class(self):
dt = importutils._import_class('datetime.datetime')
dt = importutils.import_class('datetime.datetime')
self.assertEqual(sys.modules['datetime'].datetime, dt)
def test_import_bad_class(self):
self.assertRaises(ImportError, importutils._import_class,
self.assertRaises(ImportError, importutils.import_class,
'lol.u_mad.brah')
def test_import_module(self):
dt = importutils._import_module('datetime')
dt = importutils.import_module('datetime')
self.assertEqual(sys.modules['datetime'], dt)
def test_import_object_optional_arg_not_present(self):
obj = importutils._import_object('tests.fake.FakeDriver')
obj = importutils.import_object('tests.fake.FakeDriver')
self.assertEqual(obj.__class__.__name__, 'FakeDriver')
def test_import_object_optional_arg_present(self):
obj = importutils._import_object('tests.fake.FakeDriver',
first_arg=False)
obj = importutils.import_object('tests.fake.FakeDriver',
first_arg=False)
self.assertEqual(obj.__class__.__name__, 'FakeDriver')
def test_import_object_required_arg_not_present(self):
# arg 1 isn't optional here
self.assertRaises(TypeError, importutils._import_object,
self.assertRaises(TypeError, importutils.import_object,
'tests.fake.FakeDriver2')
def test_import_object_required_arg_present(self):
obj = importutils._import_object('tests.fake.FakeDriver2',
first_arg=False)
obj = importutils.import_object('tests.fake.FakeDriver2',
first_arg=False)
self.assertEqual(obj.__class__.__name__, 'FakeDriver2')
# namespace tests
def test_import_object_ns_optional_arg_not_present(self):
obj = importutils._import_object_ns('tests', 'fake.FakeDriver')
obj = importutils.import_object_ns('tests', 'fake.FakeDriver')
self.assertEqual(obj.__class__.__name__, 'FakeDriver')
def test_import_object_ns_optional_arg_present(self):
obj = importutils._import_object_ns('tests', 'fake.FakeDriver',
first_arg=False)
obj = importutils.import_object_ns('tests', 'fake.FakeDriver',
first_arg=False)
self.assertEqual(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,
self.assertRaises(TypeError, importutils.import_object_ns,
'tests', 'fake.FakeDriver2')
def test_import_object_ns_required_arg_present(self):
obj = importutils._import_object_ns('tests', 'fake.FakeDriver2',
first_arg=False)
obj = importutils.import_object_ns('tests', 'fake.FakeDriver2',
first_arg=False)
self.assertEqual(obj.__class__.__name__, 'FakeDriver2')
# namespace tests
def test_import_object_ns_full_optional_arg_not_present(self):
obj = importutils._import_object_ns('tests2',
'tests.fake.FakeDriver')
obj = importutils.import_object_ns('tests2',
'tests.fake.FakeDriver')
self.assertEqual(obj.__class__.__name__, 'FakeDriver')
def test_import_object_ns_full_optional_arg_present(self):
obj = importutils._import_object_ns('tests2',
'tests.fake.FakeDriver',
first_arg=False)
obj = importutils.import_object_ns('tests2',
'tests.fake.FakeDriver',
first_arg=False)
self.assertEqual(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,
self.assertRaises(TypeError, importutils.import_object_ns,
'tests2', 'tests.fake.FakeDriver2')
def test_import_object_ns_full_required_arg_present(self):
obj = importutils._import_object_ns('tests2',
'tests.fake.FakeDriver2',
first_arg=False)
obj = importutils.import_object_ns('tests2',
'tests.fake.FakeDriver2',
first_arg=False)
self.assertEqual(obj.__class__.__name__, 'FakeDriver2')
def test_import_object(self):
dt = importutils._import_object('datetime.time')
dt = importutils.import_object('datetime.time')
self.assertTrue(isinstance(dt, sys.modules['datetime'].time))
def test_import_object_with_args(self):
dt = importutils._import_object('datetime.datetime', 2012, 4, 5)
dt = importutils.import_object('datetime.datetime', 2012, 4, 5)
self.assertTrue(isinstance(dt, sys.modules['datetime'].datetime))
self.assertEqual(dt, datetime.datetime(2012, 4, 5))