From 616b87f3eaecaac725b83eb7acfa31db3fad9685 Mon Sep 17 00:00:00 2001 From: mfrasca <> Date: Mon, 2 Oct 2006 06:40:07 +0000 Subject: [PATCH] can't remember why I was modifying this source this way. probably to avoid the lambda which has been deprecated. if it does not work like this, please someone opens a (detailed) pr. --- pywmgeneric/pywmgeneric.py | 61 ++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/pywmgeneric/pywmgeneric.py b/pywmgeneric/pywmgeneric.py index d152d23..b3ba74c 100755 --- a/pywmgeneric/pywmgeneric.py +++ b/pywmgeneric/pywmgeneric.py @@ -42,6 +42,16 @@ import popen2 from pywmgeneral import pywmhelpers +prevStat = {'user':0, + 'nice':0, + 'sys':0, + 'idle':0, + 'total':0, + } +import re +cpuinfo = re.compile(r'^cpu[^ ]* +(?P[0-9]+) +(?P[0-9]+)' + r'+(?P[0-9]+) +(?P[0-9]+)') + class UserMethods: """Put methods that should be called when the action is method=... here. @@ -55,7 +65,7 @@ class UserMethods: An instance of this class is created at initialization and passed to all entries, so keep in mind that they share the same object. - THE METHODS ALLREADY HERE ARE JUST SAMPLES AND WILL PROBABLY NOT WORK + THE METHODS ALREADY HERE ARE JUST SAMPLES AND WILL PROBABLY NOT WORK WITH YOUR SYSTEM. """ @@ -63,36 +73,33 @@ class UserMethods: def getCpuTemp(self): def result(): + global prevStat try: f = file('/proc/stat', 'r') except IOError: - return lambda: 'error' - - import re - cpuinfo = re.compile(r'^cpu.* (?P[0-9]+) +(?P[0-9]+)' - r'+(?P[0-9]+) +(?P[0-9]+)') - match = dict([(k, int(v)) - for (k,v) in cpuinfo.match(f.readline()).groupdict().items()]) - totalTicks = ((match['user'] - self.userTicks) + - (match['sys'] - self.sysTicks) + - (match['nice'] - self.niceTicks) + - (match['idle'] - self.idleTicks)); - - if (totalTicks > 0): - user = (100. * (match['user'] - self.userTicks)) / totalTicks; - sys = (100. * (match['sys'] - self.sysTicks)) / totalTicks; - nice = (100. * (match['nice'] - self.niceTicks)) / totalTicks; - idle = (100. - (user + sys + nice)); - else: - user = sys = nice = idle = 0; - - self.userTicks = match['user'] - self.sysTicks = match['sys'] - self.niceTicks = match['nice'] - self.idleTicks = match['idle'] + return 'error' + currStat = dict( + [(k, int(v)) + for (k,v) in cpuinfo.match(f.readline()).groupdict().items()] + ) f.close() - return '%02.f/%02.f/%02.f' % (user, nice, sys) + + total = 0 + for k,v in currStat.items(): + total += v + currStat['total'] = total + totalTicks = (currStat['total'] - prevStat['total']) + + result = {} + if (totalTicks <= 0): + return '00/00/00' + + for k in prevStat: + result[k] = (100. * (currStat[k] - prevStat[k])) / totalTicks + prevStat = currStat + + return '%(user)02.f/%(sys)02.f/%(idle)02.f' % result return result def getSysTemp(self): @@ -605,6 +612,8 @@ def readConfigFile(fileName): def main(): clConfig = parseCommandLine(sys.argv) configFile = clConfig.get('configfile', defaultConfigFile) + if not configFile.count(os.sep): + configFile = os.sep.join(sys.argv[0].split(os.sep)[:-1]) + os.sep + configFile configFile = os.path.expanduser(configFile) config = readConfigFile(configFile) parseColors(defaultRGBFiles, clConfig, xpm)