Commit 69b6172a authored by Frédéric Escudié's avatar Frédéric Escudié
Browse files

Change Parameter to IntParameter, FloatParameter, BoolParameter and...

Change Parameter to IntParameter, FloatParameter, BoolParameter and StrParameter (fix bug with None and with number parameter operators).
parent 3013ebde
......@@ -176,6 +176,7 @@ class MiltipleAction(argparse.Action):
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):
# what is commun within required and excludes
......@@ -257,28 +258,21 @@ class AbstractParameter(object):
self.required = required
self.choices = choices
# Set parameter type
if type == "date":
self.type = date
self.type = types.MemberDescriptorType
elif type == "multiple":
self.type = "multiple"
elif isinstance(type, types.FunctionType):
self.type = type
elif type.__class__.__name__ == "type":
elif type in [types.StringType, types.IntType, types.FloatType, types.BooleanType]:
self.type = type
else:
try: self.type = eval(type)
except: self.type = types.StringType
# Set parameter value
self.default = 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.__class__.__name__ == "str":
if self.default: self.default = str(self.default).lower() in (True, "true", "t", "1")
else: self.default = True
elif self.action == "append":
self.default = []
def export_to_argparse(self):
if self.type == types.BooleanType and str(self.default).lower() in (False, "false", "f", "0"):
......@@ -334,7 +328,7 @@ class MultiParameter(dict, AbstractParameter):
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.default[param_flag] = param.default if param != None else None
self.type.actions[param_flag] = param.action
if param.required:
self.type.required.append(param_flag)
......@@ -369,7 +363,7 @@ class MultiParameterList(list, AbstractParameter):
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.default[param_flag] = param.default if param != None else None
self.type.actions[param_flag] = param.action
if param.required:
self.type.required.append(param_flag)
......@@ -378,37 +372,197 @@ class MultiParameterList(list, AbstractParameter):
self.sub_parameters.append(param)
class Parameter(str, AbstractParameter):
def noneException(*args, **kwargs):
raise Exception( "The parameter value is None." )
def __new__(self, name, help, default="", type=types.StringType, choices=None, required=False,
class BoolParameter(int, AbstractParameter):
def __new__(self, name, help, default=None, type=types.BooleanType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
bool_default = False if default == None else bool(default)
val = int.__new__(self, bool_default)
val.is_None = False
if default == None:
val.is_None = True
for attr in val.__dict__:
value = getattr(val, attr)
if callable(value) and attr not in ["__new__", "__init__", "__int__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
return val
def __init__(self, name, help, default=None, type=types.BooleanType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
return str.__new__(self, default)
AbstractParameter.__init__(self, name, help, flag=flag, default=default, type=type, choices=choices, required=required,
action="store", sub_parameters=sub_parameters, group=group, display_name=display_name)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self == 'None'
else: return str.__eq__(self, other)
return self.is_None
elif self.is_None:
return False
else:
return int(self) == int(other)
def __ne__(self, other):
if other.__class__.__name__ == "NoneType":
return self != 'None'
else: return str.__ne__(self, other)
return not self.is_None
elif self.is_None:
return True
else:
return int(self) != int(other)
def __nonzero__(self):
if self == None or self == 'None':
if self.is_None:
return False
else: return True
else:
return self != 0
def __init__(self, name, help, default="", type=types.StringType, choices=None, required=False,
class IntParameter(int, AbstractParameter):
def __new__(self, name, help, default=None, type=types.IntType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
int_default = 0 if default == None else int(default)
val = int.__new__(self, int_default)
val.is_None = False
if default == None:
val.is_None = True
for attr in val.__dict__:
value = getattr(val, attr)
if callable(value) and attr not in ["__new__", "__init__", "__int__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
return val
def __init__(self, name, help, default=None, type=types.IntType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
AbstractParameter.__init__(self, name, help, flag=flag, default=default, type=type, choices=choices, required=required,
action="store", sub_parameters=sub_parameters, group=group, display_name=display_name)
class AbstractInputFile(AbstractIOFile):
"""
@summary : Parent of all InputFile(s) parameters.
"""
pass
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
elif self.is_None:
return False
else:
return int(self) == int(other)
def __ne__(self, other):
if other.__class__.__name__ == "NoneType":
return not self.is_None
elif self.is_None:
return True
else:
return int(self) != int(other)
def __nonzero__(self):
if self.is_None:
return False
else:
return self != 0
class FloatParameter(int, AbstractParameter):
def __new__(self, name, help, default=None, type=types.FloatType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
float_default = 0.0 if default == None else float(default)
val = int.__new__(self, float_default)
val.is_None = False
if default == None:
val.is_None = True
for attr in val.__dict__:
value = getattr(val, attr)
if callable(value) and attr not in ["__new__", "__init__", "__float__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
return val
def __init__(self, name, help, default=None, type=types.FloatType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
AbstractParameter.__init__(self, name, help, flag=flag, default=default, type=type, choices=choices, required=required,
action="store", sub_parameters=sub_parameters, group=group, display_name=display_name)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
elif self.is_None:
return False
else:
return float(self) == float(other)
def __ne__(self, other):
if other.__class__.__name__ == "NoneType":
return not self.is_None
elif self.is_None:
return True
else:
return float(self) != float(other)
def __nonzero__(self):
if self.is_None:
return False
else:
return self != 0.0
class StrParameter(str, AbstractParameter):
def __new__(self, name, help, default=None, type=types.StringType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
str_default = "" if default == None else str(default)
val = str.__new__(self, str_default)
val.is_None = False
if default == None:
val.is_None = True
for attr in val.__dict__:
value = getattr(val, attr)
if callable(value) and attr not in ["__new__", "__init__", "__str__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
return val
def __init__(self, name, help, default=None, type=types.StringType, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
AbstractParameter.__init__(self, name, help, flag=flag, default=default, type=type, choices=choices, required=required,
action="store", sub_parameters=sub_parameters, group=group, display_name=display_name)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
elif self.is_None:
return False
else:
return str(self) == str(other)
def __ne__(self, other):
if other.__class__.__name__ == "NoneType":
return not self.is_None
elif self.is_None:
return True
else:
return str(self) != str(other)
def __nonzero__(self):
if self.is_None:
return False
else:
return (True if str(self) else False)
class ParameterFactory(object):
@staticmethod
def factory(*args, **kwargs):
if not kwargs.has_key( "type" ):
return StrParameter( *args, **kwargs )
if kwargs["type"] == "int" or kwargs["type"] is types.IntType:
return IntParameter( *args, **kwargs )
elif kwargs["type"] == "bool" or kwargs["type"] is types.BooleanType :
return BoolParameter( *args, **kwargs )
elif kwargs["type"] == "float" or kwargs["type"] is types.FloatType :
return FloatParameter( *args, **kwargs )
else:
return StrParameter( *args, **kwargs )
class AbstractOutputFile(AbstractIOFile):
"""
......@@ -416,7 +570,8 @@ class AbstractOutputFile(AbstractIOFile):
"""
pass
class InputFile(Parameter, AbstractInputFile):
class InputFile(StrParameter, AbstractInputFile):
def __new__(self, name, help, file_format="any", default="", type="localfile", choices=None,
required=False, flag=None, group="default", display_name=None):
......@@ -429,24 +584,26 @@ class InputFile(Parameter, AbstractInputFile):
raise ValueError("InputFile.__new__: wrong type provided: '"+type2test+"', this should be choosen between '"
+ "', '".join(INPUTFILE_TYPES)+"'")
return str.__new__(self, default)
return StrParameter.__new__(self, name, help, flag=flag, default=default, type=type, choices=choices,
required=required, group=group, display_name=display_name)
def __init__(self, name, help, file_format="any", default="", type="localfile", choices=None,
required=False, flag=None, group="default", display_name=None):
AbstractIOFile.__init__(self, file_format)
Parameter.__init__(self, name, help, flag=flag, default=default, type=type, choices=choices,
StrParameter.__init__(self, name, help, flag=flag, default=default, type=type, choices=choices,
required=required, group=group, display_name=display_name)
class OutputFile(Parameter, AbstractOutputFile):
class OutputFile(StrParameter, AbstractOutputFile):
def __new__(self, name, help, file_format="any", default="", choices=None,
required=False, flag=None, group="default", display_name=None):
return str.__new__(self, default)
return StrParameter.__new__(self, name, help, flag=flag, default=default, type="localfile", choices=choices,
required=required, group=group, display_name=display_name)
def __init__(self, name, help, file_format="any", default="", choices=None,
required=False, flag=None, group="default", display_name=None):
AbstractIOFile.__init__(self, file_format)
Parameter.__init__(self, name, help, flag=flag, default=default, type="localfile", choices=choices,
StrParameter.__init__(self, name, help, flag=flag, default=default, type="localfile", choices=choices,
required=required, group=group, display_name=display_name)
......@@ -490,6 +647,7 @@ class InputFileList(ParameterList, AbstractInputFile):
elif issubclass( default.__class__, AbstractOutputFile ):
return list.__init__(self, default)
class OutputFileList(ParameterList, AbstractOutputFile):
def __init__(self, name, help, file_format="any", default=None, choices=None,
......@@ -503,6 +661,7 @@ class OutputFileList(ParameterList, AbstractOutputFile):
elif default.__class__.__name__ == "list":
return list.__init__(self, default)
class DynamicOutput(ParameterList, AbstractOutputFile):
"""
@warning : with this class of output, the component become dynamic.
......@@ -513,6 +672,7 @@ class DynamicOutput(ParameterList, AbstractOutputFile):
"""
raise NotImplementedError
class OutputFilesEndsWith(DynamicOutput):
def __init__(self, name, help, output_directory, end_str, include=True, file_format="any", choices=None,
......@@ -542,6 +702,7 @@ class OutputFilesEndsWith(DynamicOutput):
output_files.append( os.path.join(self.output_directory, file) )
list.__init__(self, output_files)
class OutputFilesPattern(DynamicOutput):
def __init__(self, name, help, output_directory, pattern, include=True, file_format="any", choices=None,
......
......@@ -157,7 +157,7 @@ class Workflow(threading.Thread):
def add_parameter(self, name, help, default=None, type=types.StringType, choices=None,
required=False, flag=None, group="default", display_name=None, add_to=None):
new_param = Parameter(name, help, flag=flag, default=default, type=type, choices=choices,
new_param = ParameterFactory.factory(name, help, flag=flag, default=default, type=type, choices=choices,
required=required, group=group, display_name=display_name)
# if this input should be added to a particular parameter
if add_to:
......@@ -244,10 +244,10 @@ class Workflow(threading.Thread):
for param in parameters:
try: args[param.name] = args[param.name].encode('ascii','ignore')
except: pass
if param.__class__ == Parameter:
if param.__class__ == StrParameter or param.__class__ == IntParameter or param.__class__ == FloatParameter or param.__class__ == BoolParameter:
if args[param.name]: default = args[param.name]
else: default = param.default
new_param = Parameter(param.name, param.help, default=default, type=param.type, choices=param.choices,
new_param = ParameterFactory.factory(param.name, param.help, default=default, 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:
......
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