System Information

Discussion in 'Python' started by MidiBot@gmail.com, Mar 20, 2006.

  1. Guest

    Hello. I want to write a program in which the main functionality will
    depend on whether or not my
    computer is connected to a power adapter(this is for laptops). I looked

    in the online-documentation for this and had no luck. I imagine it is
    quite easy and may even be as simple as a boolean variable...?

    All I want to be able to tell is whether or not my computer is being
    externally powered. More specifically
    I want to be able to have my program running, and be alerted if I
    un-plug my power adapter.

    I am using a Mac(powerbook) with if that makes any
    difference.

    Thank you very much.
     
    , Mar 20, 2006
    #1
    1. Advertising

  2. Guest

    I'm not sure about osx, but I know linux uses a proc virtual
    filesystem. ( freebsd is probably closer, and it doesn't either but you
    can mount it via mount_linprocfs none /proc)

    But if you know if you have a proc file system, you can check it to see
    if you are under power...

    IE if I remember right, on my laptop it would be
    '/proc/acpi/battery/BAT1'
    >From there I would grep / sed it until I got the results I wanted, then

    do
    if os.popen("cat /proc[...]") == 'power':
    [program]
    else:
    print "not plugged in"



    Not the prettiest of solutions, but it's worth a shot
     
    , Mar 20, 2006
    #2
    1. Advertising

  3. Guest

    Oops -- just asked a buddy, osx doesn't have /proc

    But you could look into sysctl....
    I'll boot up my laptop and check out if there is anything regarding the
    battery, but I suspect you should investigate
    sysctl -a | grep acpi
     
    , Mar 20, 2006
    #3
  4. Serge Orlov Guest

    wrote:
    > in the online-documentation for this and had no luck. I imagine it is
    > quite easy and may even be as simple as a boolean variable...?


    In general it's not so simple, since a computer can have many power
    sources.

    > All I want to be able to tell is whether or not my computer is being
    > externally powered. More specifically
    > I want to be able to have my program running, and be alerted if I
    > un-plug my power adapter.
    >
    > I am using a Mac(powerbook) with if that makes any
    > difference.


    It makes big difference, device API on different operating systems is
    *absolutely* different. On Mac OS X, start from ioreg:
    http://www.hmug.org/man/8/ioreg.php,
    also take a look at command line utility for querying battery info:
    http://www.mitt-eget.com/software/macosx/
    These two pages should help you get started.

    -- Serge.
     
    Serge Orlov, Mar 20, 2006
    #4
  5. Guest

    When I did
    "
    sysctl -a > sysctl-plugged-in.txt
    sysctl -a > sysctl-unplugged.txt
    diff sysctl-plugged-in.txt sysctl-unplugged.txt
    "

    I didn't get anything of interest...

    Maybe there is something I missed, or maybe it's just because I was
    using a live linux distro, or maybe sysctl isn't the way to go :)
     
    , Mar 20, 2006
    #5
  6. Guest

    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!
     
    , Mar 20, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z. f.
    Replies:
    0
    Views:
    717
    z. f.
    Feb 3, 2005
  2. subrato
    Replies:
    8
    Views:
    943
    Juan T. Llibre
    Mar 14, 2006
  3. clercmedia
    Replies:
    2
    Views:
    32,500
    clercmedia
    Dec 9, 2005
  4. lerenard001
    Replies:
    0
    Views:
    450
    lerenard001
    Jul 7, 2008
  5. lerenard001
    Replies:
    0
    Views:
    496
    lerenard001
    Jul 9, 2008
Loading...

Share This Page