1
0
mirror of https://github.com/gryf/pywmtemp.git synced 2025-12-19 12:18:02 +01:00

Use psutil instead of traversing through sys filesystem.

This commit is contained in:
2022-05-01 15:18:57 +02:00
parent 31b7272e68
commit 1a27c389c0

View File

@@ -153,7 +153,8 @@ class SensorDockApp(wmdocklib.DockApp):
position = 1 position = 1
if count == 0: if count == 0:
for item in self.conf['readings']: for item in self.conf['readings']:
self.add_string(self.get_reading(item), 1, position) string, offset = self.get_reading(item)
self.add_string(string, 1, position)
position += self.char_height position += self.char_height
count += 1 count += 1
@@ -163,20 +164,21 @@ class SensorDockApp(wmdocklib.DockApp):
time.sleep(0.1) time.sleep(0.1)
def get_reading(self, item): def get_reading(self, item):
if 'fname' not in item:
return ' ' * self.max_chars_in_line
with open(item['fname']) as fobj: if not self.conf:
value = fobj.read().strip() return ' ', 0
if item.get('divide'): temp = None
value = int(int(value)/int(item['divide'])) temps = psutil.sensors_temperatures()
label = item['label'] for shw in temps.get(item.get('sensor'), []):
if len(f"{label}{value}{item['unit']}") > self.max_chars_in_line: if shw.label == item.get('label'):
return 'ERRTOOLNG' temp = shw
break
while len(f"{label}{value}{item['unit']}") != self.max_chars_in_line: value = int(temp.current)
label += ' ' name = item.get('name')
self._history[name] = self._history[name][1:]
self._history[name].append(value)
# shift charset depending on the threshold defined in config, assuming # shift charset depending on the threshold defined in config, assuming
# charset is the same row(s) copied with different color for warning # charset is the same row(s) copied with different color for warning
@@ -184,18 +186,22 @@ class SensorDockApp(wmdocklib.DockApp):
# FIXME: remove hardcoded multiplies in favor of automatically # FIXME: remove hardcoded multiplies in favor of automatically
# computed factors. # computed factors.
displacement = 0 displacement = 0
if item.get('override_warning'): if item.get('override_warning') and value >= item['override_warning']:
if value >= item['override_warning']: displacement = int(self.charset_width / self.char_width) * 2
displacement = int(self.charset_width / self.char_width) * 2 elif temp.high and value >= temp.high:
if item.get('override_critical'): displacement = int(self.charset_width / self.char_width) * 2
if value >= item['override_critical']:
displacement = int(self.charset_width / self.char_width) * 4
string = f"{label}{value}{item['unit']}".replace('°', '\\').upper() if (item.get('override_critical') and
value >= item['override_critical']):
displacement = int(self.charset_width / self.char_width) * 4
elif temp.critical and value >= temp.critical:
displacement = int(self.charset_width / self.char_width) * 4
string = f"{value}{item['unit']}".replace('°', '\\').upper()
if displacement: if displacement:
string = ''.join([chr(ord(i) + displacement) for i in string]) string = ''.join([chr(ord(i) + displacement) for i in string])
return string return string, displacement
def main(): def main():