]>
code.delx.au - monosys/blob - scripts/powerbook_battery
3 import subprocess
, re
, sys
5 # Run ioreg to get the battery status line
6 p
= subprocess
.Popen(args
=["ioreg", "-w0", "-l"], stdout
=subprocess
.PIPE
)
7 for line
in p
.stdout
.xreadlines():
8 if line
.find("IOBatteryInfo") >= 0:
12 print "IOBatteryInfo not found!"
16 # Parse the line to a dict & calculate the percentage
18 m
= re
.compile('^.*"IOBatteryInfo" = ').match(line
)
19 line
= line
[m
.end()+1:-1].replace("=", ":")
20 exec "fields = " + line
21 fields
["Capacity%"] = int(100.0 * float(fields
["Capacity"]) / float(fields
["AbsoluteMaxCapacity"]))
25 for field
in ("Current", "Capacity", "Capacity%", "Cycle Count"):
26 print "%s:\t%d" % (field
, fields
[field
])