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

Better None management for standard parameters.

parent 3f2567d5
......@@ -423,8 +423,13 @@ class ParameterFactory(object):
return StrParameter( *args, **kwargs )
def noneException(*args, **kwargs):
raise Exception( "The parameter value is None." )
def none_decorator(fn):
def new_func(*args, **kwargs):
if args[0].is_None:
raise Exception("The parameter '" + args[0].name + "' is None.")
else:
return fn(*args, **kwargs)
return new_func
class BoolParameter(int, AbstractParameter):
......@@ -433,13 +438,12 @@ class BoolParameter(int, AbstractParameter):
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 bool.__dict__:
value = getattr(bool, attr)
if callable(value) and attr not in ["__new__", "__str__", "__init__", "__int__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
val.is_None = False if default != None else True
for attr in bool.__dict__:
func = getattr(bool, attr)
if callable(func) and attr not in ["__new__", "__init__", "__bool__", "__getattribute__", "__setattribute__",
"__eq__", "__ne__", "__nonzero__", "__str__", "__repr__"]:
setattr(BoolParameter, attr, none_decorator(func))
return val
def __init__(self, name, help, default=None, type=types.BooleanType, choices=None, required=False,
......@@ -452,6 +456,9 @@ class BoolParameter(int, AbstractParameter):
return str(None)
return str(bool(self))
def __repr__(self):
return str(self)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
......@@ -476,25 +483,27 @@ class IntParameter(int, AbstractParameter):
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 int.__dict__:
value = getattr(int, attr)
if callable(value) and attr not in ["__new__", "__init__", "__str__", "__int__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
val.is_None = False if default != None else True
for attr in int.__dict__:
func = getattr(int, attr)
if callable(func) and attr not in ["__new__", "__init__", "__int__", "__getattribute__", "__setattribute__",
"__eq__", "__ne__", "__nonzero__", "__str__", "__repr__"]:
setattr(IntParameter, attr, none_decorator(func))
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)
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 __str__(self):
if self.is_None:
return str(None)
return str(int(self))
def __repr__(self):
return str(self)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
......@@ -519,13 +528,12 @@ class FloatParameter(float, AbstractParameter):
flag=None, sub_parameters=None, group="default", display_name=None):
float_default = 0.0 if default == None else float(default)
val = float.__new__(self, float_default)
val.is_None = False
if default == None:
val.is_None = True
for attr in float.__dict__:
value = getattr(float, attr)
if callable(value) and attr not in ["__new__", "__init__", "__str__", "__float__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
val.is_None = False if default != None else True
for attr in float.__dict__:
func = getattr(float, attr)
if callable(func) and attr not in ["__new__", "__init__", "__float__", "__getattribute__", "__setattribute__",
"__eq__", "__ne__", "__nonzero__", "__str__", "__repr__"]:
setattr(FloatParameter, attr, none_decorator(func))
return val
def __init__(self, name, help, default=None, type=types.FloatType, choices=None, required=False,
......@@ -538,6 +546,9 @@ class FloatParameter(float, AbstractParameter):
return str(None)
return str(float(self))
def __repr__(self):
return str(self)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
......@@ -562,13 +573,12 @@ class StrParameter(str, AbstractParameter):
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 str.__dict__:
value = getattr(str, attr)
if callable(value) and attr not in ["__new__", "__init__", "__str__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
val.is_None = False if default != None else True
for attr in str.__dict__:
func = getattr(str, attr)
if callable(func) and attr not in ["__new__", "__init__", "__str__", "__getattribute__", "__setattribute__",
"__eq__", "__ne__", "__nonzero__", "__repr__"]:
setattr(StrParameter, attr, none_decorator(func))
return val
def __init__(self, name, help, default=None, type=types.StringType, choices=None, required=False,
......@@ -581,6 +591,11 @@ class StrParameter(str, AbstractParameter):
return str(None)
return str.__str__(self)
def __repr__(self):
if self.is_None:
return str(None)
return "'" + str(self) + "'"
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
......@@ -608,16 +623,13 @@ class DateParameter(datetime.datetime, AbstractParameter):
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:
val.is_None = True
for attr in datetime.datetime.__dict__:
value = getattr(datetime.datetime, attr)
if callable(value) and attr not in ["__new__", "__init__", "__str__", "__getattribute__", "__eq__", "__ne__", "__nonzero__"]:
setattr(val, attr, noneException)
val.is_None = False if default != None else True
for attr in datetime.datetime.__dict__:
func = getattr(datetime.datetime, attr)
if callable(func) and attr not in ["__new__", "__init__", "__getattribute__", "__setattribute__",
"__eq__", "__ne__", "__nonzero__", "__str__", "__repr__"]:
setattr(DateParameter, attr, none_decorator(func))
return val
def __init__(self, name, help, default=None, type=date, choices=None, required=False,
......@@ -633,6 +645,9 @@ class DateParameter(datetime.datetime, AbstractParameter):
return str(None)
return datetime.datetime.__str__(self)
def __repr__(self):
return str(self)
def __eq__(self, other):
if other.__class__.__name__ == "NoneType":
return self.is_None
......
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