Commit 1562506c authored by Jerome Mariette's avatar Jerome Mariette
Browse files

help ok

parent ea23afbe
......@@ -35,6 +35,12 @@ class MultipleParameters(object):
self.required = required
self.__name__ = "MultipleParameters"
def get_help(self):
help = " ("
for flag in self.types.keys():
help += flag + "=<" + self.types[flag].__name__.upper() + ">, "
return help[:-2] + ")"
def __call__(self, arg):
parts = arg.split("=")
if not self.types.has_key(parts[0]):
......@@ -189,37 +195,16 @@ class AbstractParameter(object):
if display_name == None:
self.display_name = name.replace("_", " ").title()
else: self.display_name = display_name
self.required = str(required).lower() in ("yes", "y", "true", "t", "1")
self.required = required
self.choices = choices
if len(self.sub_parameters) > 0:
self.nargs = "+"
if type == "date":
self.type = date
elif type == "multiple":
sub_param_hash, sub_param_types, sub_param_names, sub_param_required, sub_param_choices, sub_param_excludes = {}, [], [], [], {}, {}
sub_param_default, sub_param_actions = {}, {}
for sub_param in self.sub_parameters:
try: sub_type = sub_param.type
except: sub_type = types.StringType
sub_param_hash[sub_param.flag] = sub_type
sub_param_names.append(sub_param.flag)
sub_param_types.append(sub_type)
sub_param_choices[sub_param.flag] = sub_param.choices
sub_param_default[sub_param.flag] = sub_param.default
sub_param_actions[sub_param.flag] = sub_param.action
if sub_param.group.startswith("exclude-"):
if sub_param.group in sub_param_excludes.keys():
sub_param_excludes[sub_param.group].append(sub_param.flag)
else:
sub_param_excludes[sub_param.group] = [sub_param.flag]
if sub_param.required: sub_param_required.append(sub_param.flag)
self.type = MultipleParameters(sub_param_hash, sub_param_required, sub_param_choices, sub_param_excludes, sub_param_default, sub_param_actions)
if self.action == "append":
self.action = MiltipleAppendAction
else:
self.action = MiltipleAction
self.help += " (" + " ".join([cname + "=<" + ctype.__name__.upper() + ">" for ctype, cname in zip(sub_param_types, sub_param_names)]) + ")"
self.type = "multiple"
elif isinstance(type, types.FunctionType):
self.type = type
else:
......@@ -227,18 +212,12 @@ class AbstractParameter(object):
except: self.type = types.StringType
self.default = default
if type == "date" and not self.default:
if self.type == "date" and not self.default:
today = datetime.date.today()
self.default = today.strftime('%d/%m/%Y')
elif self.type == types.BooleanType :
if self.default: self.default = str(self.default).lower() in ("true", "t", "1")
else: self.default = True
elif self.action == "append" or self.action == MiltipleAppendAction:
self.default = []
elif type == "multiple":
self.default = {}
for param in self.sub_parameters:
self.default[param.name] = param.default
def export_to_argparse(self):
if self.type == types.BooleanType and str(self.default).lower() in ("false", "f", "0"):
......@@ -259,6 +238,27 @@ class AbstractParameter(object):
def get_type(self):
return self.type.__name__
def add_sub_parameter(self, param):
param_flag = param.flag[2:]
if self.type == "multiple":
self.type = MultipleParameters({param_flag: param.type}, [param.required],
{param_flag: param.choices}, {}, {param_flag: param.default},
{param_flag: param.action})
if self.action == "append":
self.action = MiltipleAppendAction
else:
self.action = MiltipleAction
self.global_help = self.help
self.help = self.global_help + " (" + param_flag + "=<" + param.type.__name__.upper() + ">)"
elif self.type.__class__ == MultipleParameters:
self.type.types[param_flag] = param.type
self.type.choices[param_flag] = param.choices
self.type.default[param_flag] = param.default
self.type.actions[param_flag] = param.action
self.type.required.append(param.required)
self.help = self.global_help + self.type.get_help()
self.sub_parameters.append(param)
class AbstractFile(object):
def __init__(self, file_format="any"):
......@@ -269,14 +269,16 @@ class AbstractFile(object):
class MultiParameter(dict, AbstractParameter):
def __init__(self, name, help, required=False, flag=None, group="default", display_name=None):
AbstractParameter.__init__(self, name, help, required=required, flag=flag, group=group, display_name=display_name)
AbstractParameter.__init__(self, name, help, required=required, type="multiple", flag=flag, group=group,
display_name=display_name)
return dict.__init__(self, {})
class MultiParameterList(list, AbstractParameter):
def __init__(self, name, help, required=False, flag=None, group="default", display_name=None):
AbstractParameter.__init__(self, name, help, required=required, flag=flag, group=group, display_name=display_name)
AbstractParameter.__init__(self, name, help, required=required, type="multiple", flag=flag,
action="append", group=group, display_name=display_name)
return list.__init__(self, [])
......
......@@ -131,7 +131,7 @@ class Workflow(threading.Thread):
# if this input should be added to a particular parameter
if add_to:
try:
self.__getattribute__(add_to).sub_parameters.append(new_param)
self.__getattribute__(add_to).add_sub_parameter(new_param)
except: pass
# otherwise, add it to the class itself
else:
......@@ -151,7 +151,7 @@ class Workflow(threading.Thread):
# if this input should be added to a particular parameter
if add_to:
try:
self.__getattribute__(add_to).sub_parameters.append(new_param)
self.__getattribute__(add_to).add_sub_parameter(new_param)
except: pass
# otherwise, add it to the class itself
else:
......@@ -179,7 +179,7 @@ class Workflow(threading.Thread):
# if this input should be added to a particular parameter
if add_to:
try:
self.__getattribute__(add_to).sub_parameters.append(new_param)
self.__getattribute__(add_to).add_sub_parameter(new_param)
except: pass
# otherwise, add it to the class itself
else:
......@@ -189,22 +189,115 @@ class Workflow(threading.Thread):
def _set_parameters(self, args):
parameters = self.get_parameters()
for param in parameters:
try: args[param.name] = args[param.name].encode('ascii','ignore')
except: pass
if param.__class__ == Parameter:
new_param = Parameter(param.name, param.help, default=args[param.name], type=param.type, choices=param.choices,
required=param.required, flag=param.flag, sub_parameters=param.sub_parameters,
group=param.group, display_name=param.display_name)
self.__setattr__(param.name, new_param)
elif param.__class__ == InputFile:
new_param = InputFile(param.name, param.help, file_format=param.file_format, default=args[param.name], type=param.type,
choices=param.choices, required=param.required, flag=param.flag,
group=param.group, display_name=param.display_name)
self.__setattr__(param.name, new_param)
elif param.__class__ == ParameterList or param.__class__ == InputFileList:
self.__getattribute__(param.name).extend(args[param.name])
if args[param.name]:
try: args[param.name] = args[param.name].encode('ascii','ignore')
except: pass
if param.__class__ == Parameter:
new_param = Parameter(param.name, param.help, default=args[param.name], type=param.type, choices=param.choices,
required=param.required, flag=param.flag, group=param.group, display_name=param.display_name)
self.__setattr__(param.name, new_param)
elif param.__class__ == InputFile:
new_param = InputFile(param.name, param.help, file_format=param.file_format, default=args[param.name], type=param.type,
choices=param.choices, required=param.required, flag=param.flag,
group=param.group, display_name=param.display_name)
self.__setattr__(param.name, new_param)
elif param.__class__ == ParameterList or param.__class__ == InputFileList:
self.__getattribute__(param.name).extend(args[param.name])
"""
def _get_from_config(self, parameters_section):
reader = ConfigParser()
reader.read(self._get_property_path())
parameters, param_order = [], []
try:
params, sub_order, excludes, hash_params = {}, {}, [], {}
# first grab all parameters name
for param in reader.items(parameters_section):
cvalue = reader.get(parameters_section, param[0])
cparam = param[0].split(".")[0]
ckey = ".".join(param[0].split(".")[1:])
if len(ckey.split(".")) > 1:
if sub_order.has_key(cparam):
if ckey.split(".")[0] not in sub_order[cparam]: sub_order[cparam].append(ckey.split(".")[0])
else:
sub_order[cparam] =[ckey.split(".")[0]]
if not params.has_key(cparam):
params[cparam] = {ckey: cvalue}
else:
params[cparam][ckey] = cvalue
if cparam not in param_order: param_order.append(cparam)
for param_name in param_order:
sub_params, current_params, sub_objs, sub_excludes, hash_sub_params = {}, {}, [], [], {}
for param in params[param_name]:
sub_params_values = param.split(".")
if len(sub_params_values) == 2:
# TODO: handle exclusive for sub parameter
key = None
if sub_params_values[1] == "name":
key = "display_name"
else:
key = sub_params_values[1]
if key == "exclude":
found = False
for exclude_group in sub_excludes:
if sub_params_values[0] in exclude_group:
exclude_group.append(params[param_name][param])
found = True
elif params[param_name][param] in exclude_group:
exclude_group.append(sub_params_values[0])
found = True
if not found: sub_excludes.append([sub_params_values[0], params[param_name][param]])
else:
if not sub_params.has_key(sub_params_values[0]):
sub_params[sub_params_values[0]] = {key: params[param_name][param]}
sub_params[sub_params_values[0]]["name"] = sub_params_values[0]
else:
sub_params[sub_params_values[0]][key] = params[param_name][param]
else:
if param == "name": current_params["display_name"] = params[param_name][param]
elif param == "exclude":
found = False
for exclude_group in excludes:
if param_name in exclude_group:
exclude_group.append(params[param_name][param])
found = True
elif params[param_name][param] in exclude_group:
exclude_group.append(param_name)
found = True
if not found: excludes.append([param_name, params[param_name][param]])
else: current_params[param] = params[param_name][param]
current_params["name"] = param_name
if sub_order.has_key(param_name):
for sub_param in sub_order[param_name]:
sub = Parameter(**sub_params[sub_param])
hash_sub_params[sub.name] = sub
sub_objs.append(sub)
# overwrite the group to exclude some parameters
for i, exclude_group in enumerate(sub_excludes):
group_name = "exclude-"+str(i)
for sparam in set(exclude_group):
hash_sub_params[sparam].group = group_name
current_params["sub_parameters"] = sub_objs
oparam = Parameter(**current_params)
parameters.append(oparam)
hash_params[oparam.name] = oparam
# overwrite the group to exclude some parameters
for i, exclude_group in enumerate(excludes):
group_name = "exclude-"+str(i)
for param in set(exclude_group):
hash_params[param].group = group_name
except:
pass
# finally add the metadata parameter
metadata = Parameter(name="metadata", flag="--metadata", help="Which metadata should be linked to this workflow",
action="append", display_name="Metadata")
parameters.append(metadata)
return [reader.get("global", "name"),
reader.get("global", "description"),
parameters]
def _extend_and_format_args(self, parameters, args):
extended_args = {}
for param in parameters:
......
......@@ -33,7 +33,9 @@ class Alignment (Workflow):
self.add_multiple_parameter("test", "toto est dnas le gazon")
self.add_input_file("toto", "Whices should be used", required=True, is_list=True, add_to="test")
self.add_input_file("tot", "Whould be used", is_list=True, add_to="test")
self.add_input_file("tot", "Whould be used", is_list=True, type="localfile", add_to="test")
#self.exclude_parameters("read_1", "read_2")
# prochaine etape:
# - tester les types multiples
......@@ -44,6 +46,9 @@ class Alignment (Workflow):
print ">>>>>>>>>>>>>>>>>>>> ", self.reference_genome
print ">>>>>>>>>>>>>>>>>>>> ", self.read_1
print ">>>>>>>>>>>>>>>>>>>> ", self.read_2
print ">>>>>>>>>>>>>>>>>>>> ", self.test
print "gg ", self.test.help
# index the reference genome
#bwaindex = self.add_component("BWAIndex", [self.args["reference_genome"]])
......
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