Commit 3ce25de2 authored by Jerome Mariette's avatar Jerome Mariette
Browse files

better handle of parameters

parent 613cf1a1
......@@ -16,6 +16,11 @@
#
import types
import datetime
def date(datestr):
return datetime.datetime.strptime(datestr, '%d/%m/%Y')
class Parameter(object):
......@@ -23,16 +28,32 @@ class Parameter(object):
"""
def __init__(self, name, flag, help, default=None, type=types.StringType, choices=None,
required=False, metavar=None, action="store"):
required=False, action="store"):
self.name = name
self.flag = flag
self.help = help
self.default = default
self.type = type
if type == "date":
self.type = date
else:
try: self.type = eval(type)
except: self.type = types.StringType
self.metavar = self.type.__name__.upper()
self.required = str(required).lower() in ("yes", "y", "true", "t", "1")
self.choices = choices
self.required = required
self.metavar = str(metavar).upper()
if self.choices:
self.choices = choices.split("|")
self.default = default
if type == "date" and not self.default:
today = datetime.date.today()
self.default = today.strftime('%d/%m/%Y')
self.action = action
if self.action == "append":
self.default = []
def get_type(self):
return self.type.__name__
......@@ -40,4 +61,3 @@ class Parameter(object):
def __str__(self):
return self.name + ": " + self.flag + " | " + self.help + " (default=" + \
self.default + ", required=" + str(self.required) + ")"
\ No newline at end of file
......@@ -230,44 +230,8 @@ class Workflow(threading.Thread):
params[cparam] = {ckey: cvalue}
else:
params[cparam][ckey] = cvalue
# Then build params
for param in params:
name = params[param]["name"]
flag = params[param]["flag"]
help = params[param]["help"]
# handle default values
try:
# if it's a date and there is no default value, use today as default value
if params[param]["type"] == "date" and not params[param].has_key("default"):
today = datetime.date.today()
default = today.strftime('%d/%m/%Y')
elif params[param].has_key("action"):
if params[param]["action"] == "append": default = []
else:
default = params[param]["default"]
except:
default = None
# handle type values
try:
if params[param]["type"] == "date":
type = date
metavar = "date"
else:
type = eval(params[param]["type"])
metavar = params[param]["type"]
except:
type = types.StringType
metavar = "str"
try: required = params[param]["required"].lower() in ("yes", "y", "true", "t", "1")
except: required = False
try: action = params[param]["action"]
except: action = "store"
try: choices = params[param]["choices"].split("|")
except: choices = None
oparam = Parameter(name=name, flag=flag, help=help, default=default,
type=type, required=required, metavar=metavar,
action=action, choices=choices)
oparam = Parameter(**params[param])
parameters.append(oparam)
except:
pass
......
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