parameter.py 4.76 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# 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 types
Jerome Mariette's avatar
Jerome Mariette committed
19
import datetime
Jerome Mariette's avatar
Jerome Mariette committed
20
import argparse
Jerome Mariette's avatar
Jerome Mariette committed
21
22
# import custom types
from workflows.types import *
Jerome Mariette's avatar
Jerome Mariette committed
23
24


Jerome Mariette's avatar
Jerome Mariette committed
25
26
27
class MiltipleParameters(object):
    def __init__(self, types):
        self.types = types
Jerome Mariette's avatar
Jerome Mariette committed
28
29
        self.index = None
        self.__name__ = "MiltipleParameters"
Jerome Mariette's avatar
Jerome Mariette committed
30
    def __call__(self, arg):
Jerome Mariette's avatar
Jerome Mariette committed
31
32
33
34
35
36
37
38
39
        parts = arg.split("=")
        if not self.types.has_key(parts[0]):
            raise argparse.ArgumentTypeError(parts[0] + " is an invalid flag! Available ones are: "+", ".join(self.types.keys()))
        try:
            value = self.types[parts[0]](parts[1])
        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)
Jerome Mariette's avatar
Jerome Mariette committed
40

Jerome Mariette's avatar
Jerome Mariette committed
41
42
43
44
class Parameter(object):
    """
    """
    
Jerome Mariette's avatar
Jerome Mariette committed
45
    def __init__(self, name, help, flag, default=None, type=types.StringType, choices=None, 
Jerome Mariette's avatar
Jerome Mariette committed
46
                 required=False, action="store", sub_parameters=[]):
Jerome Mariette's avatar
Jerome Mariette committed
47

Jerome Mariette's avatar
Jerome Mariette committed
48
49
50
        self.name = name
        self.flag = flag
        self.help = help
51
        self.action = action
Jerome Mariette's avatar
Jerome Mariette committed
52
        self.nargs = None
Jerome Mariette's avatar
Jerome Mariette committed
53
54
        self.sub_parameters = sub_parameters
        if len(self.sub_parameters) > 0:
Jerome Mariette's avatar
Jerome Mariette committed
55
            self.nargs = "+"
Jerome Mariette's avatar
Jerome Mariette committed
56
57
58
        
        if type == "date":
            self.type = date
Jerome Mariette's avatar
Jerome Mariette committed
59
        elif type == "multiple":
Jerome Mariette's avatar
Jerome Mariette committed
60
            sub_param_hash, sub_param_types, sub_param_names = {}, [], []
Jerome Mariette's avatar
Jerome Mariette committed
61
            for sub_param in self.sub_parameters:
Jerome Mariette's avatar
Jerome Mariette committed
62
63
64
65
66
67
68
                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)
            self.help += " | Format: " + " ".join([cname + "=<" + ctype.__name__.upper() + ">" for ctype, cname in zip(sub_param_types, sub_param_names)]) + ")"
Jerome Mariette's avatar
Jerome Mariette committed
69
70
        elif isinstance(type, types.FunctionType):
            self.type = type
Jerome Mariette's avatar
Jerome Mariette committed
71
72
73
        else:
            try: self.type = eval(type)
            except: self.type = types.StringType
Jerome Mariette's avatar
Jerome Mariette committed
74
            
Jerome Mariette's avatar
Jerome Mariette committed
75
        self.required = str(required).lower() in ("yes", "y", "true",  "t", "1")
Jerome Mariette's avatar
Jerome Mariette committed
76
        self.choices = choices
Jerome Mariette's avatar
Jerome Mariette committed
77
78
        if self.choices:
            self.choices = choices.split("|")
Jerome Mariette's avatar
Jerome Mariette committed
79
                        
Jerome Mariette's avatar
Jerome Mariette committed
80
81
82
83
        self.default = default
        if type == "date" and not self.default:
            today = datetime.date.today()
            self.default = today.strftime('%d/%m/%Y')
84
        elif self.type == types.BooleanType :
Jerome Mariette's avatar
Jerome Mariette committed
85
86
            if self.default: self.default = str(self.default).lower() in ("true",  "t", "1")
            else: self.default = True
87
        elif self.action == "append":
Jerome Mariette's avatar
Jerome Mariette committed
88
            self.default = []
Jerome Mariette's avatar
Jerome Mariette committed
89
90
91
92
93
        elif type == "multiple":
            self.default = {}
            for param in self.sub_parameters:
                self.default[param.name] = param.default
                
94
    def export_to_argparse(self):
95
96
97
98
        if self.type == types.BooleanType and str(self.default).lower() in ("false",  "f", "0"):
            return {"help": self.help, "required": self.required, "dest": self.name, 
                    "default": False, "action": "store_true"}
        elif self.type == types.BooleanType:
99
            return {"help": self.help, "required": self.required, "dest": self.name, 
100
                    "default": True, "action": "store_false"}
Jerome Mariette's avatar
Jerome Mariette committed
101
102
103
        elif self.nargs > 1:
            return {"type": self.type, "help": self.help, "required": self.required,
                    "dest": self.name, "default": self.default,
Jerome Mariette's avatar
Jerome Mariette committed
104
                    "action": self.action, "choices": self.choices, "nargs": "+"}
105
106
        else:
            return {"type": self.type, "help": self.help, "required": self.required,
Jerome Mariette's avatar
Jerome Mariette committed
107
                    "dest": self.name, "default": self.default,
108
109
                    "action": self.action, "choices": self.choices}
    
Jerome Mariette's avatar
Jerome Mariette committed
110
111
112
    def get_type(self):
        return self.type.__name__
    
Jerome Mariette's avatar
Jerome Mariette committed
113
    def __str__(self):
Jerome Mariette's avatar
Jerome Mariette committed
114
        return self.name + ": " + str(self.flag) + " | " + self.help + " (default=" + \
Jerome Mariette's avatar
Jerome Mariette committed
115
               str(self.default) + ", required=" + str(self.required) + ", flag=" + self.flag + ")"