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
This commit is contained in:
@@ -20,6 +20,7 @@ Import related utilities and helper functions.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
def import_class(import_str):
|
def import_class(import_str):
|
||||||
@@ -28,9 +29,10 @@ def import_class(import_str):
|
|||||||
try:
|
try:
|
||||||
__import__(mod_str)
|
__import__(mod_str)
|
||||||
return getattr(sys.modules[mod_str], class_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)' %
|
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):
|
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)
|
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):
|
def import_module(import_str):
|
||||||
"""Import a module."""
|
"""Import a module."""
|
||||||
__import__(import_str)
|
__import__(import_str)
|
||||||
|
|||||||
Reference in New Issue
Block a user