fix bug in parse_bool
[bwmon.git] / bwmon / util.py
blob58fe636ee4f57f96daee64cffec1e456b95b39c5
1 # -*- coding: utf-8 -*-
3 import curses
4 import sys
5 import ConfigParser
7 def clear():
8 curses.setupterm()
9 sys.stdout.write(curses.tigetstr("clear"))
10 sys.stdout.flush()
12 def read_monitor_config(configfile):
13 config = ConfigParser.ConfigParser()
14 config.read(configfile)
15 for section in config.sections():
16 c = dict(config.items(section))
18 if c['type'] == 'monitor':
19 ignorelocal = parse_bool(c.get('ignorelocal', False))
20 import monitor
21 mon = monitor.Monitor(ignorelocal=ignorelocal)
22 inc = [c.get('include', '')]
23 exc = [c.get('exclude', '')]
24 mon.set_filter(inc, exc)
26 elif c['type'] == 'pipe':
27 import pipe
28 port = int(c['port'])
29 newhost = c['newhost']
30 newport = int(c['newport'])
32 mon = pipe.PipeMonitor(pipe.Pipe(port, newhost, newport))
33 #mon.set_shape(c.get('shape_threshold', 0))
35 else:
36 mon = None
37 print 'unknown monitor type %s' % c['type']
39 if mon:
40 yield mon
43 def parse_bool(val):
44 if isinstance(val, bool):
45 return val
47 if string.lower() == 'true':
48 return True
50 return False
53 def read_notification_config(configfile):
54 config = ConfigParser.ConfigParser()
55 config.read(configfile)
56 for section in config.sections():
57 c = dict(config.items(section))
58 yield ( c['process_filter'], int(c.get('in_threshold', 0)), int(c.get('out_threshold', 0)), int(c.get('interval', 1)), c.get('notification_command', '') )
61 class RingBuffer:
63 def __init__(self,size_max):
64 self.max = size_max
65 self.data = []
67 def append(self,x):
68 """append an element at the end of the buffer"""
69 self.data.append(x)
70 if len(self.data) == self.max:
71 self.cur=0
72 self.__class__ = RingBufferFull
74 def get(self):
75 """ return a list of elements from the oldest to the newest"""
76 return self.data
79 class RingBufferFull:
81 def __init__(self,n):
82 raise "don't initialize FullRingBuffer directly"
84 def append(self,x):
85 self.data[self.cur]=x
86 self.cur=(self.cur+1) % self.max
88 def get(self):
89 return self.data[self.cur:]+self.data[:self.cur]