Skip to content

Commit

Permalink
Move some helper functions to client.py
Browse files Browse the repository at this point in the history
Signed-off-by: Luyao Huang <[email protected]>
  • Loading branch information
LuyaoHuang committed Jan 3, 2019
1 parent 8232fba commit dfa7ecf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 70 deletions.
75 changes: 75 additions & 0 deletions depend_test_framework/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Some helpers to help build client app
"""
import itertools
import sys
import os
import yaml
import copy
import importlib

from base_class import Params


def load_template(template_file):
with open(template_file) as fp:
data = yaml.load(fp)

cases = data['case']
common_params = data['params']
for case in cases:
params = Params(common_params)
if 'params' in case.keys():
params.update(case['params'])
modules = list(load_modules(case['modules']))
doc_modules = list(load_modules(case['doc-modules']))
test_objs = list(load_objs(case['test_objs']))
if 'params_matrix' in case.keys():
for extra_params in full_permutations(case['params_matrix']):
new_params = Params(params)
new_params.update(extra_params)
yield new_params, modules, doc_modules, copy.deepcopy(test_objs)
else:
yield params, modules, doc_modules, copy.deepcopy(test_objs)


def load_modules(modules_list, module_path='.'):
# TODO
cmd_folder = os.path.realpath(module_path)
sys.path.insert(0, cmd_folder)
for module in modules_list:
yield importlib.import_module(module)


def load_objs(test_objs_list):
# TODO: only support 2 layer
for test_obj in test_objs_list:
tmp_module = importlib.import_module(test_obj.split('.')[0])
yield getattr(tmp_module, test_obj.split('.')[1], None)


def full_permutations(params_matrix):
if isinstance(params_matrix, dict):
val_pt = []
keys = params_matrix.keys()
for key in keys:
ret = full_permutations(params_matrix[key])
if isinstance(ret, list):
val_pt.append(ret)
else:
val_pt.append([ret])
pmt = itertools.product(*val_pt)
return [Params(zip(keys, i)) for i in pmt]
elif isinstance(params_matrix, list):
ret = []
for data in params_matrix:
tmp = full_permutations(data)
if isinstance(tmp, list):
ret.extend(tmp)
else:
ret.append(tmp)
return ret
elif isinstance(params_matrix, (str, int, float)):
return params_matrix
else:
raise Exception
74 changes: 4 additions & 70 deletions scripts/deptest
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ TODO: only use for debugging
import os
import sys
import yaml
import itertools
import copy
import logging
import contextlib
import argparse
import importlib

# Simple magic for using scripts within a source tree
BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -18,8 +18,7 @@ if os.path.isdir(os.path.join(BASEDIR, 'depend_test_framework')):

from depend_test_framework.engine import Demo
from depend_test_framework.base_class import Params
import argparse
import importlib
from depend_test_framework.client import load_template, load_modules, load_objs


def parse_cmd():
Expand Down Expand Up @@ -56,27 +55,12 @@ def parse_cmd():
return args


def load_modules(modules_list):
# TODO
cmd_folder = os.path.realpath('.')
sys.path.insert(0, cmd_folder)
for module in modules_list:
yield importlib.import_module(module)


def load_module(module_name):
cmd_folder = os.path.realpath('.')
sys.path.insert(0, cmd_folder)
return importlib.import_module(module_name)


def load_objs(test_objs_list):
# TODO: only support 2 layer
for test_obj in test_objs_list:
tmp_module = importlib.import_module(test_obj.split('.')[0])
yield getattr(tmp_module, test_obj.split('.')[1], None)


def load_params(params_str, params_conf=None):
params = Params()
if params_conf:
Expand All @@ -94,34 +78,6 @@ def load_params(params_str, params_conf=None):
return params


# Maybe a better helper
def full_permutations(params_matrix):
if isinstance(params_matrix, dict):
val_pt = []
keys = params_matrix.keys()
for key in keys:
ret = full_permutations(params_matrix[key])
if isinstance(ret, list):
val_pt.append(ret)
else:
val_pt.append([ret])
pmt = itertools.product(*val_pt)
return [Params(zip(keys, i)) for i in pmt]
elif isinstance(params_matrix, list):
ret = []
for data in params_matrix:
tmp = full_permutations(data)
if isinstance(tmp, list):
ret.extend(tmp)
else:
ret.append(tmp)
return ret
elif isinstance(params_matrix, (str, int, float)):
return params_matrix
else:
raise Exception


@contextlib.contextmanager
def change_work_path(params):
old_path = os.path.realpath(os.curdir)
Expand All @@ -139,29 +95,6 @@ def change_work_path(params):
os.chdir(old_path)


# This will be a good choise
def load_template(template_file):
with open(template_file) as fp:
data = yaml.load(fp)

cases = data['case']
common_params = data['params']
for case in cases:
params = Params(common_params)
if 'params' in case.keys():
params.update(case['params'])
modules = list(load_modules(case['modules']))
doc_modules = list(load_modules(case['doc-modules']))
test_objs = list(load_objs(case['test_objs']))
if 'params_matrix' in case.keys():
for extra_params in full_permutations(case['params_matrix']):
new_params = Params(params)
new_params.update(extra_params)
yield new_params, modules, doc_modules, copy.deepcopy(test_objs)
else:
yield params, modules, doc_modules, copy.deepcopy(test_objs)


def main():
args = parse_cmd()
if args.debug:
Expand Down Expand Up @@ -191,5 +124,6 @@ def main():
doc_modules=list(doc_modules), hook_module=hook_module)
d.run(params)


if __name__ == '__main__':
main()

0 comments on commit dfa7ecf

Please sign in to comment.