Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Osx temps #1284

Merged
merged 12 commits into from
Jun 26, 2018
4 changes: 2 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2175,7 +2175,7 @@ def net_if_stats():
# =====================================================================


# Linux
# Linux, OSX
if hasattr(_psplatform, "sensors_temperatures"):

def sensors_temperatures(fahrenheit=False):
Expand Down Expand Up @@ -2213,7 +2213,7 @@ def convert(n):
__all__.append("sensors_temperatures")


# Linux
# Linux, OSX
if hasattr(_psplatform, "sensors_fans"):

def sensors_fans():
Expand Down
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BatteryTime(enum.IntEnum):
'shwtemp', ['label', 'current', 'high', 'critical'])
# psutil.sensors_battery()
sbattery = namedtuple('sbattery', ['percent', 'secsleft', 'power_plugged'])
# psutil.sensors_battery()
# psutil.sensors_fans()
sfan = namedtuple('sfan', ['label', 'current'])

# --- for Process methods
Expand Down
50 changes: 50 additions & 0 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import contextlib
import errno
import collections
import functools
import os
from socket import AF_INET
Expand Down Expand Up @@ -212,6 +213,42 @@ def disk_partitions(all=False):
# =====================================================================


def sensors_temperatures():
"""Return CPU temperatures
including key name and temperature.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put the whole docstring on 1 line

"""
ret = collections.defaultdict(list)

# This handles returning per-core temperature. The SMCkeys for core
# temperatue are known
rawlist = cext.cpu_cores_temperatures()
for core in rawlist:
ret["CPU"].append((core[0], core[1], None, None))

# Additional CPU sensors
rawlist = cext.cpu_misc_temperatures()
for core in rawlist:
ret["CPU"].append((core[0], core[1], None, None))

try:
rawlist = cext.battery_temperatures()
if rawlist is not None:
for batt in rawlist:
ret["Batteries"].append((batt[0], batt[1], None, None))
except(SystemError):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? In general you want to let the underlying C extension raise exceptions (e.g. OSError). In this case if cext.battery_temperatures() fails because the SMC key does not exist then the C extension should simply return an empty list or dict

pass

try:
rawlist = cext.hdd_temperatures()
if rawlist is not None:
for temp in rawlist:
ret["HDD"].append((temp[0], temp[1], None, None))
except(SystemError):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

pass

return dict(ret)


def sensors_battery():
"""Return battery information.
"""
Expand All @@ -230,6 +267,19 @@ def sensors_battery():
return _common.sbattery(percent, secsleft, power_plugged)


def sensors_fans():
"""Return fans speed information.
"""
ret = collections.defaultdict(list)
try:
for key, value in cext.sensors_fans().items():
ret["Fans"].append(_common.sfan(key, int(value)))
except (SystemError):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

# Returns an empty dict if no fans were detected
pass
return dict(ret)


# =====================================================================
# --- network
# =====================================================================
Expand Down
Loading