From d723ae47e3dcba7b251d71deb5e7b50e0d176025 Mon Sep 17 00:00:00 2001 From: gacekjk Date: Fri, 5 Jun 2015 04:13:37 +0200 Subject: [PATCH] added cpu frequency module --- i3pystatus/cpu_freq.py | 25 +++++++++++++++++++++++++ tests/cpufreq01 | 4 ++++ tests/test_cpu_freq.py | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 i3pystatus/cpu_freq.py create mode 100644 tests/cpufreq01 create mode 100644 tests/test_cpu_freq.py diff --git a/i3pystatus/cpu_freq.py b/i3pystatus/cpu_freq.py new file mode 100644 index 0000000..cc3ffb2 --- /dev/null +++ b/i3pystatus/cpu_freq.py @@ -0,0 +1,25 @@ +from i3pystatus import IntervalModule + + +class CpuFreq(IntervalModule): + format = "{avg}" + settings = ( + "format", + ("color", "The text color"), + ("average", "Show average for every core"), + ("file", "override default path"), + ) + + file = '/proc/cpuinfo' + color = '#FFFFFF' + + def run(self): + with open(self.file) as f: + mhz_values = [float(line.split(':')[1]) for line in f if line.startswith('cpu MHz')] + + cdict = {"core{}".format(key): str(value) for key, value in enumerate(mhz_values)} + cdict['avg'] = str(sum(mhz_values) / len(mhz_values)) + + self.output = { + "full_text": self.format.format(**cdict), + } diff --git a/tests/cpufreq01 b/tests/cpufreq01 new file mode 100644 index 0000000..5b79afb --- /dev/null +++ b/tests/cpufreq01 @@ -0,0 +1,4 @@ +cpu MHz : 1240.382 +cpu MHz : 1236.828 +cpu MHz : 1203.007 +cpu MHz : 1264.859 \ No newline at end of file diff --git a/tests/test_cpu_freq.py b/tests/test_cpu_freq.py new file mode 100644 index 0000000..ed678f3 --- /dev/null +++ b/tests/test_cpu_freq.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import os +from i3pystatus import cpu_freq + + +def cpu_freq_test(tfpath, tformat, expected): + cf = cpu_freq.CpuFreq(file=os.path.join(os.path.dirname(__file__), tfpath), format=tformat) + cf.run() + assert cf.output["full_text"] == expected + + +def test_basic(): + cases = [ + ('cpufreq01', '1240.382', '1236.828', '1203.007', '1264.859', '1236.269'), + ] + for path, core0, core1, core2, core3, avg in cases: + cpu_freq_test(path, "{avg}", avg) + cpu_freq_test(path, "{core0}", core0) + cpu_freq_test(path, "{core1}", core1) + cpu_freq_test(path, "{core2}", core2) + cpu_freq_test(path, "{core3}", core3)