Advertisement
homer512

py thermal

Jun 19th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. #!/usr/bin/python2
  2. #coding: utf-8
  3.  
  4. import os
  5.  
  6.  
  7. def readline(fname):
  8.     with open(fname) as handle:
  9.         return handle.readline()
  10.  
  11.  
  12. def handle_cooling(basepath, devicename):
  13.     fnames = ('cur_state', 'max_state')
  14.     fpaths = (os.path.join(basepath, devicename, fname) for fname in fnames)
  15.     state, maxstate = (int(readline(fpath)) for fpath in fpaths)
  16.     print "%s %d/%d" % (devicename, state, maxstate)
  17.  
  18.  
  19. def handle_thermal(basepath, devicename):
  20.     fpath = os.path.join(basepath, devicename, 'temp')
  21.     temp = float(readline(fpath))
  22.     print u"%s %g °C" % (devicename, temp / 1000.)
  23.  
  24.  
  25. def main():
  26.     childops = (('cooling_device', handle_cooling),
  27.                 ('thermal_zone', handle_thermal),
  28.             )
  29.     basepath = '/sys/class/thermal'
  30.     filelist = os.listdir(basepath)
  31.     for entry in filelist:
  32.         for entrytype, operation in childops:
  33.             if entry.startswith(entrytype):
  34.                 operation(basepath, entry)
  35.                 break
  36.  
  37.  
  38. if __name__ == '__main__':
  39.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement