#!/usr/bin/python """ ratatat - Battery monitor script for ratpoison Copyright 2009, Michael DeHaan This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """ import time import os.path import string class RatATat(): def __init__(self): # FIXME: config parser or getopt self.DEBUG_MODE = 0 self.BATT_LOW_PERCENTAGE = 35 self.BATT_CRITICAL_PERCENTAGE = 10 self.alerts = [] self.ONCE = True def proc2hash(self,fname): results = {} fh = open(fname) data = fh.read() fh.close() for line in data.split("\n"): tokens = line.split(":",1) if len(tokens) == 2: key = tokens[0].strip() value = tokens[1].strip() results[key] = value return results def cleanup(self,stringy): return stringy.lower().replace("mwh","").strip() def battery_alerts(self,alerts): """ Show critical battery events on OSD, but don't bother the user unless he needs to care. """ batt_state = self.proc2hash("/proc/acpi/battery/BAT0/state") batt_info = self.proc2hash("/proc/acpi/battery/BAT0/info") capacity = float(self.cleanup(batt_state["remaining capacity"])) real_capacity = float(self.cleanup(batt_info["last full capacity"])) charging_state = self.cleanup(batt_state["charging state"]) # charged, charging? percentage = int(100 * (capacity / real_capacity)) if self.DEBUG_MODE or self.ONCE or charging_state == "discharging": if percentage < self.BATT_LOW_PERCENTAGE: alerts.append("BATT LOW [%s]" % percentage) elif percentage < self.BATT_CRITICAL_PERCENTAGE: alerts.append("*** BATT CRITICAL [%s] *** " % percentage) elif self.ONCE or self.DEBUG_MODE: alerts.append("BATT OK [%s]" % percentage) self.ONCE = False def run(self): self.ONCE = True while True: alerts = [] self.battery_alerts(alerts) if len(alerts) > 0: message = " | ".join(alerts) message = message.replace(";","_") os.system("ratpoison -c 'echo %s'" % message) time.sleep(25) if __name__ == "__main__": notifier = RatATat() notifier.run()