configreader.py

Go to the documentation of this file.
00001 # This file is part of Fail2Ban.
00002 #
00003 # Fail2Ban is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # Fail2Ban is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with Fail2Ban; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 # Author: Cyril Jaquier
00018 # Modified by: Yaroslav Halchenko (SafeConfigParserWithIncludes)
00019 # $Revision: 686 $
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 # Gets the instance of the logger.
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     #@staticmethod
00043     def setBaseDir(folderName):
00044         path = folderName.rstrip('/')
00045         ConfigReader.BASE_DIRECTORY = path + '/'
00046     setBaseDir = staticmethod(setBaseDir)
00047         
00048     #@staticmethod
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     # Read the options.
00067     #
00068     # Read the given option in the configuration file. Default values
00069     # are used...
00070     # Each optionValues entry is composed of an array with:
00071     # 0 -> the type of the option
00072     # 1 -> the name of the option
00073     # 2 -> the default value for the option
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                 # No "Definition" section or wrong basedir
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
Generated on Sat May 18 03:01:45 2013 for Fail2Ban by  doxygen 1.6.3