Commit c26c1d43 authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Remove validate function + add fullorempty rule

parent 90e4a92d
......@@ -72,8 +72,6 @@ def parse_parameters_rules(wf_instance):
# Check rules:
try:
wf_instance.check_parameters_rules(user_params)
wf_instance.set_parameters(user_params)
wf_instance.validate()
except Exception as e:
arg_sub_parser.error(e)
......
......@@ -23,12 +23,13 @@ from jflow.parameter import InputFile, InputFileList, InputDirectory, MultiParam
class SimpleRule (ABC):
def __init__(self, user_args, wf_instance, src_arg, all_files=None):
def __init__(self, user_args, wf_instance, src_arg, nb_rows, all_files=None):
self.user_args = user_args
self.wf_instance = wf_instance
self.parameter_name = src_arg
self.parameter_value = user_args[src_arg]
self.all_files = all_files
self.nb_rows = nb_rows
if ">" not in src_arg:
self.wf_parameter = getattr(wf_instance, src_arg)
else:
......@@ -47,8 +48,8 @@ class SimpleRule (ABC):
class LinkRule (SimpleRule):
def __init__(self, user_args, wf_instance, src_arg, targets_args, all_files=None):
SimpleRule.__init__(self, user_args, wf_instance, src_arg, all_files)
def __init__(self, user_args, wf_instance, src_arg, targets_args, nb_rows, all_files=None):
SimpleRule.__init__(self, user_args, wf_instance, src_arg, nb_rows, all_files)
self.targets_args = targets_args
@abstractmethod
......@@ -57,8 +58,8 @@ class LinkRule (SimpleRule):
class ConditionalRule(SimpleRule):
def __init__(self, user_args, wf_instance, src_arg, conditions, which, all_files=None):
SimpleRule.__init__(self, user_args, wf_instance, src_arg, all_files)
def __init__(self, user_args, wf_instance, src_arg, conditions, which, nb_rows, all_files=None):
SimpleRule.__init__(self, user_args, wf_instance, src_arg, nb_rows, all_files)
self.conditions = conditions
# Test condition is raised:
......
......@@ -395,14 +395,9 @@ class JFlowServer (object):
kwargs_modified[param].append(multi_sub_params[param][sub_param])
workflow = self.wfmanager.get_workflow_by_class(kwargs_modified["workflow_class"])
try:
workflow.set_parameters(kwargs_modified)
except Exception:
pass
try:
workflow.check_parameters_rules(kwargs_modified)
workflow.validate()
except Exception as e:
return {"status": 1, "content": str(e)}
return {"status": 0}
......
......@@ -328,7 +328,7 @@ class Workflow(threading.Thread):
raise Exception( "Unknown class '" + parameter.__class__.__name__ + "' for parameter.")
return new_param
def set_parameters(self, args):
def _set_parameters(self, args):
parameters = self.get_parameters()
for param in parameters:
new_param = None
......@@ -871,7 +871,7 @@ class Workflow(threading.Thread):
self._postprocess_status = self.STATUS_PENDING
self.end_time = None
# if some args are provided, let's fill the parameters
self.set_parameters(self.args)
self._set_parameters(self.args)
self._serialize()
# if pre_processing has not been done yet
if self.__step == 0:
......@@ -1211,7 +1211,7 @@ class Workflow(threading.Thread):
self.globals[symbol] = getattr(m, symbol)
@staticmethod
def _load_sub_parameters(sub_parameters, parameter, multiple_values=False):
def _load_sub_parameters(parameters_used, sub_parameters, parameter, multiple_values=False):
"""
For Multiple parameters, get sub parameters given by the user
@param sub_parameters: sub parameters
......@@ -1219,12 +1219,11 @@ class Workflow(threading.Thread):
@param multiple_values:
@return:
"""
parameters_used = {}
for sub_parameter in sub_parameters:
sp_name = sub_parameter[0].replace("-", "_")
param_name = parameter.name + ">" + sp_name
if multiple_values:
if param_name not in parameters_used:
if param_name not in parameters_used.keys():
parameters_used[param_name] = []
parameters_used[param_name].append(sub_parameter[1])
else:
......@@ -1237,16 +1236,18 @@ class Workflow(threading.Thread):
rules = {}
all_files = [] # Store all given files, used later
parameters_used = {} # Parameters given by the user
nb_rows = {} # Number of rows for each MultipleParameterList
for parameter in parameters:
# Get sub-parameters of MultiParameter or MultiParameterLists:
if isinstance(parameter, MultiParameterList) or isinstance(parameter, MultiParameter):
parameters_used[parameter.name] = {}
# Get parameters given by the user:
if isinstance(parameter, MultiParameterList):
nb_rows[parameter.name] = len(user_params[parameter.name])
for rows in user_params[parameter.name]:
parameters_used.update(self._load_sub_parameters(rows, parameter, True))
parameters_used.update(self._load_sub_parameters(parameters_used, rows, parameter, True))
else: # MultiParameter
parameters_used.update(self._load_sub_parameters(user_params[parameter.name], parameter))
parameters_used.update(self._load_sub_parameters(parameters_used, user_params[parameter.name], parameter))
# Get all parameters and check rules for the ones given by the user:
for sub_param in parameter.sub_parameters:
......@@ -1274,15 +1275,16 @@ class Workflow(threading.Thread):
for src_arg, arg_rules in rules.items():
for arg_rule in arg_rules:
if arg_rule != "":
self.check_parameter_rule(arg_rule, src_arg, parameters_used, all_files)
self.check_parameter_rule(arg_rule, src_arg, parameters_used, all_files, nb_rows)
def check_parameter_rule(self, rule: str, src_arg: str, user_args: dict, all_files: list):
def check_parameter_rule(self, rule: str, src_arg: str, user_args: dict, all_files: list, nb_rows: dict):
"""
Check a parameter rule
@param rule: string describing the rule
@param src_arg: the argument containing the rule (string)
@param user_args: all arguments given by the user, with their values: {parameter_name1: value, ...}
@param all_files: all files given by the user
@param nb_rows: for each MultipleParameterList, get number of rows
"""
###############################################################
......@@ -1317,7 +1319,7 @@ class Workflow(threading.Thread):
# Launch validator:
if issubclass(validator_class, j_rules.ConditionalRule): # Check the rule is the same special rule
validator = validator_class(user_args, self, src_arg, conditions, which, all_files)
validator = validator_class(user_args, self, src_arg, conditions, which, nb_rows, all_files)
validator.check()
else:
raise Exception("Rule is not a conditional link rule: " + name)
......@@ -1338,7 +1340,7 @@ class Workflow(threading.Thread):
# Launch validator
if issubclass(validator_class, j_rules.LinkRule): # Check the rule is the same special rule
validator = validator_class(user_args, self, src_arg, targets, all_files)
validator = validator_class(user_args, self, src_arg, targets, nb_rows, all_files)
validator.check()
else:
raise Exception("Rule is not a link rule: " + name)
......@@ -1355,10 +1357,7 @@ class Workflow(threading.Thread):
# Launch validator:
if issubclass(validator_class, j_rules.SimpleRule): # Check it's a simple rule
validator = validator_class(user_args, self, src_arg, all_files)
validator = validator_class(user_args, self, src_arg, nb_rows, all_files)
validator.check()
else:
raise Exception("Rule is not a simple rule: " + name)
def validate(self):
pass
......@@ -45,3 +45,23 @@ class UniqueAll(SimpleRule):
else:
print("\033[93mWarning: rule unique_all is ignored: " + self.parameter_name +
" is not an input file or input file list\033[0m")
class FullOrEmpty(SimpleRule):
def check(self):
if self.parameter_name.index(">") > -1:
p_name_parts = self.parameter_name.split(">")
parent_param = p_name_parts[0]
child_param = p_name_parts[1]
if parent_param in self.nb_rows:
nb_arg_rows = len(self.user_args[self.parameter_name])
if 0 < nb_arg_rows < self.nb_rows[parent_param]:
raise Exception("Argument " + child_param + " must be filled for each " + parent_param +
" or for none")
else:
print("\033[93mWarning: ignoring rule FullOrEmpty on parameter " + self.parameter_name + ": parameter " +
"is not in a MultipleParameterList\033[0m")
else:
print("\033[93mWarning: ignoring rule FullOrEmpty on parameter " + self.parameter_name + ": parameter is " +
"not in a MultipleParameterList\033[0m")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment