Package commons ::
Module 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
15 """
16 Interface emulation for a checker
17 """
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
28 """
29 A concrete checker implementation with a logging.
30 Logger instance is a singleton of logging module
31 """
33 try:
34 single = _Logger()
35 except _Logger, l:
36 single = l
37 return single
38
42
44 """
45 set (change) default logger
46
47 @param logger: a new logger
48 @type logger: class Logger
49 """
50 self._log = logger
51
53 """
54 returns the logger instance
55 """
56 return self._log
57
59 """
60 Exception raised during check.
61
62 @ivar msg : message embeded in Execption class
63 @type msg: str
64
65 """
67 self.msg = msg
68 Exception.__init__(self, msg)
69
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
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 """
125 self.messages = messages
126 Exception.__init__(self, msg)
127
129
130 __single = None
131
141
144