homer512 icon

py thermal

homer512 | PRO | 06/19/14 05:49:30 PM UTC | 0 ⭐ | 787 👁️ | Never ⏰ | []
Python |

1.04 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/python2
#coding: utf-8
 
import os
 
 
def readline(fname):
    with open(fname) as handle:
        return handle.readline()
 
 
def handle_cooling(basepath, devicename):
    fnames = ('cur_state', 'max_state')
    fpaths = (os.path.join(basepath, devicename, fname) for fname in fnames)
    state, maxstate = (int(readline(fpath)) for fpath in fpaths)
    print "%s %d/%d" % (devicename, state, maxstate)
 
 
def handle_thermal(basepath, devicename):
    fpath = os.path.join(basepath, devicename, 'temp')
    temp = float(readline(fpath))
    print u"%s %g °C" % (devicename, temp / 1000.)
 
 
def main():
    childops = (('cooling_device', handle_cooling),
                ('thermal_zone', handle_thermal),
            )
    basepath = '/sys/class/thermal'
    filelist = os.listdir(basepath)
    for entry in filelist:
        for entrytype, operation in childops:
            if entry.startswith(entrytype):
                operation(basepath, entry)
                break
 
 
if __name__ == '__main__':
    main()

Comments