From 648728327b31b45e887a8ee92bbab82acad8f1e0 Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Wed, 1 Apr 2015 21:27:58 +0100 Subject: A module for displaying NVIDIA GPUs' temperature --- py3status/modules/nvidia_temp.py | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 py3status/modules/nvidia_temp.py (limited to 'py3status/modules/nvidia_temp.py') diff --git a/py3status/modules/nvidia_temp.py b/py3status/modules/nvidia_temp.py new file mode 100755 index 0000000..4f6a40e --- /dev/null +++ b/py3status/modules/nvidia_temp.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" +Module for displaying NVIDIA GPU temperature + +Requires: + - nvidia-smi + +@author jmdana +@license GPLv3 +""" + +import re +import shlex +from subprocess import check_output +from time import time + +TEMP_RE = re.compile(r"Current Temp\s+:\s+([0-9]+)") + +class Py3status: + # configuration parameters + cache_timeout = 10 + color_good = None + color_bad = None + format_prefix = "GPU: " + format_units = "°C" + separator = '|' + + def nvidia_temp(self, i3s_output_list, i3s_config): + # The whole command: + # nvidia-smi -q -d TEMPERATURE | sed -nr 's/.*Current Temp.*:[[:space:]]*([0-9]+).*/\1/p' + + out = check_output(shlex.split("nvidia-smi -q -d TEMPERATURE")) + temps = re.findall(TEMP_RE, out.decode("utf-8")) + + if temps != []: + data = [] + for temp in temps: + fmt_str = "{temp}{format_units}".format( + temp=temp, + format_units=self.format_units + ) + data.append(fmt_str) + + output = "{format_prefix}{data}".format( + format_prefix=self.format_prefix, + data=self.separator.join(data) + ) + + color = self.color_good or i3s_config['color_good'] + else: + output = "{format_prefix}OFF".format( + format_prefix=self.format_prefix + ) + + color = self.color_bad or i3s_config['color_bad'] + + response = { + 'cached_until': time() + self.cache_timeout, + 'full_text': output, + 'color': color, + } + + return response + +if __name__ == "__main__": + """ + Test this module by calling it directly. + """ + from time import sleep + x = Py3status() + config = { + "color_good": "#00FF00", + "color_bad": "#FF0000", + } + + while True: + print(x.nvidia_temp([], config)) + sleep(1) -- cgit v1.3 From fdab47f35982bbb007d04759af2b57f768ac334c Mon Sep 17 00:00:00 2001 From: "J.M. Dana" Date: Thu, 2 Apr 2015 19:10:14 +0100 Subject: QA for nvidia_temp --- py3status/modules/nvidia_temp.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'py3status/modules/nvidia_temp.py') diff --git a/py3status/modules/nvidia_temp.py b/py3status/modules/nvidia_temp.py index 4f6a40e..b9340e9 100755 --- a/py3status/modules/nvidia_temp.py +++ b/py3status/modules/nvidia_temp.py @@ -1,5 +1,4 @@ -#!/usr/bin/env python -# encoding: utf-8 +# -*- coding: utf-8 -*- """ Module for displaying NVIDIA GPU temperature @@ -7,8 +6,16 @@ Module for displaying NVIDIA GPU temperature Requires: - nvidia-smi +Configuration parameters: + - cache_timeout: how often we refresh this module in seconds + - color_good: the color used if everything is OK. + - color_bad: the color used if the temperature can't be read. + - format_prefix: a prefix for the output. + - format_units: the temperature units. Will appear at the end. + - separator: the separator char between temperatures (only if more than one GPU) + @author jmdana -@license GPLv3 +@license BSD """ import re @@ -58,8 +65,8 @@ class Py3status: response = { 'cached_until': time() + self.cache_timeout, - 'full_text': output, 'color': color, + 'full_text': output, } return response -- cgit v1.3