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