config_reader.py 7.55 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
#
# Copyright (C) 2009 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/>.
#

__author__ = 'Plateforme bioinformatique Midi Pyrenees'
__copyright__ = 'Copyright (C) 2009 INRA'
__license__ = 'GNU General Public License'
__version__ = '1.0'
22
__email__ = 'support.bioinfo.genotoul@inra.fr'
Jerome Mariette's avatar
Jerome Mariette committed
23
__status__ = 'beta'
24
25

import os
Penom Nom's avatar
Penom Nom committed
26
27
from configparser import ConfigParser
from .utils import *
28
import logging
Celine Noirot's avatar
Celine Noirot committed
29
30
31
32
33

from dateutil.relativedelta import relativedelta
import datetime
import time

34
35
36
class NG6ConfigReader(object):
    """
    Class NG6ConfigReader: this object read the config file and return the different configuration values
37
38
    """

39
    # The ng6 config file path
40
    CONFIG_FILE = os.path.join(os.path.dirname(__file__), "../../application.properties")
41
    PID = 5
42

43
44
45
    def __init__(self):
        """
        Build a ConfigReader
46
        """
47
        self.reader = ConfigParser()
48
        self.reader.read(self.CONFIG_FILE)
49
50


51
52
53
54
55
    def get_typo3_db_params(self):
        """
        return params needed to connect to the typo3 database
          @return: [host, user, passwd, dbname]
        """
56
        try:
Penom Nom's avatar
Penom Nom committed
57
58
59
60
61
62
            db_params = []
            db_params.append(self.reader.get('database', 'host'))
            db_params.append(self.reader.get('database', 'user'))
            db_params.append(self.reader.get('database', 'passwd'))
            db_params.append(self.reader.get('database', 'dbname'))
            return db_params
63
        except :
Celine Noirot's avatar
Celine Noirot committed
64
            raise Exception("Failed when parsing the config file - get_typo3_db_params !")
65
66
67
68
69
70
71


    def get_pid(self):
        """
        return the pid where to links data to
          @return: pid
        """
72
        return str(self.PID)
73
74
75
76
77
78
79
80


    def get_work_directory(self):
        """
        return the work directory
          @return: work_dir
        """
        try:
81
            return self.reader.get('storage', 'work_directory')
82
        except :
Celine Noirot's avatar
Celine Noirot committed
83
            raise Exception("Failed when parsing the config file - get_work_directory! ")
84
85
86
87
88
89
90
91


    def get_tmp_directory(self):
        """
        return the tmp directory
          @return: tmp_dir
        """
        try:
92
            return self.reader.get('storage', 'tmp_directory')
93
        except :
Celine Noirot's avatar
Celine Noirot committed
94
            raise Exception("Failed when parsing the config file get_tmp_directory !")
95

96
97
98
99
100
101
102

    def get_save_directory(self):
        """
        return the save directory
          @return: save_dir
        """
        try:
103
            return self.reader.get('storage', 'save_directory')
104
        except :
Celine Noirot's avatar
Celine Noirot committed
105
            raise Exception("Failed when parsing the config file get_save_directory !")
106

107
108
109
110
111
112
113
    def get_space_directory(self, space_id="default"):
        """
        return the directory corresponding to space_id
          @return: space_dir
        """
        try:
            return self.reader.get('space_'+space_id, 'dir_name').strip("/")
Celine Noirot's avatar
Celine Noirot committed
114
115
        except:
            raise Exception("Failed when parsing the config file !")
116

117
    def get_retention_date(self, space_id="default", creation_date = None):
Celine Noirot's avatar
Celine Noirot committed
118
        """
Romain Therville's avatar
Romain Therville committed
119
        return the retention corresponding to space_id
Celine Noirot's avatar
Celine Noirot committed
120
121
          @return: space_dir
        """
122
        
Celine Noirot's avatar
Celine Noirot committed
123
        date = None
124
125
126
127
        start_date= datetime.datetime.today()
        if creation_date is not None :
            start_date=datetime.datetime.fromtimestamp(creation_date)
            
Celine Noirot's avatar
Celine Noirot committed
128
129
        try :
            nb_month=self.reader.getint('space_'+space_id,"retention_period")
