Commit 97df402f authored by Jerome Mariette's avatar Jerome Mariette
Browse files

sub params ok

parent 2a3163c3
......@@ -21,32 +21,55 @@ import datetime
from workflows.types import *
class MiltipleParameters(object):
def __init__(self, types):
self.types = types
self.index = 0
def __repr__(self):
return self.types[self.index].__name__
def __call__(self, arg):
value = self.types[self.index](arg)
self.index = (self.index + 1) % len(self.types)
return value
class Parameter(object):
"""
"""
def __init__(self, name, flag, help, default=None, type=types.StringType, choices=None,
required=False, action="store"):
def __init__(self, name, help, flag=None, default=None, type=types.StringType, choices=None,
required=False, action="store", sub_parameters=[]):
self.name = name
self.flag = flag
self.help = help
self.action = action
self.nargs = 1
self.sub_parameters = sub_parameters
if len(self.sub_parameters) > 0:
self.nargs = len(self.sub_parameters)
if type == "date":
self.type = date
elif type == "multiple":
sub_param_types, sub_param_names = [], []
for sub_param in self.sub_parameters:
sub_param_names.append(sub_param.name)
try: sub_param_types.append(sub_param.type)
except: sub_param_types.append(types.StringType)
self.type = MiltipleParameters(sub_param_types)
self.help += " | Format: " + " ".join([cname + "(" + ctype.__name__.upper() + ")" for ctype, cname in zip(sub_param_types, sub_param_names)]) + ")"
elif isinstance(type, types.FunctionType):
self.type = type
else:
try: self.type = eval(type)
except: self.type = types.StringType
self.metavar = self.type.__name__.upper()
self.required = str(required).lower() in ("yes", "y", "true", "t", "1")
self.choices = choices
if self.choices:
self.choices = choices.split("|")
self.default = default
if type == "date" and not self.default:
today = datetime.date.today()
......@@ -54,6 +77,10 @@ class Parameter(object):
elif self.type == types.BooleanType :
if self.default: self.default = str(self.default).lower() in ("true", "t", "1")
else: self.default = True
elif type == "multiple":
self.default = []
for param in self.sub_parameters:
self.default.append(param.default)
elif self.action == "append":
self.default = []
......@@ -64,14 +91,18 @@ class Parameter(object):
elif self.type == types.BooleanType:
return {"help": self.help, "required": self.required, "dest": self.name,
"default": True, "action": "store_false"}
elif self.nargs > 1:
return {"type": self.type, "help": self.help, "required": self.required,
"dest": self.name, "default": self.default,
"action": self.action, "choices": self.choices, "nargs": self.nargs}
else:
return {"type": self.type, "help": self.help, "required": self.required,
"dest": self.name, "metavar": self.metavar, "default": self.default,
"dest": self.name, "default": self.default,
"action": self.action, "choices": self.choices}
def get_type(self):
return self.type.__name__
def __str__(self):
return self.name + ": " + self.flag + " | " + self.help + " (default=" + \
self.default + ", required=" + str(self.required) + ")"
return self.name + ": " + str(self.flag) + " | " + self.help + " (default=" + \
str(self.default) + ", required=" + str(self.required) + ")"
......@@ -282,18 +282,32 @@ class Workflow(threading.Thread):
for param in reader.items("parameters"):
cvalue = reader.get("parameters", param[0])
cparam = param[0].split(".")[0]
ckey = param[0].split(".")[1]
ckey = ".".join(param[0].split(".")[1:])
if not params.has_key(cparam):
params[cparam] = {ckey: cvalue}
else:
params[cparam][ckey] = cvalue
for param in params:
oparam = Parameter(**params[param])
for param_name in params:
sub_params, current_params, sub_objs = {}, {}, []
for param in params[param_name]:
sub_params_values = param.split(".")
if len(sub_params_values) == 2:
if not sub_params.has_key(sub_params_values[0]):
sub_params[sub_params_values[0]] = {sub_params_values[1]: params[param_name][param]}
else:
sub_params[sub_params_values[0]][sub_params_values[1]] = params[param_name][param]
else:
current_params[param] = params[param_name][param]
for sub_param in sub_params:
sub_objs.append(Parameter(**sub_params[sub_param]))
current_params["sub_parameters"] = sub_objs
oparam = Parameter(**current_params)
parameters.append(oparam)
except:
pass
# finally add the metadata parameter
metadata = Parameter("metadata", "--metadata", "Which metadata should be linked to this workflow", action="append")
metadata = Parameter(name="metadata", flag="--metadata", help="Which metadata should be linked to this workflow", action="append")
parameters.append(metadata)
return [reader.get("global", "name"),
reader.get("global", "description"),
......
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