summaryrefslogtreecommitdiffstats
path: root/py3status/modules/nvidia_temp.py
blob: ba17d276c55c834b1961df1ba82433e3b67a0d77 (plain)
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
# -*- coding: utf-8 -*-
"""
Display NVIDIA GPU temperature.

Configuration parameters:
    - cache_timeout: how often we refresh this module in seconds
    - color_bad: the color used if the temperature can't be read.
    - color_good: the color used if everything is OK.
    - 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)

Requires:
    - nvidia-smi

@author jmdana <https://github.com/jmdana>
@license BSD
"""

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_bad = None
    color_good = 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,
            'color': color,
            'full_text': output
        }

        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)