Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# -*- coding: utf-8 -*-
# minimal sample showing how to write GTK3 indicator for Ubuntu Unity
# copyright 2012 Charl P. Botha <info@charlbotha.com>
# hereby released under the BSD license.
# use the PyGObject GObject introspection to use GTK+ 3
# also see
# http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html
# http://developer.gnome.org/gtk3/stable/ (API reference)
from gi.repository import Gtk, GLib
try:
from gi.repository import AppIndicator3 as AppIndicator
except:
from gi.repository import AppIndicator
import re
class IndicatorCPUSpeed:
def __init__(self):
# param1: identifier of this indicator
# param2: name of icon. this will be searched for in the standard them
# dirs
# finally, the category. We're monitoring CPUs, so HARDWARE.
self.ind = AppIndicator.Indicator.new(
"indicator-cpuspeed",
"dialog-information",
AppIndicator.IndicatorCategory.HARDWARE)
# some more information about the AppIndicator:
# http://developer.ubuntu.com/api/ubuntu-12.04/python/AppIndicator3-0.1.html
# http://developer.ubuntu.com/resources/technologies/application-indicators/
# need to set this for indicator to be shown
self.ind.set_status (AppIndicator.IndicatorStatus.ACTIVE)
# have to give indicator a menu
self.menu = Gtk.Menu()
# you can use this menu item for experimenting
item = Gtk.MenuItem()
item.set_label("Test")
item.connect("activate", self.handler_menu_test)
item.show()
self.menu.append(item)
# this is for exiting the app
item = Gtk.MenuItem()
item.set_label("Exit")
item.connect("activate", self.handler_menu_exit)
item.show()
self.menu.append(item)
self.menu.show()
self.ind.set_menu(self.menu)
# initialize cpu speed display
self.update_cpu_speeds()
# then start updating every 2 seconds
# http://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib--timeout-add-seconds
GLib.timeout_add_seconds(2, self.handler_timeout)
def get_cpu_speeds(self):
"""Use regular expression to parse speeds of all CPU cores from
/proc/cpuinfo on Linux.
"""
f = open('/proc/cpuinfo')
# this gives us e.g. ['2300', '2300']
s = re.findall('cpu MHz\s*:\s*(\d+)\.', f.read())
# this will give us ['2.3', '2.3']
f = ['%.1f' % (float(i) / 1000,) for i in s]
return f
def handler_menu_exit(self, evt):
Gtk.main_quit()
def handler_menu_test(self, evt):
# we can change the icon at any time
self.ind.set_icon("dialog-information")
def handler_timeout(self):
"""This will be called every few seconds by the GLib.timeout.
"""
# read, parse and put cpu speeds in the label
self.update_cpu_speeds()
# return True so that we get called again
# returning False will make the timeout stop
return True
def update_cpu_speeds(self):
f = self.get_cpu_speeds()
self.ind.set_label(' '.join(f), "8.8 8.8 8.8 8.8")
def main(self):
Gtk.main()
if __name__ == "__main__":
ind = IndicatorCPUSpeed()
ind.main()