summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-01-24 17:40:51 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-01-24 17:40:51 +0100
commitb64b87943b2e1d5955db90a94de611bf2c4bad2a (patch)
treebddeb9517d98e34e9b9d1707002b194a2a130186
parent1818df036f4ab4b851e8cc0dd15f0bf28d8640f8 (diff)
downloadpsutil-b64b87943b2e1d5955db90a94de611bf2c4bad2a.tar.gz
#371: temperatures: change returned data type from list to dict
-rw-r--r--psutil/__init__.py41
-rw-r--r--psutil/_common.py2
-rw-r--r--psutil/_pslinux.py15
3 files changed, 34 insertions, 24 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 31f79f46..797d3b8d 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -2191,23 +2191,30 @@ if hasattr(_psplatform, "sensors_temperatures"):
is not configured in order to provide these metrics.
"""
def to_fahrenheit(n):
- return (n * 9 / 5) + 32
-
- ret = []
- for rawtuple in _psplatform.sensors_temperatures():
- name, label, current, high, critical = rawtuple
- if fahrenheit:
- current = to_fahrenheit(current)
- if high is not None:
- high = to_fahrenheit(high)
- if critical is not None:
- critical = to_fahrenheit(critical)
- if high and not critical:
- critical = high
- elif critical and not high:
- high = critical
- ret.append(_common.shwtemp(name, label, current, high, critical))
- return ret
+ return (float(n) * 9 / 5) + 32
+
+ ret = collections.defaultdict(list)
+ rawdict = _psplatform.sensors_temperatures()
+
+ for name, values in rawdict.items():
+ while values:
+ label, current, high, critical = values.pop(0)
+ if fahrenheit:
+ current = to_fahrenheit(current)
+ if high is not None:
+ high = to_fahrenheit(high)
+ if critical is not None:
+ critical = to_fahrenheit(critical)
+
+ if high and not critical:
+ critical = high
+ elif critical and not high:
+ high = critical
+
+ ret[name].append(
+ _common.shwtemp(label, current, high, critical))
+
+ return dict(ret)
__all__.append("sensors_temperatures")
diff --git a/psutil/_common.py b/psutil/_common.py
index 8866ef19..0d965ac1 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -160,7 +160,7 @@ scpustats = namedtuple(
scpufreq = namedtuple('scpufreq', ['current', 'min', 'max'])
# psutil.sensors_temperatures()
shwtemp = namedtuple(
- 'shwtemp', ['name', 'label', 'current', 'high', 'critical'])
+ 'shwtemp', ['label', 'current', 'high', 'critical'])
# --- for Process methods
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 28e4a77c..bb3d171b 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -7,6 +7,7 @@
from __future__ import division
import base64
+import collections
import errno
import functools
import glob
@@ -1096,7 +1097,8 @@ def boot_time():
if os.path.exists('/sys/class/hwmon'):
- def sensors_temps():
+
+ def sensors_temperatures():
"""Return hardware (CPU and others) temperatures as a list
of named tuples including name, label, current, max and
critical temperatures.
@@ -1121,22 +1123,23 @@ if os.path.exists('/sys/class/hwmon'):
with f:
return f.read().strip()
- ret = []
+ ret = collections.defaultdict(list)
basenames = sorted(set(
[x.split('_')[0] for x in
glob.glob('/sys/class/hwmon/hwmon*/temp*_*')]))
for base in basenames:
name = cat(os.path.join(os.path.dirname(base), 'name'))
label = cat(base + '_label', replace='')
- current = int(cat(base + '_input')) / 1000.0
+ current = float(cat(base + '_input')) / 1000.0
high = cat(base + '_max', replace=None)
critical = cat(base + '_crit', replace=None)
+
if high is not None:
- high = int(high) / 1000.0
+ high = float(high) / 1000.0
if critical is not None:
- critical = int(critical) / 1000.0
+ critical = float(critical) / 1000.0
- ret.append((name, label, current, high, critical))
+ ret[name].append((label, current, high, critical))
return ret