Package commons :: Module Checker
[hide private]
[frames] | no frames]

Source Code for Module commons.Checker

  1  ''' 
  2  Created on 9 avr. 2009 
  3   
  4  @author: oinizan 
  5  ''' 
  6  import logging.config 
  7  import ConfigParser 
  8  from ConfigParser import NoSectionError 
  9  from ConfigParser import NoOptionError 
 10   
 11  LOG_FILE = "/home/oinizan/workspace/repet_pipe/pyRepetUnit/blaster/check/longconf.ini" 
 12  DEFAULT_LOGGER_NAME = "log02" 
 13   
14 -class IChecker():
15 """ 16 Interface emulation for a checker 17 """
18 - def check(self, arg=""):
19 """ 20 perform check, raise a CheckerException if error occured 21 22 @param arg: a collecting parameter: put here all you need to perform check 23 @type arg: choose the appropriate type 24 """ 25 pass
26
27 -class Checker (IChecker):
28 """ 29 A concrete checker implementation with a logging. 30 Logger instance is a singleton of logging module 31 """
32 - def _handle (self):
33 try: 34 single = _Logger() 35 except _Logger, l: 36 single = l 37 return single
38
39 - def __init__(self):
40 logger = self._handle() 41 self._log = logger.getLogger()
42
43 - def setLogger(self, logger):
44 """ 45 set (change) default logger 46 47 @param logger: a new logger 48 @type logger: class Logger 49 """ 50 self._log = logger
51
52 - def getLogger(self):
53 """ 54 returns the logger instance 55 """ 56 return self._log
57
58 -class CheckerException(Exception):
59 """ 60 Exception raised during check. 61 62 @ivar msg : message embeded in Execption class 63 @type msg: str 64 65 """
66 - def __init__(self,msg=""):
67 self.msg = msg 68 Exception.__init__(self, msg)
69
70 -class ConfigChecker (IChecker):
71 """ 72 A checker for config file. 73 @ivar sectionName: section to check in config file 74 @type sectionName: str 75 76 @ivar optionsDict: dict with option to check as keys and empty strings "" as values 77 @type optionsDict: class Dict 78 """ 79
80 - def __init__ (self, sectionName, optionsDict):
81 self._sectionName = sectionName 82 self._optionsDict = optionsDict
83 """ 84 Check options stored in optionsDict. 3 checks are performed: 85 (i) config file existence, (ii) section existence, (iii) options existences 86 If a check fail, raise a ConfigException with a list of messages 87 """
88 - def check (self, configFile):
89 config = ConfigParser.ConfigParser() 90 msg = [] 91 try: 92 config.readfp( open(configFile) ) 93 except IOError, e: 94 msg.append("CONFIG FILE not found - " + e.message) 95 raise ConfigException("", msg) 96 97 if not (config.has_section(self._sectionName)): 98 msg.append("[" + self._sectionName + "]" + " section not found - ") 99 raise ConfigException("", msg) 100 101 isExceptionOccured = False 102 for key in self._optionsDict.keys(): 103 try: 104 self._optionsDict[key] = config.get(self._sectionName, key) 105 except NoOptionError, e: 106 msg.append("[" + self._sectionName + "]" + " - " + e.message) 107 isExceptionOccured = True 108 109 if (isExceptionOccured): 110 raise ConfigException("", msg)
111
112 -class ConfigException(Exception):
113 114 """ 115 A exception raised by check method class ConfigChecker 116 117 @ivar msg : message embeded in Execption class 118 @type msg: str 119 120 @ivar messages: list of messages 121 @type messages: Class list 122 123 """
124 - def __init__(self,msg, messages=[]):
125 self.messages = messages 126 Exception.__init__(self, msg)
127
128 -class _Logger:
129 130 __single = None 131
132 - def __init__( self, loggerName ="" ):
133 if _Logger.__single: 134 raise _Logger.__single 135 logging.config.fileConfig(LOG_FILE) 136 if (loggerName != ""): 137 self.__log = logging.getLogger(loggerName) 138 else: 139 self.__log = logging.getLogger(DEFAULT_LOGGER_NAME) 140 _Logger.__single = self
141
142 - def getLogger(self):
143 return self.__log
144