remove search_conf_item

This commit is contained in:
debugtalk
2018-08-09 00:49:45 +08:00
parent db3e1a2ae9
commit cc922c8619
5 changed files with 64 additions and 75 deletions

View File

@@ -268,6 +268,39 @@ def load_debugtalk_module(start_path=None):
return load_python_module(imported_module)
def get_module_item(module_mapping, item_type, item_name):
""" get expected function or variable from module mapping.
Args:
module_mapping(dict): module mapping with variables and functions.
{
"variables": {},
"functions": {}
}
item_type(str): "functions" or "variables"
item_name(str): function name or variable name
Returns:
object: specified variable or function object.
Raises:
exceptions.FunctionNotFound: If specified function not found in module mapping
exceptions.VariableNotFound: If specified variable not found in module mapping
"""
try:
return module_mapping[item_type][item_name]
except KeyError:
err_msg = "{} not found in debugtalk.py module!\n".format(item_name)
err_msg += "module mapping: {}".format(module_mapping)
if item_type == "functions":
raise exceptions.FunctionNotFound(err_msg)
else:
raise exceptions.VariableNotFound(err_msg)
###############################################################################
## suite loader
###############################################################################

View File

@@ -294,7 +294,7 @@ class TestcaseParser(object):
self.functions = functions
def _get_bind_item(self, item_type, item_name):
if item_type == "function":
if item_type == "functions":
if item_name in self.functions:
return self.functions[item_name]
@@ -307,7 +307,7 @@ class TestcaseParser(object):
except (NameError, TypeError):
# is not builtin function, continue to search
pass
elif item_type == "variable":
elif item_type == "variables":
if item_name in self.variables:
return self.variables[item_name]
else:
@@ -315,16 +315,17 @@ class TestcaseParser(object):
try:
assert self.file_path is not None
return utils.search_conf_item(self.file_path, item_type, item_name)
debugtalk_module = loader.load_debugtalk_module(self.file_path)
return loader.get_module_item(debugtalk_module, item_type, item_name)
except (AssertionError, exceptions.FunctionNotFound):
raise exceptions.ParamsError(
"{} is not defined in bind {}s!".format(item_name, item_type))
def get_bind_function(self, func_name):
return self._get_bind_item("function", func_name)
return self._get_bind_item("functions", func_name)
def get_bind_variable(self, variable_name):
return self._get_bind_item("variable", variable_name)
return self._get_bind_item("variables", variable_name)
def parameterize(self, csv_file_name, fetch_method="Sequential"):
parameter_file_path = os.path.join(

View File

@@ -148,7 +148,7 @@ def deep_update_dict(origin_dict, override_dict):
return origin_dict
def get_imported_module_from_file(file_path):
""" import module from python file path and return imported module
""" DEPRECATED: import module from python file path and return imported module
"""
if is_py3:
imported_module = importlib.machinery.SourceFileLoader(
@@ -160,44 +160,6 @@ def get_imported_module_from_file(file_path):
return imported_module
def filter_module(module, filter_type):
""" filter functions or variables from import module
@params
module: imported module
filter_type: "function" or "variable"
"""
filter_type = validator.is_function if filter_type == "function" else validator.is_variable
module_functions_dict = dict(filter(filter_type, vars(module).items()))
return module_functions_dict
def search_conf_item(start_path, item_type, item_name):
""" search expected function or variable recursive upward
@param
start_path: search start path
item_type: "function" or "variable"
item_name: function name or variable name
"""
dir_path = os.path.dirname(os.path.abspath(start_path))
target_file = os.path.join(dir_path, "debugtalk.py")
if os.path.isfile(target_file):
imported_module = get_imported_module_from_file(target_file)
items_dict = filter_module(imported_module, item_type)
if item_name in items_dict:
return items_dict[item_name]
else:
return search_conf_item(dir_path, item_type, item_name)
if dir_path == start_path:
# system root path
err_msg = "{} not found in recursive upward path!".format(item_name)
if item_type == "function":
raise exceptions.FunctionNotFound(err_msg)
else:
raise exceptions.VariableNotFound(err_msg)
return search_conf_item(dir_path, item_type, item_name)
def lower_dict_keys(origin_dict):
""" convert keys in dict to lower case
e.g.