]> code.delx.au - monosys/commitdiff
OSX battery update
authorJames Bunton <jamesbunton@fastmail.fm>
Sat, 30 May 2009 05:34:32 +0000 (15:34 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Sat, 30 May 2009 05:34:32 +0000 (15:34 +1000)
scripts/osx_battery [new file with mode: 0755]
scripts/powerbook_battery [deleted file]

diff --git a/scripts/osx_battery b/scripts/osx_battery
new file mode 100755 (executable)
index 0000000..b1fcffb
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+
+import itertools
+import subprocess
+import re
+import sys
+
+# Run ioreg to get the battery status line
+classes = ["AppleSmartBattery", "IOBatteryInfo"]
+cmd = ["ioreg", "-w0", "-r", "-c"]
+iterables = []
+for klass in classes:
+       p = subprocess.Popen(args=cmd + [klass], stdout=subprocess.PIPE)
+       iterables.append(p.stdout)
+
+lines = []
+for line in itertools.chain(*iterables):
+       if line.startswith("+"):
+               continue
+       line = line.strip()
+       if line.find("=") >= 0:
+               line = line.replace("Yes", "True").replace("No", "False")
+               line = line.replace("=", ":")
+               if line.find("<") >= 0:
+                       line = line.replace("<", "'<").replace(">", ">'")
+               line += ","
+       lines.append(line)
+exec "data = " + "\n".join(lines)
+
+
+fields = [
+       "Current",
+       "Capacity",
+       "Capacity%",
+       "Cycle Count",
+
+       "CurrentCapacity",
+       "MaxCapacity",
+       "DesignCapacity",
+       "CycleCount",
+       "DesignCycleCount",
+]
+for field in fields:
+       if data.has_key(field):
+               print (field+":").ljust(3+max(map(len, fields))),
+               print data[field]
+
+
diff --git a/scripts/powerbook_battery b/scripts/powerbook_battery
deleted file mode 100755 (executable)
index 871f017..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/usr/bin/env python
-
-import subprocess, re, sys
-
-# Run ioreg to get the battery status line
-p = subprocess.Popen(args=["ioreg", "-w0", "-l"], stdout=subprocess.PIPE)
-for line in p.stdout.xreadlines():
-       if line.find("IOBatteryInfo") >= 0:
-               p.stdout.close()
-               break
-else:
-       print "IOBatteryInfo not found!"
-       sys.exit(1)
-
-
-# Parse the line to a dict & calculate the percentage
-line = line.strip()
-m = re.compile('^.*"IOBatteryInfo" = ').match(line)
-line = line[m.end()+1:-1].replace("=", ":")
-exec "fields = " + line
-fields["Capacity%"] = int(100.0 * float(fields["Capacity"]) / float(fields["AbsoluteMaxCapacity"]))
-
-
-# Print to screen
-for field in ("Current", "Capacity", "Capacity%", "Cycle Count"):
-       print "%s:\t%d" % (field, fields[field])
-
-