Version 0.1.1.
[SysBars.git] / plugins / cpu.py
blob3306202bee16b331defb6d889d76009f91fd5153
1 import os
3 import gtk as g
5 import addedit
6 from addedit import VSPACING, HSPACING
7 import bardesc
8 from choices import settings
9 import plugins
11 plugins.types['cpu'] = _("CPU Load")
14 platform = plugins.get_platform()
17 def get_n_cpus():
18 try:
19 return os.sysconf("SC_NPROCESSORS_ONLN")
20 except:
21 raise
22 # FIXME: SC_NPROCESSORS_ONLN may not be standard, try /proc/stat
23 return 1
26 class Description(bardesc.Description):
27 def __init__(self, num):
28 bardesc.Description.__init__(self, num)
29 self.type = 'cpu'
30 self.cpu = settings.get_bar_int(num, 'cpu', -1)
32 def get_name(self):
33 if self.cpu == -1:
34 return _("All CPUs")
35 else:
36 return _("CPU %d") % self.cpu
38 def change_num(self, num):
39 bardesc.Description.change_num(self, num)
40 settings.set_bar(num, 'cpu', self.cpu)
42 def get_range(self):
43 return platform.get_cpu_load_range()
45 def get_reading(self):
46 return platform.get_cpu_load(self.cpu)
50 class Dialog(addedit.Dialog):
51 def get_type_name(self):
52 return _("CPU Load")
54 def layout_for_type(self):
55 hbox = g.HBox(False)
56 hbox.pack_start(self.make_label(_("CPU")) , False, True, HSPACING)
57 self.cpu_w = g.combo_box_new_text()
58 self.cpu_w.append_text(_("All"))
59 for n in range(get_n_cpus()):
60 self.cpu_w.append_text(str(n))
61 self.cpu_w.set_active(self.desc.cpu + 1)
62 hbox.pack_start(self.cpu_w, True, True, HSPACING)
63 self.vbox.pack_start(hbox, True, True, VSPACING)
64 if self.instant_apply:
65 self.cpu_w.connect("changed", self.cpu_changed_cb)
67 def apply(self):
68 self.cpu_changed_cb()
69 addedit.Dialog.apply(self)
71 def cpu_changed_cb(self, w = None):
72 cpu = self.cpu_w.get_active()
73 if cpu < 0:
74 cpu = -1
75 else:
76 cpu -= 1
77 self.desc.cpu = cpu
78 settings.set_bar(self.desc.num, 'cpu', cpu)
79 if self.instant_apply:
80 self.bar_w.whole_redraw()
81 self.parent_win.update_bar_name(self.desc)
83 def get_default_label(self):
84 return _("C")