2013-01-10 23 views

Odpowiedz

5

Szybki i brudny roztwór call ioreg i analizować dane wyjściowe.

import subprocess 
import re 

POWER_MGMT_RE = re.compile(r'IOPowerManagement.*{(.*)}') 

def display_status(): 
    output = subprocess.check_output(
     'ioreg -w 0 -c IODisplayWrangler -r IODisplayWrangler'.split()) 
    status = POWER_MGMT_RE.search(output).group(1) 
    return dict((k[1:-1], v) for (k, v) in (x.split('=') for x in 
              status.split(','))) 

w moim komputerze, wartość CurrentPowerState jest 4 gdy ekran jest włączony i 1 gdy ekran jest wyłączony.

Lepsze rozwiązanie: użyj ctypes, aby uzyskać te informacje bezpośrednio od IOKit.

+0

Niesamowite, dzięki! Btw, na moim Macu wyjście 'ioreg' jest przycinane z jakiegokolwiek powodu i nie wyświetlało' CurrentPowerState'. Musiałem dodać '-w 0' jako pierwszy argument' ioreg', aby go pokazać. –

+0

@ceilingcat Właśnie zaktualizowałem odpowiedź za pomocą parametru '-w 0'. –

3

Jedynym sposobem mogę myśleć off jest za pomocą OSX pmset Power Management CML Tool

OPIS

pmset changes and reads power management settings such as idle sleep timing, wake on administrative 
access, automatic restart on power loss, etc. 

Patrz poniższego linku, będzie dostarczać wiele informacji, które powinien pomóc Ci w osiągnięciu dokładnie tego, czego szukasz.

http://managingamac.blogspot.com/2012/12/power-assertions-in-python.html

będę zawierać kod podany przez link do celów "Zapisywanie i dokumentacji":

#!/usr/bin/python 

import ctypes 
import CoreFoundation 
import objc 
import subprocess 
import time 

def SetUpIOFramework(): 
    # load the IOKit library 
    framework = ctypes.cdll.LoadLibrary(
     '/System/Library/Frameworks/IOKit.framework/IOKit') 

    # declare parameters as described in IOPMLib.h 
    framework.IOPMAssertionCreateWithName.argtypes = [ 
     ctypes.c_void_p, # CFStringRef 
     ctypes.c_uint32, # IOPMAssertionLevel 
     ctypes.c_void_p, # CFStringRef 
     ctypes.POINTER(ctypes.c_uint32)] # IOPMAssertionID 
    framework.IOPMAssertionRelease.argtypes = [ 
     ctypes.c_uint32] # IOPMAssertionID 
    return framework 

def StringToCFString(string): 
    # we'll need to convert our strings before use 
    return objc.pyobjc_id(
     CoreFoundation.CFStringCreateWithCString(
      None, string, 
      CoreFoundation.kCFStringEncodingASCII).nsstring()) 

def AssertionCreateWithName(framework, a_type, 
          a_level, a_reason): 
    # this method will create an assertion using the IOKit library 
    # several parameters 
    a_id = ctypes.c_uint32(0) 
    a_type = StringToCFString(a_type) 
    a_reason = StringToCFString(a_reason) 
    a_error = framework.IOPMAssertionCreateWithName(
     a_type, a_level, a_reason, ctypes.byref(a_id)) 

    # we get back a 0 or stderr, along with a unique c_uint 
    # representing the assertion ID so we can release it later 
    return a_error, a_id 

def AssertionRelease(framework, assertion_id): 
    # releasing the assertion is easy, and also returns a 0 on 
    # success, or stderr otherwise 
    return framework.IOPMAssertionRelease(assertion_id) 

def main(): 
    # let's create a no idle assertion for 30 seconds 
    no_idle = 'NoIdleSleepAssertion' 
    reason = 'Test of Pythonic power assertions' 

    # first, we'll need the IOKit framework 
    framework = SetUpIOFramework() 

    # next, create the assertion and save the ID! 
    ret, a_id = AssertionCreateWithName(framework, no_idle, 255, reason) 
    print '\n\nCreating power assertion: status %s, id %s\n\n' % (ret, a_id) 

    # subprocess a call to pmset to verify the assertion worked 
    subprocess.call(['pmset', '-g', 'assertions']) 
    time.sleep(5) 

    # finally, release the assertion of the ID we saved earlier 
    AssertionRelease(framework, a_id) 
    print '\n\nReleasing power assertion: id %s\n\n' % a_id 

    # verify the assertion has been removed 
    subprocess.call(['pmset', '-g', 'assertions']) 

if __name__ == '__main__': 
    main() 

http://opensource.apple.com/source/PowerManagement/PowerManagement-211/pmset/pmset.c

Kod opiera się na IOPMLib, który działa, aby twierdzenia, planuj zdarzenia zasilania, mierz termiki i wiele więcej.

Aby wywołać te funkcje przez Pythona, musimy przejść przez ram IOKit.

http://developer.apple.com/library/mac/#documentation/devicedrivers/conceptual/IOKitFundamentals/

Abyśmy manipulowania typy danych w Pythonie C, użyjemy funkcji interfejsu zagranicznej nazwie ctypes.

http://python.net/crew/theller/ctypes/

Oto wrapper autor opisuje jest na stronie; napisane przez Michael Lynn. Kod opublikowany z powyższego linku autora to przepisanie tego kodu, aby uczynić go bardziej zrozumiałym.

https://github.com/pudquick/pypmset/blob/master/pypmset.py

+0

Nie jestem pewien, czy podążam. Wydaje mi się, że ten kod zapobiega przechodzeniu systemu OS X w stan uśpienia; natomiast moje pytanie brzmi: jak sprawdzić, czy ekran jest wyłączony (ze względu na ustawienia oszczędzania energii itp.). Chociaż te dwa problemy są powiązane, nie mogę wymyślić, jak użyć tego kodu, aby sprawdzić, czy ekran jest wyłączony. Może czegoś mi brakuje? Czy możesz rozwinąć trochę więcej? –

Powiązane problemy