]> code.delx.au - monosys/blob - scripts/powerbook_battery
Added ssh-agent script
[monosys] / scripts / powerbook_battery
1 #!/usr/bin/env python
2
3 import subprocess, re, sys
4
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:
9 p.stdout.close()
10 break
11 else:
12 print "IOBatteryInfo not found!"
13 sys.exit(1)
14
15
16 # Parse the line to a dict & calculate the percentage
17 line = line.strip()
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"]))
22
23
24 # Print to screen
25 for field in ("Current", "Capacity", "Capacity%", "Cycle Count"):
26 print "%s:\t%d" % (field, fields[field])
27
28