Guest User

battery check script

a guest
Oct 21st, 2011
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. '''
  4. Script for checking laptop battery state and displaying warnings
  5.  
  6. Needs python3, acpi and gxmessage to be installed
  7. '''
  8.  
  9. import subprocess, re
  10. from time import sleep
  11.  
  12. # seconds of sleep time between battery checks
  13. sleep_time = 60
  14.  
  15. # actions
  16. # play_sound='aplay /home/marek/temps/alarm.wav'
  17. # empty_action='/sbin/shutdown -h now'
  18.  
  19. # states of battery life
  20. s_ok = 0 # > 30 min
  21. s_30 = 1 # <= 30 min
  22. s_15 = 2 # <= 15 min
  23. s_10 = 3 # <= 10 min
  24. s_05 = 4 # <= 5 min
  25.  
  26. # main state
  27. state = s_ok
  28.  
  29. def getstate():
  30. '''getstate() -> state, minutes_left
  31. return the battery state and battery minutes left'''
  32. battery_info = subprocess.getoutput('acpi -b')
  33. battery_state = re.search(r'^Battery \d: (.+), \d+%', battery_info).groups()[0]
  34.  
  35. if battery_state.lower() == 'discharging':
  36. time_left = int(re.search(r'(\d+:\d+)', battery_info).groups()[0].replace(':', ''))
  37. if time_left <= 5:
  38. return s_05, time_left
  39. elif time_left <= 10:
  40. return s_10, time_left
  41. elif time_left <= 15:
  42. return s_15, time_left
  43. elif time_left <= 30:
  44. return s_30, time_left
  45. return s_ok, None
  46.  
  47. def printwarning(minutes):
  48. '''prints battery state warning'''
  49. subprocess.getoutput('gxmessage -center -title "{M} min battery left" -buttons ok:0 -default ok "{M} minutes of computer battery left..."'.format(M=minutes))
  50.  
  51. if __name__ == '__main__':
  52. while True:
  53. prevstate = state
  54. state, time = getstate()
  55. if prevstate < state or state == s_05:
  56. printwarning(time)
  57. sleep(sleep_time)
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment