configreader.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 __author__ = "Cyril Jaquier"
00022 __version__ = "$Revision: 686 $"
00023 __date__ = "$Date: 2008-04-13 19:48:52 +0200 (Sun, 13 Apr 2008) $"
00024 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00025 __license__ = "GPL"
00026
00027 import logging, os
00028 from configparserinc import SafeConfigParserWithIncludes
00029 from ConfigParser import NoOptionError, NoSectionError
00030
00031
00032 logSys = logging.getLogger("fail2ban.client.config")
00033
00034 class ConfigReader(SafeConfigParserWithIncludes):
00035
00036 BASE_DIRECTORY = "/etc/fail2ban/"
00037
00038 def __init__(self):
00039 SafeConfigParserWithIncludes.__init__(self)
00040 self.__opts = None
00041
00042
00043 def setBaseDir(folderName):
00044 path = folderName.rstrip('/')
00045 ConfigReader.BASE_DIRECTORY = path + '/'
00046 setBaseDir = staticmethod(setBaseDir)
00047
00048
00049 def getBaseDir():
00050 return ConfigReader.BASE_DIRECTORY
00051 getBaseDir = staticmethod(getBaseDir)
00052
00053 def read(self, filename):
00054 basename = ConfigReader.BASE_DIRECTORY + filename
00055 logSys.debug("Reading " + basename)
00056 bConf = basename + ".conf"
00057 bLocal = basename + ".local"
00058 if os.path.exists(bConf) or os.path.exists(bLocal):
00059 SafeConfigParserWithIncludes.read(self, [bConf, bLocal])
00060 return True
00061 else:
00062 logSys.error(bConf + " and " + bLocal + " do not exist")
00063 return False
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 def getOptions(self, sec, options, pOptions = None):
00076 values = dict()
00077 for option in options:
00078 try:
00079 if option[0] == "bool":
00080 v = self.getboolean(sec, option[1])
00081 elif option[0] == "int":
00082 v = self.getint(sec, option[1])
00083 else:
00084 v = self.get(sec, option[1])
00085 if not pOptions == None and option[1] in pOptions:
00086 continue
00087 values[option[1]] = v
00088 except NoSectionError, e:
00089
00090 logSys.error(e)
00091 values[option[1]] = option[2]
00092 except NoOptionError:
00093 if not option[2] == None:
00094 logSys.warn("'%s' not defined in '%s'. Using default value"
00095 % (option[1], sec))
00096 values[option[1]] = option[2]
00097 except ValueError:
00098 logSys.warn("Wrong value for '" + option[1] + "' in '" + sec +
00099 "'. Using default one: '" + `option[2]` + "'")
00100 values[option[1]] = option[2]
00101 return values