aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-02-28 20:50:53 +0100
committerMattias Andrée <maandree@operamail.com>2014-02-28 20:50:53 +0100
commitdb7cf58ac21627b5ad851c1e738725d824fceac7 (patch)
tree303fe45674985d96d6c251b04fbfe3a3b8f5fec3
parentadd support for simple arrows (diff)
downloadxpybar-db7cf58ac21627b5ad851c1e738725d824fceac7.tar.gz
xpybar-db7cf58ac21627b5ad851c1e738725d824fceac7.tar.bz2
xpybar-db7cf58ac21627b5ad851c1e738725d824fceac7.tar.xz
add uptime
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--examples/plugin-test20
-rw-r--r--src/plugins/uptime.py75
2 files changed, 95 insertions, 0 deletions
diff --git a/examples/plugin-test b/examples/plugin-test
new file mode 100644
index 0000000..fda91aa
--- /dev/null
+++ b/examples/plugin-test
@@ -0,0 +1,20 @@
+# -*- python -*-
+
+# A simple xpybar configuration example that tests the plugins
+
+from plugins.uptime import Uptime
+
+OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True
+
+def redraw():
+ bar.clear()
+
+ uptime_ = Uptime()
+ uptime = '%id %02i:%02i:%0.2f' % uptime_.uptime
+ tot_idle = '%id %02i:%02i:%0.2f' % uptime_.total_idle
+ avg_idle = '%id %02i:%02i:%0.2f' % uptime_.average_idle
+
+ text = 'Uptime: %s │ Idle time: %s %s'
+ text %= (uptime, tot_idle, avg_idle)
+ bar.draw_coloured_text(0, 10, 0, 2, text)
+
diff --git a/src/plugins/uptime.py b/src/plugins/uptime.py
new file mode 100644
index 0000000..0979b7b
--- /dev/null
+++ b/src/plugins/uptime.py
@@ -0,0 +1,75 @@
+# -*- python -*-
+'''
+xpybar – xmobar replacement written in python
+Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+'''
+
+from util import *
+
+
+class Uptime:
+ '''
+ The current uptime of the machine and how long it has been idle
+
+ @variable uptime:(days:int, hours:int, minutes:int, seconds:float) Total uptime
+ @variable total_idle:(days:int, hours:int, minutes:int, seconds:float) Total processor idle time
+ @variable average_idle:(days:int, hours:int, minutes:int, seconds:float) Average processor idle time
+ '''
+
+ cpu_count = None
+ '''
+ :int? The number of processors on the machine
+ '''
+
+
+ def __init__(self):
+ '''
+ Constructor
+ '''
+ uptime = None
+ with open('/proc/uptime', 'rb') as file:
+ uptime = file.read()
+ uptime = uptime.decode('utf-8', 'replace')
+ uptime = uptime.replace('\n', ' ').split(' ')
+ uptime, idle = float(uptime[0]), float(uptime[1])
+
+ if Uptime.cpu_count is None:
+ with open('/proc/cpuinfo', 'rb') as file:
+ Uptime.cpu_count = file.read()
+ Uptime.cpu_count = Uptime.cpu_count.decode('utf-8', 'replace').split('\n')
+ Uptime.cpu_count = filter(lambda line : 'processor' in line, Uptime.cpu_count)
+ Uptime.cpu_count = len(list(Uptime.cpu_count))
+
+ self.uptime = Uptime.split_time(uptime)
+ self.total_idle = Uptime.split_time(idle)
+ self.average_idle = Uptime.split_time(idle / Uptime.cpu_count)
+
+
+ @staticmethod
+ def split_time(t):
+ '''
+ Split a large number of seconds to days, hours, minutes and seconds
+
+ @param t:float The number of seconds
+ @return :(days:int, hours:int, minutes:int, seconds:float)
+ The time split into days, hours [0, 23], minutes [0, 59] and seconds [0, 60[
+ '''
+ seconds, t = t % 60, t // 60
+ minutes, t = int(t % 60), t // 60
+ hours, t = int(t % 24), t // 24
+ days = int(t)
+ return (days, hours, minutes, seconds)
+