add html user manual
[bwmon.git] / bwmon / util.py
blobbd47790e607ec6446767f7555ddd0162e328da6f
1 # -*- coding: utf-8 -*-
3 import curses
4 import sys
5 import ConfigParser
7 def clear():
8 """Clear the console using curses
10 This utility function empties the console ("clear").
11 """
12 curses.setupterm()
13 sys.stdout.write(curses.tigetstr("clear"))
14 sys.stdout.flush()
16 def read_monitor_config(configfile):
17 """TODO
19 @param configfile: TODO
20 @return: TODO
21 """
22 config = ConfigParser.ConfigParser()
23 config.read(configfile)
24 for section in config.sections():
25 c = dict(config.items(section))
27 if c['type'] == 'monitor':
28 ignorelocal = parse_bool(c.get('ignorelocal', False))
29 import monitor
30 mon = monitor.Monitor(ignorelocal=ignorelocal)
31 inc = [c.get('include', '')]
32 exc = [c.get('exclude', '')]
33 mon.set_filter(inc, exc)
35 elif c['type'] == 'pipe':
36 import pipe
37 port = int(c['port'])
38 newhost = c['newhost']
39 newport = int(c['newport'])
41 mon = pipe.PipeMonitor(pipe.Pipe(port, newhost, newport))
42 #mon.set_shape(c.get('shape_threshold', 0))
44 else:
45 mon = None
46 print 'unknown monitor type %s' % c['type']
48 if mon:
49 yield mon
52 def parse_bool(val):
53 """Convert a string to a boolean value
55 @param val: The string value (or boolean)
56 @return: True or False, depending on "val"
57 """
58 if isinstance(val, bool):
59 return val
61 if string.lower() == 'true':
62 return True
64 return False
67 def read_notification_config(configfile):
68 """TODO
70 @param configfile: TODO
71 @return: TODO
72 """
73 config = ConfigParser.ConfigParser()
74 config.read(configfile)
75 for section in config.sections():
76 c = dict(config.items(section))
77 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', '') )
80 class RingBuffer:
81 """TODO
82 """
84 def __init__(self,size_max):
85 """TODO
87 @param size_max: TODO
88 """
89 self.max = size_max
90 self.data = []
92 def append(self,x):
93 """Append an element at the end of the buffer
95 @param x: The element to append
96 """
97 self.data.append(x)
98 if len(self.data) == self.max:
99 self.cur=0
100 self.__class__ = RingBufferFull
102 def get(self):
103 """Get a list of contained elements
105 @return: A list of ements from oldest to newesta
107 return self.data
110 class RingBufferFull:
111 """TODO
114 def __init__(self, n):
115 """Don't initialize this object directly!"""
116 raise "don't initialize FullRingBuffer directly"
118 def append(self,x):
119 """Append an element at the end of the buffer
121 @param x: The element to append
123 self.data[self.cur]=x
124 self.cur=(self.cur+1) % self.max
126 def get(self):
127 """Get a list of contained elements
129 @return: A list of ements from oldest to newesta
131 return self.data[self.cur:]+self.data[:self.cur]