1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#!/usr/bin/env python
#
# $Id: battery.py,v 1.3 2003/06/11 11:31:26 resolve Exp $
#
# Time-stamp: <2002-12-30 16:05:01 resolve>
#
# Copyright (C) Damien Elmes <resolve@repose.cx>, 2001.
# This file is licensed under the GPL. Please see COPYING for more details.
#
"""
Display a pyosd-based graph of how much of the battery has been used.
"""
threshold_warn = 11
threshold_shutdown = 9
import commands
import os
import re
import string
import pyosd, pyosd.daemon as pyd
class BatteryStatus:
def __init__(self, battery=None):
"""The battery argument can be used to specify a particular battery to
use, otherwise the first one will be."""
self.data = {}
ACPI_PROC_DIR = "/proc/acpi/battery/"
if not battery:
try:
files = os.listdir(ACPI_PROC_DIR)
except OSError:
raise "Couldn't open dir: %s" % ACPI_PROC_DIR
if not files:
raise "No battery appears to be present"
battery = files[0]
try:
self.file = open(ACPI_PROC_DIR + os.sep + battery + os.sep +
"state")
except:
raise "Couldn't open specified battery info: %s" % \
ACPI_PROC_DIR + os.sep + battery + \
os.sep + "state"
self.total_capacity = 3736.0
def check(self, battery=None):
"""Check and return battery status"""
self.file.seek(0)
lines = self.file.readlines()
for line in lines:
(id, val) = string.split(line, ":", maxsplit=1)
self.data[id] = string.strip(val)
self.remaining_capacity = int(string.split(self.data['remaining capacity'])[0])
self.percent_used = (self.remaining_capacity / self.total_capacity * 100.0 )
def __repr__(self):
return `self.data`
class plugin:
def __init__(self):
self.plugin_name = "battery"
self.plugin_desc = "Display ACPI battery status"
self.plugin_keys = ["bat"]
self.batstat = BatteryStatus()
self.increment = 100
self.check_battery()
def check_battery(self):
"""Check battery status and warn when it reaches a certain level"""
self.batstat.check()
if self.batstat.data['charging state'] == 'discharging':
# if the battery is dischanging, check we're not running out of
# power
if self.batstat.percent_used < threshold_shutdown:
pyd.top.set_timeout(-1)
pyd.top.display("Critical! Battery at less than 7%" +
" - shutting down")
os.system("sync")
os.system("sudo halt")
return
if self.batstat.percent_used < threshold_warn:
pyd.top.set_timeout(-1)
pyd.top.display("Alert! Battery at less than 10%")
if self.batstat.percent_used < self.increment:
self.bat()
self.increment = self.batstat.percent_used - 5
else:
pyd.top.set_timeout(3)
self.increment = 100
pyd.reactor.callLater(30, self.check_battery)
def bat(self, *args):
"""Display battery info"""
self.batstat.check()
if self.batstat.data['charging state'] == 'unknown':
state = "On A/C power."
else:
state = self.batstat.data['charging state']
state = state + " (at %d%%)" % self.batstat.percent_used
pyd.top.set_colour("#bbbbFF")
pyd.top.set_timeout(3)
pyd.top.display(state)
pyd.top.display(self.batstat.percent_used, type=pyosd.TYPE_PERCENT, line=1)
|