2013-01-10 73 views

回答

5

快速和髒溶液:呼叫ioreg和解析輸出。

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(','))) 

在我的電腦,爲CurrentPowerState值是4當屏幕上1當屏幕處於關閉狀態。

更好的解決方案:使用​​直接從IOKit獲取該信息。

+0

太棒了,謝謝!順便說一句,在我的Mac上,'ioreg'的輸出會因爲任何原因被剪切掉,並且不會顯示'CurrentPowerState'。我必須添加'-w 0'作爲'ioreg'的第一個參數來顯示它。 –

+0

@ceilingcat我剛剛用'-w 0'參數更新了答案。 –

3

我能想到的唯一方法是通過使用OSX pmset Power Management CML Tool

說明

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

請參考以下鏈接,它會提供的大量信息是應該幫助你完成你正在尋找的東西。

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

我將包括由鏈接 「節約型和文檔」 的目的提供的代碼:

#!/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

的代碼依賴於IOPMLib,使斷言哪些功能,安排電力事件,測量熱量等等。

要通過Python中調用這些函數,我們必須經歷由於IOKit框架。

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

爲了讓我們能夠操縱在Python C數據類型,我們將使用一個名爲ctypes的外國功能界面。

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

下面是筆者描述頁面上的包裝;由Michael Lynn編寫。我從上面的作者鏈接發佈的代碼是對此代碼的重寫,以使其更易於理解。

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

+0

我不確定我關注。在我看來,這段代碼阻止OS X進入休眠狀態;而我的問題是如何檢查屏幕是否關閉(由於節能設置等)。雖然這兩個問題是相關的,但我無法確定如何使用此代碼來檢查屏幕是否關閉。也許我錯過了什麼?你能再詳細一點嗎? –