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

Add DateParameter.

Fix bug on parameters.
parent dfb9cffd
......@@ -102,7 +102,7 @@ class Component(object):
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:
......
......@@ -260,7 +260,7 @@ class AbstractParameter(object):
# Set parameter type
if type == "date":
self.type = types.MemberDescriptorType
self.type = date
elif type == "multiple":
self.type = "multiple"
elif isinstance(type, types.FunctionType):
......@@ -372,13 +372,31 @@ class MultiParameterList(list, AbstractParameter):
self.sub_parameters.append(param)
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 )
elif kwargs["type"] == "date" or kwargs["type"] == date:
return DateParameter( *args, **kwargs )
else:
return StrParameter( *args, **kwargs )
def noneException(*args, **kwargs):
raise Exception( "The parameter value is None." )
class BoolParameter(int, AbstractParameter):
def __new__(self, name, help, default=None, type=types.BooleanType, choices=None, required=False,
def __new__(self, name, help, default=False, 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)
......@@ -393,24 +411,24 @@ class BoolParameter(int, AbstractParameter):
def __init__(self, name, help, default=None, type=types.BooleanType, 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,
AbstractParameter.__init__(self, name, help, flag=flag, default=bool(default), type=type, choices=choices, required=required,
action="store", sub_parameters=sub_parameters, group=group, display_name=display_name)
def __str__(self):
if self.is_None:
noneException()
return str(bool(self))
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)
return int(self) == int(bool(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)
return not (self == other)
def __nonzero__(self):
if self.is_None:
......@@ -448,12 +466,7 @@ class IntParameter(int, AbstractParameter):
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)
return not (self == other)
def __nonzero__(self):
if self.is_None:
......@@ -462,12 +475,12 @@ class IntParameter(int, AbstractParameter):
return self != 0
class FloatParameter(int, AbstractParameter):
class FloatParameter(float, 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 = float.__new__(self, float_default)
val.is_None = False
if default == None:
val.is_None = True
......@@ -491,12 +504,7 @@ class FloatParameter(int, AbstractParameter):
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)
return not (self == other)
def __nonzero__(self):
if self.is_None:
......@@ -534,12 +542,7 @@ class StrParameter(str, AbstractParameter):
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)
return not(self == other)
def __nonzero__(self):
if self.is_None:
......@@ -548,20 +551,58 @@ class StrParameter(str, AbstractParameter):
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 )
class DateParameter(datetime.datetime, AbstractParameter):
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 )
def __new__(self, name, help, default=None, type=date, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
date_default = datetime.datetime.today()
if default != None and issubclass(default.__class__, datetime.datetime):
date_default = default
elif default != None:
date_default = date(default)
val = datetime.datetime.__new__(self, date_default.year, date_default.month, date_default.day)
if default != None:
val.is_None = False
else:
return StrParameter( *args, **kwargs )
val.is_None = True
for attr in val.__dict__:
value = getattr(val, attr)
if callable(value) and attr not in ["__new__", "__init__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
return val
def __init__(self, name, help, default=None, type=date, choices=None, required=False,
flag=None, sub_parameters=None, group="default", display_name=None):
if default != None and not issubclass(default.__class__, datetime.datetime):
date_default = date(default)
default = datetime.datetime(date_default.year, date_default.month, date_default.day)
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 datetime.date(self.year, self.month, self.day) == other
def __ne__(self, other):
return not( self == other )
def __nonzero__(self):
if self.is_None:
return False
else:
return (True if datetime.date(self.year, self.month, self.day) else False)
class AbstractInputFile(AbstractIOFile):
"""
@summary : Parent of all InputFile(s) parameters.
"""
pass
class AbstractOutputFile(AbstractIOFile):
......
......@@ -244,7 +244,7 @@ class Workflow(threading.Thread):
for param in parameters:
try: args[param.name] = args[param.name].encode('ascii','ignore')
except: pass
if param.__class__ == StrParameter or param.__class__ == IntParameter or param.__class__ == FloatParameter or param.__class__ == BoolParameter:
if param.__class__ == StrParameter or param.__class__ == IntParameter or param.__class__ == FloatParameter or param.__class__ == BoolParameter or param.__class__ == DateParameter:
if args[param.name]: default = args[param.name]
else: default = param.default
new_param = ParameterFactory.factory(param.name, param.help, default=default, type=param.type, choices=param.choices,
......
#
# Copyright (C) 2012 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from jflow.workflow import Workflow
from jflow.parameter import InputFileList, InputFile
class Test (Workflow):
def get_description(self):
return "use for test jflow core modifications"
def define_parameters(self, parameters_section=None):
self.add_multiple_parameter_list("multi1", "Multiple.")
self.add_parameter("test_A1", "Multiple float null.", add_to="multi1", type="float")
self.add_parameter("test_A2", "Multiple float default 1.", add_to="multi1", type="float", default=1)
self.add_parameter("test_A3", "Multiple int default 2.", add_to="multi1", type=int, default=2)
self.add_parameter("test_A4", "Multiple int null.", add_to="multi1", type=int)
self.add_parameter("test_A5", "Multiple date default str '20/08/2014'.", add_to="multi1", type="date", default="20/08/2014")
self.add_parameter("test_A6", "date default null.", add_to="multi1", type="date")
self.add_parameter("test_A7", "Multiple string default str 'None'.", add_to="multi1", default="None")
self.add_parameter("test_A8", "Multiple string null.", add_to="multi1")
self.add_parameter("test_A9", "Multiple bool default True.", add_to="multi1", type="bool", default=True)
self.add_parameter("test_A10", "Multiple bool default 3.", add_to="multi1", type="bool", default=3)
self.add_parameter("test_A11", "Multiple bool default null.", add_to="multi1", type="bool")
self.add_input_file("test_A12", "Multiple InputFile default null.", add_to="multi1")
self.add_input_file("test_A13", "Multiple InputFile default application.properties.", add_to="multi1", default="application.properties")
self.add_parameter("test_B1", "float null.", type="float")
self.add_parameter("test_B2", "float default 1.", type="float", default=1)
self.add_parameter("test_B3", "int default 2.", type=int, default=2)
self.add_parameter("test_B4", "int null.", type=int)
self.add_parameter("test_B5", "date default str 20/08/2014.", type="date", default="20/08/2014")
self.add_parameter("test_B6", "date default null.", type="date")
self.add_parameter("test_B7", "string default str None.", default="None")
self.add_parameter("test_B8", "string null.")
self.add_parameter("test_B9", "bool default True.", type="bool", default=True)
self.add_parameter("test_B10", "bool default false.", type="bool", default=False)
self.add_parameter("test_B11", "bool default null.", type="bool")
self.add_input_file("test_B12", "Multiple InputFile default null.")
self.add_input_file("test_B13", "Multiple InputFile default application.properties.", default="application.properties")
self.add_multiple_parameter_list("multi2", "Multiple required.", required=True)
self.add_parameter("valid", "Multiple required.", add_to="multi2", default="ok")
self.add_parameter("test_C1", "Multiple float null.", add_to="multi2", type="float")
self.add_parameter("test_C2", "Multiple float default 1.", add_to="multi2", type="float", default=1)
self.add_parameter("test_C3", "Multiple int default 2.", add_to="multi2", type=int, default=2)
self.add_parameter("test_C4", "Multiple int null.", add_to="multi2", type=int)
self.add_parameter("test_C5", "Multiple date default str '20/08/2014'.", add_to="multi2", type="date", default="20/08/2014")
self.add_parameter("test_C6", "date default null.", add_to="multi2", type="date")
self.add_parameter("test_C7", "Multiple string default str 'None'.", add_to="multi2", default="None")
self.add_parameter("test_C8", "Multiple string null.", add_to="multi2")
self.add_parameter("test_C9", "Multiple bool default True.", add_to="multi2", type="bool", default=True)
self.add_parameter("test_C10", "Multiple bool default 3.", add_to="multi2", type="bool", default=3)
self.add_parameter("test_C11", "Multiple bool default null.", add_to="multi2", type="bool")
self.add_input_file("test_C12", "Multiple InputFile default null.", add_to="multi2")
self.add_input_file("test_C13", "Multiple InputFile default application.properties.", add_to="multi2", default="application.properties")##### Pb localfile and urlfile
def process(self):
print "###################### Workflow ############################"
for attr in self.params_order:
value = getattr(self, attr)
print attr
print value
print "class : " + value.__class__.__name__
print "is None : " + str(value is None)
print "== None : " + str(value == None)
print ""
test_parameters = self.add_component("TestComponentParameters", [
self.test_B1,
self.test_B2,
self.test_B3,
self.test_B4,
self.test_B5,
self.test_B6,
self.test_B7,
self.test_B8,
self.test_B9,
self.test_B10,
self.test_B11,
self.test_B12,
self.test_B13,
self.multi2[0]["test_C1"],
self.multi2[0]["test_C2"],
self.multi2[0]["test_C3"],
self.multi2[0]["test_C4"],
self.multi2[0]["test_C5"],
self.multi2[0]["test_C6"],
self.multi2[0]["test_C7"],
self.multi2[0]["test_C8"],
self.multi2[0]["test_C9"],
self.multi2[0]["test_C10"],
self.multi2[0]["test_C11"],
self.multi2[0]["test_C12"],
self.multi2[0]["test_C13"]
])
\ No newline at end of file
#
# Copyright (C) 2012 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
\ No newline at end of file
#
# Copyright (C) 2012 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import os
from subprocess import Popen, PIPE
from jflow.component import Component
from jflow.parameter import Formats
class TestComponentParameters (Component):
def define_parameters(self, test_B1, test_B2, test_B3, test_B4, test_B5, test_B6, test_B7, test_B8, test_B9, test_B10, test_B11, test_B12, test_B13, test_C1, test_C2, test_C3, test_C4, test_C5, test_C6, test_C7, test_C8, test_C9, test_C10, test_C11, test_C12, test_C13):
self.add_parameter("test_B1", "float null.", type="float", default=test_B1)
self.add_parameter("test_B2", "float default 1.", type="float", default=test_B2)
self.add_parameter("test_B3", "int default 2.", type=int, default=test_B3)
self.add_parameter("test_B4", "int null.", type=int, default=test_B4)
self.add_parameter("test_B5", "date default str 20/08/2014.", type="date", default=test_B5)
self.add_parameter("test_B6", "date default null.", type="date", default=test_B6)
self.add_parameter("test_B7", "string default str None.", default=test_B7)
self.add_parameter("test_B8", "string null.", default=test_B8)
self.add_parameter("test_B9", "bool default True.", type="bool", default=test_B9)
self.add_parameter("test_B10", "bool default false.", type="bool", default=test_B10)
self.add_parameter("test_B11", "bool default null.", type="bool", default=test_B11)
self.add_input_file("test_B12", "Multiple InputFile default null.", default=test_B12)
self.add_input_file("test_B13", "Multiple InputFile default application.properties.", default=test_B13)
self.add_parameter("test_C1", "Multiple float null.", type="float", default=test_C1)
self.add_parameter("test_C2", "Multiple float default 1.", type="float", default=test_C2)
self.add_parameter("test_C3", "Multiple int default 2.", type=int, default=test_C3)
self.add_parameter("test_C4", "Multiple int null.", type=int, default=test_C4)
self.add_parameter("test_C5", "Multiple date default str '20/08/2014'.", type="date", default=test_C5)
self.add_parameter("test_C6", "date default null.", type="date", default=test_C6)
self.add_parameter("test_C7", "Multiple string default str 'None'.", default=test_C7)
self.add_parameter("test_C8", "Multiple string null.", default=test_C8)
self.add_parameter("test_C9", "Multiple bool default True.", type="bool", default=test_C9)
self.add_parameter("test_C10", "Multiple bool default 3.", type="bool", default=test_C10)
self.add_parameter("test_C11", "Multiple bool default null.", type="bool", default=test_C11)
self.add_input_file("test_C12", "Multiple InputFile default null.", default=test_C12)
self.add_input_file("test_C13", "Multiple InputFile default application.properties.", default=test_C13)
def process(self):
print "###################### Component ############################"
for attr in self.params_order:
value = getattr(self, attr)
print attr
print value
print "class : " + value.__class__.__name__
print "is None : " + str(value is None)
print "== None : " + str(value == None)
print ""
def get_version(self):
return "-"
python bin/jflow_cli.py test \
--multi1 test-A1=1.1 test-A2=2 test-A3=3 test-A4=4 test-A5=02/09/2014 test-A6=03/09/2014 test-A7=test test-A8=None test-A11 test-A12=application.properties test-A13=bin/jflow_cli.py \
--test-B1 1.1 --test-B2 2 --test-B3 3 --test-B4 4 --test-B5 02/09/2014 --test-B6 03/09/2014 --test-B7 test --test-B8 None --test-B11 --test-B12 application.properties --test-B13 bin/jflow_cli.py \
--multi2 test-C1=1.1 test-C2=2 test-C3=3 test-C4=4 test-C5=02/09/2014 test-C6=03/09/2014 test-C7=test test-C8=None test-C11 test-C12=application.properties test-C13=bin/jflow_cli.py
python bin/jflow_cli.py test --multi2 valid=test
\ No newline at end of file
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