diff options
author | Tushar Gohad <tushar.gohad@intel.com> | 2014-06-22 01:13:56 -0700 |
---|---|---|
committer | Tushar Gohad <tushar.gohad@intel.com> | 2014-06-22 01:13:56 -0700 |
commit | 108a2aa1932d3e5affbf26246726306bd9bae8ae (patch) | |
tree | fd76834316c0e70adca58086c60b62fd13791c6f | |
parent | ef051208e1ac7a13f508c8810435325c1af9a5e5 (diff) | |
download | pyeclib-108a2aa1932d3e5affbf26246726306bd9bae8ae.tar.gz |
Move create_instance and other helpers to utils
Signed-off-by: Tushar Gohad <tushar.gohad@intel.com>
-rw-r--r-- | src/python/pyeclib/ec_iface.py | 40 | ||||
-rw-r--r-- | src/python/pyeclib/utils.py | 40 |
2 files changed, 41 insertions, 39 deletions
diff --git a/src/python/pyeclib/ec_iface.py b/src/python/pyeclib/ec_iface.py index dfa93e4..2da800a 100644 --- a/src/python/pyeclib/ec_iface.py +++ b/src/python/pyeclib/ec_iface.py @@ -24,8 +24,7 @@ from enum import Enum from enum import unique -import sys -import traceback +from utils import create_instance @unique @@ -70,43 +69,6 @@ class ECDriverError(Exception): return self.error_str -def import_class(import_str): - """ - Returns a class from a string that specifies a module and/or class - - :param import_str: import path, e.g. 'httplib.HTTPConnection' - :returns imported object - :raises: ImportedError if the class does not exist or the path is invalid - """ - (mod_str, separator, class_str) = import_str.rpartition('.') - try: - __import__(mod_str) - return getattr(sys.modules[mod_str], class_str) - except (ValueError, AttributeError) as e: - raise ImportError('Class %s cannot be found (%)' % - (class_str, - traceback.format_exception(*sys.exc_info()))) - - -def create_instance(import_str, *args, **kwargs): - """ - Returns instance of class which imported by import path. - - :param import_str: import path of class - :param \*args: indexed arguments for new instance - :param \*\*kwargs: keyword arguments for new instance - :returns: instance of imported class which instantiated with - arguments *args and **kwargs - """ - try: - object_class = import_class(import_str) - except Exception as e: - raise - instance = object_class(*args, **kwargs) - - return instance - - class ECDriver(object): def __init__(self, library_import_str, *args, **kwargs): diff --git a/src/python/pyeclib/utils.py b/src/python/pyeclib/utils.py index 4d1206f..74c3933 100644 --- a/src/python/pyeclib/utils.py +++ b/src/python/pyeclib/utils.py @@ -22,6 +22,9 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import sys +import traceback + def positive_int_value(param): # Returns value as a positive int or raises ValueError otherwise @@ -33,3 +36,40 @@ def positive_int_value(param): # and AssertionError for values <= 0 raise ValueError('Must be an integer > 0, not "%s".' % param) return value + + +def import_class(import_str): + """ + Returns a class from a string that specifies a module and/or class + + :param import_str: import path, e.g. 'httplib.HTTPConnection' + :returns imported object + :raises: ImportedError if the class does not exist or the path is invalid + """ + (mod_str, separator, class_str) = import_str.rpartition('.') + try: + __import__(mod_str) + return getattr(sys.modules[mod_str], class_str) + except (ValueError, AttributeError) as e: + raise ImportError('Class %s cannot be found (%)' % + (class_str, + traceback.format_exception(*sys.exc_info()))) + + +def create_instance(import_str, *args, **kwargs): + """ + Returns instance of class which imported by import path. + + :param import_str: import path of class + :param \*args: indexed arguments for new instance + :param \*\*kwargs: keyword arguments for new instance + :returns: instance of imported class which instantiated with + arguments *args and **kwargs + """ + try: + object_class = import_class(import_str) + except Exception as e: + raise + instance = object_class(*args, **kwargs) + + return instance |