Ich habs mir geschrieben um in meiner "taskleiste" von dwm die temps vom PC anzeigen zu lassen.
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
from collections import defaultdict
SENSORS_CMD = 'sensors'
SENSORS_ARGS = ['-u']
def parse_sensors(sensors_cmd=SENSORS_CMD, sensors_args=None):
'''opens a sensors process with subprocess and returns
a dict with the parsed stdout.
sensors_args must be None or a list, which contains the
arguments for the sensors process.'''
if sensors_args is None:
sensors_args = SENSORS_ARGS
elif not '-u' in sensors_args:
sensors_args.append('-u')
stdout, stderr = Popen([sensors_cmd] + sensors_args, stdout=PIPE,
universal_newlines=True).communicate()
return parse_sensors_stdout(stdout)
def parse_sensors_stdout(sensors_stdout):
'''parses the stdout of a sensors process called with the
-u argument'''
parsed_stdout = [b.splitlines() for b in
sensors_stdout.strip().split('\n\n')]
pd = defaultdict(lambda:defaultdict(dict))
for block in parsed_stdout:
key = block.pop(0)
for line in block:
if not ':' in line:
pass
elif line.endswith(':'):
name = line[:-1]
elif not line.startswith(' '):
name, value = line.split(': ')
pd[key][name] = value
else:
type_, value = line.strip().split(': ')
pd[key][name][type_.partition('_')[2]] = value
return dict([(k, dict(v)) for k, v in pd.iteritems()])
if __name__ == '__main__':
from pprint import pprint
pprint(parse_sensors())