config_reader.py 4.23 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
# 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
import os
import inspect

from ConfigParser import ConfigParser, NoOptionError
from jflow.parameter import Parameter


class WorkflowConfigReader(object):
    """
    """
    
    def __init__(self, path):
        """ 
        """
        self.reader = ConfigParser()
        self.reader.read(path)

    def get_name(self):
        return self.reader.get("global", "name")

    def get_description(self):
        return self.reader.get("global", "description")

    def get_parameters(self):
        """ 
        """
        parameters = []
        try:
            params = {}
            # First grab all parameters name
            for param in self.reader.items("parameters"):
                cvalue = self.reader.get("parameters", param[0])
                cparam = param[0].split(".")[0]
                ckey = param[0].split(".")[1]
                if not params.has_key(cparam):
                    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"]
                try: default = params[param]["default"]
                except: default = None
                try: 
                    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)
                parameters.append(oparam)
        except:
            pass
        return parameters
    

class JFlowConfigReader(object):
    """
    """
    
Jerome Mariette's avatar
Jerome Mariette committed
89
    CONFIG_FILE_PATH = "../../application.properties"
Jerome Mariette's avatar
Jerome Mariette committed
90
91
92
93
94
95
96
97
98
99
    
    def __init__(self):
        """ 
        """
        self.reader = ConfigParser()
        self.reader.read(os.path.join(os.path.dirname(inspect.getfile(self.__class__)), self.CONFIG_FILE_PATH))

    def get_tmp_directory(self):
        return self.reader.get("storage", "tmp_directory")
        
Jerome Mariette's avatar
Jerome Mariette committed
100
101
    def get_work_directory(self):
        return self.reader.get("storage", "work_directory")
Jerome Mariette's avatar
Jerome Mariette committed
102
103
104
105
106
107
    
    def get_exec(self, software):
        try:
            return self.reader.get("softwares", software)
        except NoOptionError, e:
            return None
Jerome Mariette's avatar
Jerome Mariette committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

    def get_log_file_path(self):
        """
        return the log file path
          @return: the path to the log file
        """
        try:
            return self.reader.get('storage', 'log_file')
        except :
            raise Error("Failed when parsing the config file, no section logging found!")
        
    def get_makeflow_path(self):
        try:
            return self.reader.get("grid", "makeflow")
        except NoOptionError, e:
            return None

    def get_batch(self):
        try:
            type = self.reader.get("grid", "batch_system_type")
            options = self.reader.get("grid", "batch_options")
            return [type, options]
        except NoOptionError, e:
            return None