130
            retention_date = time.mktime((start_date+ relativedelta(months=nb_month)).timetuple())
Celine Noirot's avatar
Celine Noirot committed
131
132
133
134
135
136
137
138
            return retention_date
        except:
            try :
                (d,m,y)=self.reader.get('space_'+space_id,"retention_date").split('/')
                date = time.mktime(datetime.date(int(y),int(m),int(d)).timetuple())
                return date
            except:
                raise Exception("Failed when parsing the config file !")
Romain Therville's avatar
Romain Therville committed
139
        raise ValueError("Failed while generating retention date!")
140

Celine Noirot's avatar
Celine Noirot committed
141
    
142
143
144
145
146
147
148
149
150
151
152
    def get_available_spaces(self):
        """
        return a list of space_ids
          @return: a list containing the spaces strings 
        """
        available_spaces=[]
        for each_section in self.reader.sections():
            if each_section.startswith("space_") :
                available_spaces.append(each_section.replace('space_',''))        
        return available_spaces
    
153
154
155
156
157
158
    def get_log_file_path(self):
        """
        return the log file path
          @return:    the path to the log file
        """
        try:
159
            return self.reader.get('storage', 'log_file')
160
        except :
Celine Noirot's avatar
Celine Noirot committed
161
            raise Exception("Failed when parsing the config file !")
162
163
164
165
166
167
168
169
    def get_log_level(self):
        """
        return the level of log
          @return:    the level of logging (logging.DEBUG, logging.WARNING....
        """
        try:
            return self.reader.get('storage', 'log_level')
        except :
Celine Noirot's avatar
Celine Noirot committed
170
            raise Exception("Failed when parsing the config file !")
171
172


173
174
175
176
177
178
179
180
181
182
183
184
    def get_454_mids(self):
        """
        return the 454 mids list
          @return:    hash table with mids desriptions
        """
        try:
            mid_array = {}
            mids = self.reader.items("454_mids")
            for mid in mids:
                mid_array[mid[0]] = mid[1]
            return mid_array
        except :
Celine Noirot's avatar
Celine Noirot committed
185
            raise Exception("Failed when parsing the config file get_454_mids !")
186

187
188
189
190
191
192
193
194
195
196
197
198
199
200
    def get_10X_indexs(self):
        """
        return the 10X indexs list
            @return : hash table with 10X barcode list of index 
        """
        try:
            barcode_array = {}
            barcodes = self.reader.items("10X_barcodes")
            for barcode in barcodes:
                barcode_array[barcode[0].upper()] = barcode[1].upper()
            return barcode_array
        except : 
            raise Exception("Failed when parsin the config file for 10X barcodes !")

201
    def get_workflow_filters(self):
Penom Nom's avatar
Penom Nom committed
202
        """
203
            Return a list of workflow class names
Penom Nom's avatar
Penom Nom committed
204
205
206
            @return: list
        """
        filters = self.reader.get("local", 'runwf_filter')
207
208
209
        if filters :
            return filters.split()
        return []
210
211


Penom Nom's avatar
Penom Nom committed
212
    def get_user_base_directory(self):
Penom Nom's avatar
Penom Nom committed
213
214
215
216
217
        """
            Return an absolute path of a directory mounted on the web server
            @return: string
        """
        try:
Penom Nom's avatar
Penom Nom committed
218
            return self.reader.get('ui', 'user_base_directory')
Penom Nom's avatar
Penom Nom committed
219
        except :
Celine Noirot's avatar
Celine Noirot committed
220
            raise Exception("Could not retrieve server directory get_user_base_directory !")
221
222


Penom Nom's avatar
Penom Nom committed
223
224
225
    def get_server_parameters(self):
        """
        return the server parameters
226
          @return:    server_socket_host, server_socket_port, server_socket_protocol
Penom Nom's avatar
Penom Nom committed
227
        """
Celine Noirot's avatar
Celine Noirot committed
228
229
230
231
232
233
234
235
236
        try:
            server_params = []
            server_params.append(self.reader.get('global', 'server_socket_host'))
            server_params.append(self.reader.get('global', 'server_socket_port'))
            server_params.append(self.reader.get("global", 'server_socket_protocol'))
            return server_params
        except :
            raise Exception("Failed when parsing the config file get_server_parameters !")