config_reader.py 8.07 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

Celine Noirot's avatar
Celine Noirot committed
117
118
    def get_retention_date(self, space_id="default"):
        """
Romain Therville's avatar
Romain Therville committed
119
        return the retention corresponding to space_id
Celine Noirot's avatar
Celine Noirot committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
          @return: space_dir
        """
        date = None
        try :
            nb_month=self.reader.getint('space_'+space_id,"retention_period")
            retention_date = time.mktime((datetime.datetime.today()+ relativedelta(months=nb_month)).timetuple())
            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
134
135
        raise ValueError("Failed while generating retention date!")
    
136
    def compute_new_retention_date(self, space_id="default", creation_date = None):
Romain Therville's avatar
Romain Therville committed
137
        """
138
139
        return the retention corresponding to space_id and crdate
          @return: unix epoch timestamp retention value
Romain Therville's avatar
Romain Therville committed
140
141
142
143
        """
        date = None
        try :
            nb_month=self.reader.getint('space_'+space_id,"retention_period")
144
145
            retention_date = time.mktime((datetime.datetime.fromtimestamp(creation_date)+ relativedelta(months=nb_month)).timetuple())
            logging.getLogger("config_reader.compute_new_retention_date").debug("IN retention period")
Romain Therville's avatar
Romain Therville committed
146
147
148
149
150
            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())
151
                logging.getLogger("config_reader.compute_new_retention_date").debug("IN retention date")
Romain Therville's avatar
Romain Therville committed
152
153
154
155
                return date
            except:
                raise Exception("Failed when parsing the config file !")
        raise ValueError("Failed while generating retention date!")    
Celine Noirot's avatar
Celine Noirot committed
156
    
157
158
159
160
161
162
    def get_log_file_path(self):
        """
        return the log file path
          @return:    the path to the log file
        """
        try:
163
            return self.reader.get('storage', 'log_file')
164
        except :
Celine Noirot's avatar
Celine Noirot committed
165
            raise Exception("Failed when parsing the config file !")
166
167
168
169
170
171
172
173
    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
174
            raise Exception("Failed when parsing the config file !")
175
176


177
178
179
180
181
182
183
184
185
186
187
188
    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
189
            raise Exception("Failed when parsing the config file get_454_mids !")
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
    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 !")

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


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


Penom Nom's avatar
Penom Nom committed
227
228
229
    def get_server_parameters(self):
        """
        return the server parameters
230
          @return:    server_socket_host, server_socket_port, server_socket_protocol
Penom Nom's avatar
Penom Nom committed
231
        """
Celine Noirot's avatar
Celine Noirot committed
232
233
234
235
236
237
238
239
240
        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 !")