13
|
1 # Copyright INRA (Institut National de la Recherche Agronomique)
|
|
2 # http://www.inra.fr
|
|
3 # http://urgi.versailles.inra.fr
|
|
4 #
|
|
5 # This software is governed by the CeCILL license under French law and
|
|
6 # abiding by the rules of distribution of free software. You can use,
|
|
7 # modify and/ or redistribute the software under the terms of the CeCILL
|
|
8 # license as circulated by CEA, CNRS and INRIA at the following URL
|
|
9 # "http://www.cecill.info".
|
|
10 #
|
|
11 # As a counterpart to the access to the source code and rights to copy,
|
|
12 # modify and redistribute granted by the license, users are provided only
|
|
13 # with a limited warranty and the software's author, the holder of the
|
|
14 # economic rights, and the successive licensors have only limited
|
|
15 # liability.
|
|
16 #
|
|
17 # In this respect, the user's attention is drawn to the risks associated
|
|
18 # with loading, using, modifying and/or developing or reproducing the
|
|
19 # software by the user in light of its specific status of free software,
|
|
20 # that may mean that it is complicated to manipulate, and that also
|
|
21 # therefore means that it is reserved for developers and experienced
|
|
22 # professionals having in-depth computer knowledge. Users are therefore
|
|
23 # encouraged to load and test the software's suitability as regards their
|
|
24 # requirements in conditions enabling the security of their systems and/or
|
|
25 # data to be ensured and, more generally, to use and operate it in the
|
|
26 # same conditions as regards security.
|
|
27 #
|
|
28 # The fact that you are presently reading this means that you have had
|
|
29 # knowledge of the CeCILL license and that you accept its terms.
|
|
30
|
|
31
|
|
32 import ConfigParser
|
|
33 from ConfigParser import NoOptionError
|
|
34 from commons.core.checker.IChecker import IChecker
|
|
35 from commons.core.checker.ConfigException import ConfigException
|
|
36
|
|
37
|
|
38 ## A checker for a configuration file
|
|
39 #
|
|
40 #
|
|
41 # A configuration file is formatted as follow:
|
|
42 #
|
|
43 # [section1]
|
|
44 #
|
|
45 # option_name1: option_value1
|
|
46 #
|
|
47 # option_name2: option_value2
|
|
48 #
|
|
49 # option_name3: option_value3
|
|
50 #
|
|
51 # [section2]
|
|
52 #
|
|
53 # ...
|
|
54 #
|
|
55 #
|
|
56 # This class performs 3 checkes on a configuration file:
|
|
57 #
|
|
58 # (i) check if file exists
|
|
59 #
|
|
60 # (ii) check if section exists
|
|
61 #
|
|
62 # (iii) check if option exists
|
|
63 #
|
|
64 class ConfigChecker( IChecker ):
|
|
65
|
|
66 ## Constructor A checker for configuration file.
|
|
67 #
|
|
68 # @param sectionName name of section to check in configuration file
|
|
69 # @param optionsDict dictionary with option(s) to check as keys and empty strings ("") as values
|
|
70 def __init__ (self, sectionName, optionsDict):
|
|
71 self._sectionName = sectionName
|
|
72 self._optionsDict = optionsDict
|
|
73
|
|
74
|
|
75 ## Perform 3 checks : file exists, sections exists, option exists
|
|
76 #
|
|
77 # @param configFile configuration file to check
|
|
78 # @exception ConfigException with a list of messages
|
|
79 def check (self, configFile):
|
|
80 config = ConfigParser.ConfigParser()
|
|
81 msg = []
|
|
82 try:
|
|
83 config.readfp( open(configFile) )
|
|
84 except IOError, e:
|
|
85 msg.append("CONFIG FILE not found - " + e.message)
|
|
86 raise ConfigException("", msg)
|
|
87
|
|
88 if not (config.has_section(self._sectionName)):
|
|
89 msg.append("[" + self._sectionName + "]" + " section not found - ")
|
|
90 raise ConfigException("", msg)
|
|
91
|
|
92 isExceptionOccured = False
|
|
93 for key in self._optionsDict.keys():
|
|
94 try:
|
|
95 self._optionsDict[key] = config.get(self._sectionName, key)
|
|
96 except NoOptionError, e:
|
|
97 msg.append("[" + self._sectionName + "]" + " - " + e.message)
|
|
98 isExceptionOccured = True
|
|
99
|
|
100 if (isExceptionOccured):
|
|
101 raise ConfigException("", msg)
|