Commit efbdf4cd authored by Jerome Mariette's avatar Jerome Mariette
Browse files

handle append for sub parameters

parent 3d38549f
......@@ -25,16 +25,17 @@ from workflows.types import *
class MiltipleParameters(object):
def __init__(self, types, required, choices, excludes, default):
def __init__(self, types, required, choices, excludes, default, actions):
self.types = types
self.choices = choices
self.excludes = excludes
self.default = default
self.actions = actions
self.index = None
self.required = required
self.__name__ = "MiltipleParameters"
def __call__(self, arg):
def __call__(self, arg):
parts = arg.split("=")
if not self.types.has_key(parts[0]):
raise argparse.ArgumentTypeError(parts[0] + " is an invalid flag! Available ones are: "+", ".join(self.types.keys()))
......@@ -51,7 +52,7 @@ class MiltipleParameters(object):
raise argparse.ArgumentTypeError("invalid " + self.types[parts[0]].__name__ + " value: '" + parts[1] + "' for sub parameter '" + parts[0] + "'")
self.index = parts[0]
return (parts[0], value, self.required, self.excludes)
return (parts[0], value, self.required, self.excludes, self.actions)
class MiltipleAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
......@@ -96,8 +97,19 @@ class MiltipleAction(argparse.Action):
for param in values[0][3][group]:
if param in given_params: rfound = True
if not rfound: parser.error("one of the arguments: " + ", ".join(values[0][3][group]) + " is required")
# if ok add the value
setattr(namespace, self.dest, values)
final_hash, final_values = {}, []
for value in values:
if values[0][4][value[0]] == "append" and final_hash.has_key(value[0]):
final_hash[value[0]].append(value[1])
elif values[0][4][value[0]] == "append":
final_hash[value[0]]= [value[1]]
else:
final_hash[value[0]]= value[1]
for param in final_hash:
final_values.append((param, final_hash[param]))
setattr(namespace, self.dest, final_values)
class MiltipleAppendAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
......@@ -144,7 +156,17 @@ class MiltipleAppendAction(argparse.Action):
if not rfound: parser.error("one of the arguments: " + ", ".join(values[0][3][group]) + " is required")
# if ok add the value
items = _copy.copy(_ensure_value(namespace, self.dest, []))
items.append(values)
final_hash, final_values = {}, []
for value in values:
if values[0][4][value[0]] == "append" and final_hash.has_key(value[0]):
final_hash[value[0]].append(value[1])
elif values[0][4][value[0]] == "append":
final_hash[value[0]]= [value[1]]
else:
final_hash[value[0]]= value[1]
for param in final_hash:
final_values.append((param, final_hash[param]))
items.append(final_values)
setattr(namespace, self.dest, items)
class Parameter(object):
......@@ -176,7 +198,7 @@ class Parameter(object):
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_default, sub_param_actions = {}, {}
for sub_param in self.sub_parameters:
try: sub_type = sub_param.type
except: sub_type = types.StringType
......@@ -185,13 +207,14 @@ class Parameter(object):
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 = MiltipleParameters(sub_param_hash, sub_param_required, sub_param_choices, sub_param_excludes, sub_param_default)
self.type = MiltipleParameters(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:
......
Supports Markdown
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