System Information

totalgeekdom at gmail.com totalgeekdom at gmail.com
Mon Mar 20 16:35:24 EST 2006


Good news!
I asked a buddy that runs osx, and he whipped up a script for you  :)

I understand it's not python, but it gets you close, you should be able
to use popen or subprocess.call to complete this.

function is_charger_plugged_in()
{
        sleep 3;

        BATTINFO=`ioreg -l -w 0 | grep IOBatteryInfo | head -n 1 | sed
-e 's/[( ) { }]//g'`
        AMP=`echo $BATTINFO | awk -F, '{print $2}' | cut -f 2 -d =`
        CAP=`echo $BATTINFO | awk -F, '{print $7}' | cut -f 2 -d =`

        if [ "$AMP" -lt "$CAP" ]; then
                return 0
        else
                return 1
        fi > /dev/null 2>&1
}

is_charger_plugged_in

if [ "$?" -eq "0" ]; then
        echo "charger is plugged in";
else
        echo "charger is not plugged in";
fi


If I had to guess for the python script I'd do --
#!/usr/bin/env python
import time,os

def pluggedin():
	# ioreg is for osx only
	time.sleep(3)
	battInfo = "ioreg -l -w 0 | grep IOBatteryInfo | head -n 1 | sed -e
's/[( ) { }]//g'"
	amp = "echo $BATTINFO | awk -F, '{print $2}' | cut -f 2 -d ="
	cap = "echo $BATTINFO | awk -F, '{print $7}' | cut -f 2 -d ="
	if amp < cap:
		return 0
	else:
		return 1
if pluggedin() == 1:
	print "It is plugged in"
else:
	print "It is not plugged in"


Original code was hooked up to me by prism  =)   Cause he's a cool guy

Hope that helps!




More information about the Python-list mailing list