"Fossies" - the Fresh Open Source Software Archive

Member "svnchecker-0.3/modules/Config.py" (15 Jul 2008, 6389 Bytes) of package /linux/privat/old/svnchecker-0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "Config.py" see the Fossies "Dox" file reference documentation.

    1 # pylint: disable-msg=W0201, W0704
    2 
    3 # Copyright 2008 German Aerospace Center (DLR)
    4 #
    5 # Licensed under the Apache License, Version 2.0 (the "License");
    6 # you may not use this file except in compliance with the License.
    7 # You may obtain a copy of the License at
    8 #
    9 #     http://www.apache.org/licenses/LICENSE-2.0
   10 #
   11 # Unless required by applicable law or agreed to in writing, software
   12 # distributed under the License is distributed on an "AS IS" BASIS,
   13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14 # See the License for the specific language governing permissions and
   15 # limitations under the License.
   16 
   17 """ Class to work with the configuration. """
   18 
   19 
   20 import os.path
   21 import ConfigParser
   22 
   23 DEFAULTSECT = "Default"
   24 ConfigParser.DEFAULTSECT = DEFAULTSECT
   25 
   26 
   27 class NoSuchConfigurationValueError(Exception):
   28     def __init__(self, var):
   29         Exception.__init__(self, """Could not find configuration option %r in the configuration files.
   30 Please take a look at the documentation for a description of this option.""" % var)
   31 
   32 
   33 class Config:
   34     def __init__(self, globalConfigFilename, localConfigFilename):
   35         """ Initialize the Config object. """
   36 
   37         self.configFiles = []
   38 
   39         configFilenames = [globalConfigFilename, localConfigFilename]
   40         
   41         for filename in configFilenames:
   42             if filename is not None and os.path.exists(filename):
   43                 try:
   44                     configFile = ConfigParser.ConfigParser()
   45                     configFile.read(filename)
   46                     self.configFiles.append(configFile)
   47                 except ConfigParser.ParsingError:
   48                     pass
   49 
   50         if len(self.configFiles) == 0:
   51             raise Exception("""Could not find a valid configuration file named svncherconfig.ini.
   52 You can use a global file in your svnchecker directory and/or a local one in your hooks directory.""")
   53 
   54         self.profile = ConfigParser.DEFAULTSECT
   55         self.hooksLocation = ""
   56 
   57     def __getVar(self, var):
   58         """ This is the variable resolver. """
   59         if not "." in var:
   60             raise NoSuchConfigurationValueError(var)
   61 
   62         value = None
   63 
   64         for configFile in self.configFiles:
   65             if configFile.has_option(self.profile, var):
   66                 value = configFile.get(self.profile, var)
   67             elif configFile.has_option(self.profile, "Main." + var.split(".")[1]):
   68                 value = configFile.get(self.profile, "Main." + var.split(".")[1])
   69             elif configFile.has_option(ConfigParser.DEFAULTSECT, var):
   70                 value = configFile.get(ConfigParser.DEFAULTSECT, var)
   71             elif configFile.has_option(ConfigParser.DEFAULTSECT, "Main." + var.split(".")[1]):
   72                 value = configFile.get(ConfigParser.DEFAULTSECT, "Main." + var.split(".")[1])
   73 
   74             if value is not None:
   75                 break
   76 
   77         if value is not None:
   78             return value.replace("%HOOKS%", self.hooksLocation)
   79         else:
   80             raise NoSuchConfigurationValueError(var)
   81 
   82     def setHooksLocation(self, location):
   83         """
   84         Sets the repository location in order to replace %HOOKS% by it in the returned values.
   85         """
   86         self.hooksLocation = location
   87 
   88     def setProfile(self, profile):
   89         """
   90         Sets the used profile.
   91         Do NOT use in your checks or handlers.
   92         """
   93         self.profile = profile
   94 
   95     def getProfiles(self):
   96         """
   97         Returns the available profiles.
   98         Do NOT use in your checks or handlers.
   99         """
  100         sections = []
  101         for configFile in self.configFiles:
  102             for section in configFile.sections():
  103                 if section not in sections and section != ConfigParser.DEFAULTSECT:
  104                     sections.append(section)
  105         return sections
  106 
  107     def getString(self, var, default=None):
  108         """
  109         Returns a variable as string. If the
  110         variable does not exist and default is
  111         set default will be returned, otherwise
  112         a NoSuchConfigurationValueError will be
  113         raised.
  114         """
  115         try:
  116             return self.__getVar(var)
  117         except NoSuchConfigurationValueError, e:
  118             if default is not None:
  119                 return default
  120             else:
  121                 raise e
  122 
  123     def getArray(self, var, default=None):
  124         """
  125         Returns a variable as array. The
  126         configuration string is split by the ','
  127         character. If the variable does not
  128         exist and default is set default will be
  129         returned, otherwise a
  130         NoSuchConfigurationValueError will be
  131         raised.
  132         """
  133         try:
  134             string = self.__getVar(var)
  135             return [x.strip() for x in string.split(",") if x.strip() != ""]
  136         except NoSuchConfigurationValueError, e:
  137             if default is not None:
  138                 return default
  139             else:
  140                 raise e
  141 
  142     def getBoolean(self, var, default=None):
  143         """ 
  144         Returns a variable as boolean. True,
  145         true, 1 will return True. False, false,
  146         0 will return False. If the variable
  147         does not match the pattern a ValueError
  148         will be raised.If the variable does not
  149         exist and default is set default will be
  150         returned, otherwise a
  151         NoSuchConfigurationValueError will be
  152         raised.
  153         """
  154         try:
  155             var = self.__getVar(var)
  156             if var in ["True", "true", "1"]:
  157                 return True
  158             elif var in ["False", "false", "0"]:
  159                 return False
  160             raise ValueError('Not a boolean: %s' % var)
  161         except NoSuchConfigurationValueError, e:
  162             if default is not None:
  163                 return default
  164             else:
  165                 raise e
  166 
  167     def getInteger(self, var, default=None):
  168         """
  169         Returns a variable as integer. If the
  170         variable cannot be converted to an
  171         integer a ValueError will be raised. If
  172         the variable does not exist and default
  173         is set default will be returned,
  174         otherwise a
  175         NoSuchConfigurationValueError will be
  176         raised.
  177         """
  178         try:
  179             var = self.__getVar(var)
  180             return int(var)
  181         except NoSuchConfigurationValueError, e:
  182             if default is not None:
  183                 return default
  184             else:
  185                 raise e