2 # -*- coding: utf-8 -*-
4 # Title: toolbox for szs script
5 # Date: Saturday 6th of November 2004
6 # Author: Norman Köhring
10 from os import chdir,getenv
11 from os.path import dirname,abspath
12 from os.path import exists as fexists
13 from time import strftime,time
15 chdir(dirname(abspath(argv[0])))
19 class Error(Exception):
20 """A general Exception which can be used/inherited
22 def __init__(self, reason):
25 return repr(self.reason)
30 """ all functions to give messages to whatever
31 this class contains: help, debug, say and log """
35 """ initializes the variables """
44 def usage(self, modname = None, modtxt = None):
45 """ prints a helpmessage to stdout
46 Usage: msg.usage([modulename [, moduletext ]])
49 print progname+progver
51 print "Module:"+ str(modname)
60 No parameters at the moment.
61 For configurations look at szs.cfg,
62 for more help look at README.
67 def debug(self, msg, lvl):
68 """ prints debugmessages if lvl >= debuglvl
69 Usage: debug(message, lvl, source)
70 Returns: True and prints the debugmessage """
72 if lvl <= debuglevel: # and x == 0:
73 src = inspect.getouterframes(inspect.currentframe())[1]
74 prefix = ['Critical: ', 'Warning: ', 'Debug: '][lvl-1]
75 MSG.log(self, prefix+str(src)+': '+msg)
76 print prefix+src[1].split('/')[-1].split('.')[0]+': '+src[3]+'(): '+msg
79 # if cache.get(src) <> None and msg not in cache.get(src)\
80 # or cache.get(src) == None:
81 # MSG.log(self, str(src)+": "+msg)
82 # print 'Debug from '+str(src)+': '+msg
84 # if cache.has_key(src):
85 # if cache.get(src).has_key(msg):
86 # cache.get(src).__setitem__(msg,cache.get(src).get(msg)+1)
88 # cache.get(src).__setitem__(msg,1)
90 # cache.__setitem__(src,{msg:1})
95 # MSG.log(self, str(src)+": "+msg)
96 # print 'Debug from '+str(src)+': '+msg
99 # MSG.log(self, "...", True)
106 def say(self, dict, msg):
107 """ dont use it... froze this funtion till next release """
109 print "DONT USE THIS FUNCTION (see README for more details)"
114 def log(self, msg, write=False, msgcache=[]):
115 """ sends messages to the configured logfile
116 should only be used by 'debug' """
119 time = strftime('%a%d%b%y %X')
120 if len(msg) > 0 and msg not in msgcache:
122 F = open(logfile, 'a')
123 F.writelines(msgcache)
126 msgcache.append(time+': '+msg+'\n')
131 """ functions for reading and writing the config """
134 def __init__(self, group):
139 cfgR = file(cfgF,'r')
140 self.cfgL = cfgR.readlines()
145 def __call__(self, group):
153 """ returns the actual settings
154 Usage: cfg.settings()
155 Returns: ['value','group','cfgF path'] """
158 return [self.value, self.group, cfgF.name]
162 def read(self, value):
163 """ reads the configurationfile and returns the values
164 Usage: cfg.read(value)
165 Returns: whats inside value """
171 for part in self.cfgL:
172 if part.find('#') < 0:
173 if part.split('=')[0] == self.group+'.'+self.value:
174 lst.append(part.split('=')[1].strip('\n'))
175 elif part.find('#') > 0:
176 part = part.split('#')[0]
177 if part.split('=')[0] == self.group+'.'+self.value:
178 lst.append(part.split('=')[1].strip('\n'))
188 def write(self, value):
189 """ this function does nothing at the moment
190 in future it writes into the configfiles
191 ...it is maybe planned for third release """
199 """ some extra functions like counters """
202 """ yeah... the init... """
207 def countadd(self, y=1, x=[0]):
208 """ counter for addition; Usage: xtra.countadd([y [,x ]])
209 where y (standard 1) is the value to add to x
210 Returns: the new value of x """
217 def countsub(self, y=1, x=[0]):
218 """ counter for substraction
219 Usage: xtra.countsub([y ],x ]),
220 where y (standard 1) is the value to substract from x
221 Returns: the new value of x """
228 def strrotate(self, t, l, x=[0]):
229 """ trying to build a string rotator
230 Usage: xtra.strrotate(str, length)
231 where 'str' is the String to rotate in
233 Returns: part of str """
240 ret = [x[0],t[x[0]:x[0]+l]]
245 def savevar(self, name, value):
246 """ saves variables in the cache, so your module
247 can use it later again...
248 Usage: xtra.savevar(name, value)
251 self.cache[name] = value
252 if self.cache.has_key(name):
259 def getvar(self, name):
260 """ returns the value of name
261 Usage: xtra.getvar(name)
262 Returns: value of name """
264 return self.cache[name]
268 def delvar(self, name):
269 """ deletes the variable from list
270 Usage: xtra.delvar(name)
274 if self.cache.has_key(name):
289 if fexists(getenv('HOME')+'/.wmi/szs.cfg'):
290 cfgF = getenv('HOME')+'/.wmi/szs.cfg'
297 logfile = cfgG.read('logfile').strip()
298 debuglevel = int(cfgG.read('debuglevel').strip())
299 accuracy = int(cfgG.read('accuracy').strip())
300 interval = float(cfgG.read('interval').strip())
302 # determine kernel version
303 fd = open('/proc/version')
304 kernel_version = fd.readline()[14:20]