-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUptimeAppIndicator.py
51 lines (41 loc) · 1.57 KB
/
UptimeAppIndicator.py
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
##!/usr/bin/env python
import gtk
import appindicator
import logging
import os
# Set up logging
log_file = os.path.expanduser('~/.local/share/UptimeAppIndicator.log')
logging.basicConfig(filename=log_file, level=logging.ERROR, format='%(asctime)s %(message)s')
def get_uptime():
try:
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
hours, remainder = divmod(uptime_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
uptime_string = "{:02}:{:02}".format(hours, minutes)
return uptime_string
except IOError:
logging.error("Error: Unable to read /proc/uptime")
return "Error: Unable to read /proc/uptime"
except ValueError:
logging.error("Error: Unable to parse uptime")
return "Error: Unable to parse uptime"
def exit_item(_):
gtk.main_quit()
if __name__ == "__main__":
try:
ind = appindicator.Indicator("uptime_indicator", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
ind.set_status(appindicator.STATUS_ACTIVE)
ind.set_label(get_uptime(), "")
# create a menu
menu = gtk.Menu()
# create some
exit = gtk.MenuItem('Exit')
exit.connect('activate', exit_item)
exit.show()
menu.append(exit)
ind.set_menu(menu)
gtk.timeout_add(60000, lambda: ind.set_label(get_uptime(), ""))
gtk.main()
except ImportError:
logging.error("Error: Unable to import necessary libaries")