Commit 45f7834e authored by Jerome Mariette's avatar Jerome Mariette
Browse files

add availibility to define required sub parameters

parent 6955ca50
......@@ -18,14 +18,17 @@
import types
import datetime
import argparse
from argparse import _ensure_value
import copy as _copy
# import custom types
from workflows.types import *
class MiltipleParameters(object):
def __init__(self, types):
def __init__(self, types, required):
self.types = types
self.index = None
self.required = required
self.__name__ = "MiltipleParameters"
def __call__(self, arg):
parts = arg.split("=")
......@@ -36,8 +39,34 @@ class MiltipleParameters(object):
except:
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)
return (parts[0], value, self.required)
class MiltipleAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
try:
required = _copy.copy(values[0][2])
for val in values:
if val[0] in required:
del required[required.index(val[0])]
except: pass
if len(required) == 1: parser.error(", ".join(required) + " is a required parameter!")
elif len(required) > 1: parser.error(", ".join(required) + " are required parameters!")
setattr(namespace, self.dest, values)
class MiltipleAppendAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
try:
required = _copy.copy(values[0][2])
for val in values:
if val[0] in required:
del required[required.index(val[0])]
except: pass
if len(required) == 1: parser.error(", ".join(required) + " is a required parameter!")
elif len(required) > 1: parser.error(", ".join(required) + " are required parameters!")
items = _copy.copy(_ensure_value(namespace, self.dest, []))
items.append(values)
setattr(namespace, self.dest, items)
class Parameter(object):
"""
"""
......@@ -53,18 +82,22 @@ class Parameter(object):
self.sub_parameters = sub_parameters
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_hash, sub_param_types, sub_param_names, sub_param_required = {}, [], [], []
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)
self.type = MiltipleParameters(sub_param_hash)
if sub_param.required: sub_param_required.append(sub_param.flag)
self.type = MiltipleParameters(sub_param_hash, sub_param_required)
if self.action == "append":
self.action = MiltipleAppendAction
else:
self.action = MiltipleAction
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
......@@ -84,7 +117,7 @@ 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 self.action == "append":
elif self.action == "append" or self.action == MiltipleAppendAction:
self.default = []
elif type == "multiple":
self.default = {}
......
......@@ -33,7 +33,7 @@ from inspect import getcallargs
import jflow
import jflow.utils as utils
from jflow.workflows_manager import WorkflowsManager, JFlowConfigReader
from jflow.parameter import Parameter, date
from jflow.parameter import Parameter, date, MiltipleAppendAction
from weaver.script import ABSTRACTIONS
from weaver.script import DATASETS
......@@ -248,7 +248,7 @@ class Workflow(threading.Thread):
except: pass
# if this parameter has been modified by the user
if args.has_key(param.name):
if param.action == "append" and param.type.__name__ == "MiltipleParameters":
if param.action == MiltipleAppendAction:
extended_args[param.name] = []
for arg in args[param.name]:
sub_hash = {}
......
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