Commit 9401c25e authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Update display

parent 58773fa7
......@@ -57,14 +57,24 @@ class JflowArgumentParser (argparse.ArgumentParser):
return new_arg_strings
def _load_sub_parameters(items, parameter):
def _load_sub_parameters(sub_parameters, parameter, multiple_values=False):
"""
For Multiple parameters, get sub parameters given by the user
@param sub_parameters: sub parameters
@param parameter: parent parameter
@param multiple_values:
@return:
"""
parameters_used = {}
for item in items:
p_item = item[0].replace("-", "_")
param_name = parameter.name + ">" + p_item
if param_name not in parameters_used:
parameters_used[param_name] = []
parameters_used[param_name].append(item[1])
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:
parameters_used[param_name] = []
parameters_used[param_name].append(sub_parameter[1])
else:
parameters_used[param_name] = sub_parameter[1]
return parameters_used
......@@ -93,7 +103,7 @@ def parse_parameters_rules(wf_instance):
# Get parameters given by the user:
if isinstance(parameter, MultiParameterList):
for rows in user_params[parameter.name]:
parameters_used = {**parameters_used, **_load_sub_parameters(rows, parameter)}
parameters_used = {**parameters_used, **_load_sub_parameters(rows, parameter, True)}
else: # MultiParameter
parameters_used = {**parameters_used, **_load_sub_parameters(user_params[parameter.name], parameter)}
......
......@@ -17,13 +17,27 @@
from abc import ABC, abstractmethod
from jflow.parameter import InputFile, InputFileList, InputDirectory, MultiParameter, MultiParameterList
class SimpleRule (ABC):
def __init__(self, user_args, wf_instance, src_arg, all_files=None):
self.user_args = user_args
self.wf_instance = wf_instance
self.src_arg = src_arg
self.parameter_name = src_arg
self.parameter_value = user_args[src_arg]
self.all_files = all_files
if ">" not in src_arg:
self.wf_parameter = getattr(wf_instance, src_arg)
else:
self.wf_parameter = getattr(wf_instance, src_arg[:src_arg.index(">")]).\
get_sub_parameters_by_name()[src_arg[src_arg.index(">")+1:]][0]
self.is_file_list = isinstance(self.wf_parameter, InputFileList)
self.is_a_file = isinstance(self.wf_parameter, InputFile)
self.is_file = self.is_a_file or self.is_file_list
self.is_directory = isinstance(self.wf_parameter, InputDirectory)
self.is_file_or_directory = self.is_file or self.is_directory
@abstractmethod
def check(self):
......@@ -45,6 +59,24 @@ class ConditionalLinkRule(LinkRule):
LinkRule.__init__(self, user_args, wf_instance, src_arg, targets_args, all_files)
self.conditions = conditions
# Check parameter is correct:
self.check_type_argument()
# Test condition is raised:
self.condition_raised = None
for condition in self.conditions:
if (condition[0] == "!" and str(self.user_args[src_arg]) != str(condition[1:])) or (condition[0] != "!" and
str(self.user_args[src_arg]) == str(condition)):
self.condition_raised = condition
break
def check_type_argument(self):
if isinstance(self.wf_parameter, InputFile) or isinstance(self.wf_parameter, InputFileList) or \
isinstance(self.wf_parameter, InputDirectory) or isinstance(self.wf_parameter, MultiParameter) or \
isinstance(self.wf_parameter, MultiParameterList):
raise Exception("Rule " + str(self.__class__.__name__) + " (ConditionalLinkRule) is not available on files "
"and directories parameters or multiple parameters")
@abstractmethod
def check(self):
pass
......@@ -16,7 +16,6 @@
#
from jflow.rules import SimpleRule, LinkRule, ConditionalLinkRule
from jflow.parameter import InputFile, InputFileList, InputDirectory, MultiParameter, MultiParameterList
# Define your rules check below
......@@ -24,38 +23,27 @@ from jflow.parameter import InputFile, InputFileList, InputDirectory, MultiParam
class Unique(SimpleRule):
def check(self):
if ">" not in self.src_arg:
wf_parameter = getattr(self.wf_instance, self.src_arg)
if self.is_file_list:
for file in self.parameter_value:
if self.parameter_value.count(file) > 1:
raise Exception("the file \"" + file + "\" in parameter \"" + self.parameter_name +
"\" is duplicated")
else:
wf_parameter = getattr(self.wf_instance, self.src_arg[:self.src_arg.index(">")]).\
get_sub_parameters_by_name(self.src_arg[self.src_arg.index(">")+1:])
if isinstance(wf_parameter, InputFileList):
for file in self.user_args[self.src_arg]:
if self.user_args[self.src_arg].count(file) > 1:
raise Exception("the file \"" + file + "\" in parameter \"" + self.src_arg + "\" is duplicated")
else:
print("\033[93mWarning: rule unique is ignored: " + self.src_arg + " is not an input file list\033[0m")
print("\033[93mWarning: rule unique is ignored: " + self.parameter_name +
" is not an input file list\033[0m")
class UniqueAll(SimpleRule):
def check(self):
if ">" not in self.src_arg:
wf_parameter = getattr(self.wf_instance, self.src_arg)
else:
wf_parameter = getattr(self.wf_instance, self.src_arg[:self.src_arg.index(">")]).\
get_sub_parameters_by_name()[self.src_arg[self.src_arg.index(">")+1:]][0]
if isinstance(wf_parameter, InputFile) or isinstance(wf_parameter,
InputFileList):
files = self.user_args[self.src_arg]
if type(files) == str:
files = [files]
if self.is_file:
files = self.parameter_value if type(self.parameter_value) == list else [self.parameter_value]
for file in files:
if self.all_files.count(file) > 1:
raise Exception("the file \"" + file + "\" from parameter \"" + self.src_arg + "\" is given "
raise Exception("the file \"" + file + "\" from parameter \"" + self.parameter_name + "\" is given "
"several times (on this or another argument)")
else:
print("\033[93mWarning: rule unique_all is ignored: " + self.src_arg +
print("\033[93mWarning: rule unique_all is ignored: " + self.parameter_name +
" is not an input file or input file list\033[0m")
......@@ -63,71 +51,40 @@ class Exclude(LinkRule):
def check(self):
for exclude in self.targets_args:
if exclude in self.user_args and self.user_args[exclude] is not None:
raise Exception("arguments " + self.src_arg + " and " + exclude + " are mutually excluded")
if exclude in self.user_args and self.user_args[exclude] is not None: # The target is found
raise Exception("arguments " + self.parameter_name + " and " + exclude + " are mutually excluded")
class ExcludeIf(ConditionalLinkRule):
def check(self):
# Rule valid anony for non input files/directories:
if ">" not in self.src_arg:
wf_parameter = getattr(self.wf_instance, self.src_arg)
else:
wf_parameter = getattr(self.wf_instance, self.src_arg[:self.src_arg.index(">")]).\
get_sub_parameters_by_name(self.src_arg[self.src_arg.index(">")+1:])
if not isinstance(wf_parameter, InputFile) and \
not isinstance(wf_parameter, InputFileList) and not isinstance(wf_parameter, InputDirectory) and \
not isinstance(wf_parameter, MultiParameter) and not isinstance(wf_parameter, MultiParameterList):
condition_raised = None
for condition in self.conditions:
if (condition[0] == "!" and self.user_args[self.src_arg] != condition[1:]) or (condition[0] != "!" and
str(self.user_args[self.src_arg]) == str(condition)):
condition_raised = condition
break
if condition_raised:
validator = Exclude(self.user_args, self.wf_instance, self.src_arg, self.targets_args, self.all_files)
try:
validator.check()
except Exception as e:
raise Exception("Argument " + self.src_arg + " is set to " + condition_raised + ": " + str(e))
else:
raise Exception("Rule ExcludeIf is not available on files and directories parameters "
"or multiples parameters")
if self.condition_raised is not None:
validator = Exclude(self.user_args, self.wf_instance, self.parameter_name, self.targets_args, self.all_files)
try:
validator.check()
except Exception as e:
raise Exception("Argument " + self.parameter_name + " is set to " + self.condition_raised + ": " +
str(e))
class ToBeRequired(LinkRule):
def check(self):
for require in self.targets_args:
if require not in self.user_args or (require in self.user_args and self.user_args[require] is None):
raise Exception("the argument " + self.src_arg + " require argument " + require + " to be defined")
if require not in self.user_args or (require in self.user_args and self.user_args[require] is None): # The target is not found
raise Exception("the argument " + self.parameter_name + " require argument " + require +
" to be defined")
class ToBeRequiredIf(ConditionalLinkRule):
def check(self):
if ">" not in self.src_arg:
wf_parameter = getattr(self.wf_instance, self.src_arg)
else:
wf_parameter = getattr(self.wf_instance, self.src_arg[:self.src_arg.index(">")]).\
get_sub_parameters_by_name(self.src_arg[self.src_arg.index(">")+1:])
# Rule valid only for non input files/directories:
if not isinstance(wf_parameter, InputFile) and \
not isinstance(wf_parameter, InputFileList) and not isinstance(wf_parameter, InputDirectory) and \
not isinstance(wf_parameter, MultiParameter) and not isinstance(wf_parameter, MultiParameterList):
condition_raised = None
for condition in self.conditions:
if (condition[0] == "!" and self.user_args[self.src_arg] != condition[1:]) or (condition[0] != "!" and
str(self.user_args[self.src_arg]) == str(condition)):
condition_raised = condition
break
if condition_raised:
validator = ToBeRequired(self.user_args, self.wf_instance, self.src_arg, self.targets_args, self.all_files)
try:
validator.check()
except Exception as e:
raise Exception("Argument " + self.src_arg + " is set to " + condition_raised + ": " + str(e))
else:
raise Exception("Rule ToBeRequiredIf is not available on files and directories parameters or multiple "
"parameters")
if self.condition_raised is not None:
validator = ToBeRequired(self.user_args, self.wf_instance, self.parameter_name, self.targets_args,
self.all_files)
try:
validator.check()
except Exception as e:
raise Exception("Argument " + self.parameter_name + " is set to " + self.condition_raised + ": " +
str(e))
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