Mercurial > repos > yufei-luo > s_mart
comparison commons/core/checker/ConfigChecker.py @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
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 re | |
33 import sys | |
34 from commons.core.utils.RepetConfigParser import RepetConfigParser | |
35 from commons.core.checker.ConfigValue import ConfigValue | |
36 from commons.core.checker.IChecker import IChecker | |
37 from commons.core.checker.RepetException import RepetException | |
38 from commons.core.utils.FileUtils import FileUtils | |
39 | |
40 | |
41 class Rule(object): | |
42 | |
43 def __init__(self, mandatory= False, isPattern=False, type="", set=(), help =""): | |
44 self.mandatory = mandatory | |
45 self.isPattern = isPattern | |
46 self.type = type | |
47 self.set = set | |
48 self.help = help | |
49 | |
50 class ConfigRules(object): | |
51 | |
52 def __init__(self, configName = "", configDescription = ""): | |
53 self.configName = configName | |
54 self.configDescription = configDescription | |
55 self.dRules4Sections={} | |
56 | |
57 def _addRule(self, section, option="DEFAULT", mandatory=False, isPattern=False, type="", set=(), help =""): | |
58 if not self.dRules4Sections.has_key(section): | |
59 self.dRules4Sections[section] = {} | |
60 self.dRules4Sections[section][option]=Rule(mandatory, isPattern, type.lower(), set) | |
61 | |
62 def addRuleSection(self, section, mandatory=False, isPattern=False, help = ""): | |
63 self._addRule(section = section, option = "DEFAULT", mandatory = mandatory, isPattern = isPattern, help = "") | |
64 | |
65 def addRuleOption(self, section, option, mandatory=False, isPattern=False, type="", set=(), help = ""): | |
66 self._addRule(section = section, option = option, mandatory = mandatory, isPattern = isPattern, type = type, set=set , help = "") | |
67 | |
68 def isSectionMandatory(self, section): | |
69 if self.dRules4Sections.has_key(section): | |
70 if self.dRules4Sections[section].has_key("DEFAULT"): | |
71 return self.dRules4Sections[section]["DEFAULT"].mandatory | |
72 return False | |
73 | |
74 def isOptionMandatory(self, section, option): | |
75 if self.dRules4Sections.has_key(section): | |
76 if self.dRules4Sections[section].has_key(option): | |
77 return self.dRules4Sections[section][option].mandatory | |
78 return False | |
79 | |
80 def getRule(self, section, option): | |
81 if self.dRules4Sections.has_key(section): | |
82 if self.dRules4Sections[section].has_key(option): | |
83 return self.dRules4Sections[section][option] | |
84 return None | |
85 | |
86 class ConfigChecker(IChecker): | |
87 | |
88 def __init__ (self, cfgFileName, iCfgRules): | |
89 self._configFileName = cfgFileName | |
90 self._iConfigRules = iCfgRules | |
91 self._iRawConfig = ConfigValue() | |
92 self._iExtendedConfigRules = ConfigRules() | |
93 | |
94 def readConfigFile(self): | |
95 iConfig = RepetConfigParser() | |
96 try: | |
97 iConfig.readfp(open(self._configFileName)) | |
98 return iConfig | |
99 # TODO USE OF CONFIG ERROR | |
100 # if DuplicateSectionError: | |
101 # raise Exception ("Duplicate section exist in config file %s" %(self._configFileName )) | |
102 except : | |
103 raise RepetException ("Unexpected error: %s" % sys.exc_info()[0]) | |
104 | |
105 def setRawConfig(self, iConfig ): | |
106 for sectionName in iConfig.sections(): | |
107 for optionName in iConfig.options(sectionName): | |
108 optionValue = iConfig.get(sectionName, optionName) | |
109 self._iRawConfig.set(sectionName, optionName, optionValue) | |
110 | |
111 def getOptionValueAccordingRule(self, iConfig, sectionName, optionName): | |
112 optionRule = self._iExtendedConfigRules.getRule(sectionName, optionName) | |
113 if optionRule == None : | |
114 return iConfig.get(sectionName, optionName) | |
115 | |
116 if optionRule.type == "int": | |
117 optionValue = iConfig.getint(sectionName, optionName) | |
118 elif optionRule.type == "float": | |
119 optionValue = iConfig.getfloat(sectionName, optionName) | |
120 elif optionRule.type == "bool" or optionRule.type == "boolean": | |
121 optionValue = iConfig.getboolean(sectionName, optionName) | |
122 else: | |
123 optionValue = iConfig.get(sectionName, optionName) | |
124 if optionRule.set!=() and not(optionValue in optionRule.set): | |
125 #TODO : test and fix | |
126 raise RepetException ("value must be in %s " % set.__repr__()) | |
127 | |
128 return optionValue | |
129 | |
130 def setConfig(self, iConfig ): | |
131 config = ConfigValue() | |
132 valueErr = "" | |
133 for sectionName in iConfig.sections(): | |
134 for optionName in iConfig.options(sectionName): | |
135 try: | |
136 optionValue = self.getOptionValueAccordingRule(iConfig, sectionName, optionName ) | |
137 config.set(sectionName, optionName, optionValue) | |
138 except RepetException, re : | |
139 #TODO : test and fix | |
140 valueErr += "\n - [%s]: %s %s" % re.getMessage() | |
141 if valueErr == "": | |
142 self._iRawConfig = config | |
143 else: | |
144 raise RepetException ("Following errors occurs:%s\n" %valueErr) | |
145 | |
146 def checkIfExistsConfigFile (self): | |
147 if not (FileUtils.isRessourceExists(self._configFileName)): | |
148 raise RepetException("CONFIG FILE not found - '%s'" % self._configFileName) | |
149 | |
150 def checkMandatorySections (self): | |
151 missingSection = "" | |
152 for sectionName in self._iExtendedConfigRules.dRules4Sections.keys(): | |
153 if self._iExtendedConfigRules.isSectionMandatory(sectionName) and not self._iRawConfig.has_section(sectionName): | |
154 missingSection += "\n - %s" %(sectionName) | |
155 if missingSection != "": | |
156 raise RepetException ("Error in configuration file %s, following sections are missing:%s\n" % (self._configFileName, missingSection)) | |
157 | |
158 def checkMandatoryOptions (self): | |
159 missingOption = "" | |
160 for sectionName in self._iExtendedConfigRules.dRules4Sections.keys(): | |
161 if self._iExtendedConfigRules.isSectionMandatory(sectionName) or self._iRawConfig.has_section(sectionName) : | |
162 dRules4OptionsOfThisSection = self._iExtendedConfigRules.dRules4Sections[sectionName] | |
163 for optionName in dRules4OptionsOfThisSection.keys(): | |
164 if optionName != "DEFAULT" and self._iExtendedConfigRules.isOptionMandatory(sectionName, optionName) and not self._iRawConfig.has_option(sectionName, optionName): | |
165 missingOption += "\n - [%s]: %s" % (sectionName, optionName) | |
166 if missingOption != "": | |
167 raise RepetException ("Error in configuration file %s, following options are missing: %s\n" % (self._configFileName, missingOption)) | |
168 | |
169 def getSectionNamesAccordingPatternRules (self, sectionWordOrPattern, isPattern): | |
170 lSectionsFoundAccordingPatternRules=[] | |
171 if isPattern == False: | |
172 if self._iRawConfig.has_section(sectionWordOrPattern): | |
173 lSectionsFoundAccordingPatternRules.append(sectionWordOrPattern) | |
174 else: | |
175 for sectionName in self._iRawConfig.sections(): | |
176 if re.search(sectionWordOrPattern, sectionName, re.IGNORECASE): | |
177 lSectionsFoundAccordingPatternRules.append(sectionName) | |
178 return lSectionsFoundAccordingPatternRules | |
179 | |
180 def getOptionsNamesAccordingPatternRules(self, sectionName, optionWordOrPattern, isPattern): | |
181 lOptionsFoundAccordingPatternRules=[] | |
182 if isPattern == False: | |
183 if self._iRawConfig.has_option(sectionName, optionWordOrPattern): | |
184 lOptionsFoundAccordingPatternRules.append(optionWordOrPattern) | |
185 else : | |
186 for optionName in self._iRawConfig.options(sectionName): | |
187 if re.search(optionWordOrPattern, optionName, re.IGNORECASE)!= None: | |
188 lOptionsFoundAccordingPatternRules.append(optionName) | |
189 return lOptionsFoundAccordingPatternRules | |
190 | |
191 def extendConfigRulesWithPatternRules(self): | |
192 for sectionName in self._iConfigRules.dRules4Sections.keys(): | |
193 dRules4OptionsOfThisSection = self._iConfigRules.dRules4Sections[sectionName] | |
194 lRawSections=[] | |
195 if dRules4OptionsOfThisSection.has_key("DEFAULT"): | |
196 mandatorySection = dRules4OptionsOfThisSection["DEFAULT"].mandatory | |
197 isPatternSection = dRules4OptionsOfThisSection["DEFAULT"].isPattern | |
198 lRawSections=self.getSectionNamesAccordingPatternRules(sectionName, isPatternSection) | |
199 for rawSectionName in lRawSections: | |
200 self._iExtendedConfigRules.addRuleSection(rawSectionName, "DEFAULT", mandatorySection ) | |
201 if mandatorySection and (len(lRawSections)==0): | |
202 self._iExtendedConfigRules.addRuleSection(sectionName, "DEFAULT", mandatorySection ) | |
203 else: | |
204 lRawSections.append(sectionName) | |
205 for optionName in dRules4OptionsOfThisSection.keys(): | |
206 setOption = dRules4OptionsOfThisSection[optionName].set | |
207 isPatternOption = dRules4OptionsOfThisSection[optionName].isPattern | |
208 mandatoryOption = dRules4OptionsOfThisSection[optionName].mandatory | |
209 typeOption = dRules4OptionsOfThisSection[optionName].type | |
210 if optionName != "DEFAULT": | |
211 for rawSectionName in lRawSections: | |
212 lRawOptions=self.getOptionsNamesAccordingPatternRules(rawSectionName, optionName, isPatternOption) | |
213 for rawOptionName in lRawOptions: | |
214 self._iExtendedConfigRules.addRuleOption(rawSectionName, rawOptionName, mandatoryOption, False, typeOption, setOption) | |
215 if mandatoryOption and (len(lRawOptions)==0): | |
216 self._iExtendedConfigRules.addRuleOption(rawSectionName, optionName, mandatoryOption, False, typeOption, setOption) | |
217 | |
218 def getConfig(self): | |
219 self.checkIfExistsConfigFile() | |
220 iConfig = self.readConfigFile() | |
221 self.setRawConfig(iConfig) | |
222 self.extendConfigRulesWithPatternRules() | |
223 self.checkMandatorySections() | |
224 self.checkMandatoryOptions() | |
225 self.setConfig(iConfig) | |
226 return self._iRawConfig |