diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-01-24 22:09:50 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-01-24 22:09:50 +0100 |
commit | 77a8df098f354b990832b33f324577fa24ad16e6 (patch) | |
tree | 8442584bf4bd8fbaf1cfacb86c4253fbf1d02585 | |
parent | 62b92a1d569f40ca06ff45cb8b00f58b233cfbe6 (diff) | |
download | psutil-77a8df098f354b990832b33f324577fa24ad16e6.tar.gz |
refactoring
-rw-r--r-- | psutil/__init__.py | 3 | ||||
-rw-r--r-- | psutil/_pslinux.py | 20 | ||||
-rwxr-xr-x | psutil/tests/test_system.py | 21 |
3 files changed, 19 insertions, 25 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index a57eacc9..0417ed7f 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -185,7 +185,8 @@ __all__ = [ "net_io_counters", "net_connections", "net_if_addrs", # network "net_if_stats", "disk_io_counters", "disk_partitions", "disk_usage", # disk - "users", "boot_time", # "sensors_temperatures" # others + # "sensors_temperatures", # sensors + "users", "boot_time", # others ] __all__.extend(_psplatform.__extra__all__) __author__ = "Giampaolo Rodola'" diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index aea4aa52..2de7dbcb 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -285,7 +285,7 @@ def cat(fname, fallback=_DEFAULT, binary=True): """Return file content.""" try: with open_binary(fname) if binary else open_text(fname) as f: - return f.read() + return f.read().strip() except IOError: if fallback != _DEFAULT: return fallback @@ -1111,28 +1111,16 @@ if os.path.exists('/sys/class/hwmon'): - /sys/class/thermal/thermal_zone* is another one but it's more difficult to parse """ - def cat(fname, replace=_DEFAULT): - try: - f = open(fname) - except IOError: - if replace != _DEFAULT: - return replace - else: - raise - else: - with f: - return f.read().strip() - 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='') + label = cat(base + '_label', fallback='') current = float(cat(base + '_input')) / 1000.0 - high = cat(base + '_max', replace=None) - critical = cat(base + '_crit', replace=None) + high = cat(base + '_max', fallback=None) + critical = cat(base + '_crit', fallback=None) if high is not None: high = float(high) / 1000.0 diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index de2ddef0..f9331791 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -29,6 +29,7 @@ from psutil import POSIX from psutil import SUNOS from psutil import WINDOWS from psutil._compat import long +from psutil._compat import unicode from psutil.tests import AF_INET6 from psutil.tests import APPVEYOR from psutil.tests import check_net_address @@ -757,14 +758,18 @@ class TestSystemAPIs(unittest.TestCase): @unittest.skipUnless(hasattr(psutil, "sensors_temperatures"), "platform not suported") def test_sensors_temperatures(self): - ls = psutil.sensors_temperatures() - for entry in ls: - if entry.current is not None: - self.assertGreaterEqual(entry.current, 0) - if entry.high is not None: - self.assertGreaterEqual(entry.high, 0) - if entry.critical is not None: - self.assertGreaterEqual(entry.critical, 0) + temps = psutil.sensors_temperatures() + assert temps, temps + for name, entries in temps.items(): + self.assertIsInstance(name, (str, unicode)) + for entry in entries: + self.assertIsInstance(entry.label, (str, unicode)) + if entry.current is not None: + self.assertGreaterEqual(entry.current, 0) + if entry.high is not None: + self.assertGreaterEqual(entry.high, 0) + if entry.critical is not None: + self.assertGreaterEqual(entry.critical, 0) if __name__ == '__main__': |