2013-01-10 18 views

Trả lời

5

Nhanh chóng và bẩn giải pháp: gọi ioreg và phân tích đầu ra.

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

Trong máy tính của tôi, giá trị cho CurrentPowerState4 khi màn hình đang bật và 1 khi màn hình tắt.

Giải pháp tốt hơn: sử dụng ctypes để nhận thông tin đó trực tiếp từ IOKit.

+0

Tuyệt vời, cảm ơn! Btw, trên máy Mac của tôi, đầu ra của 'ioreg' bị cắt bớt vì bất kỳ lý do gì và sẽ không hiển thị' CurrentPowerState'. Tôi đã thêm '-w 0' làm đối số đầu tiên cho' ioreg' để hiển thị nó. –

+0

@ceilingcat Tôi vừa cập nhật câu trả lời với tham số '-w 0'. –

3

Cách duy nhất tôi có thể nghĩ ra là bằng cách sử dụng OSX pmset Power Management CML Tool

MÔ TẢ

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

Tham khảo các liên kết sau đây, nó sẽ cung cấp rất nhiều thông tin mà nên giúp bạn hoàn thành chính xác những gì bạn đang tìm kiếm.

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

tôi sẽ bao gồm các mã được cung cấp bởi các liên kết cho "tiết kiệm và tài liệu hướng dẫn" mục đích:

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

Mã này dựa trên IOPMLib, có chức năng để thực hiện khẳng định, lịch sự kiện điện, đo lường nhiệt, và nhiều hơn nữa.

Để gọi các chức năng này thông qua Python, chúng ta phải đi qua khung IOKit.

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

Để chúng tôi để thao tác loại C dữ liệu trong Python, chúng tôi sẽ sử dụng một giao diện chức năng nước ngoài gọi là ctypes.

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

Đây là trình bao bọc mà tác giả mô tả trên trang; được viết bởi Michael Lynn. Mã tôi đăng từ liên kết của Tác giả ở trên là viết lại mã này để làm cho nó dễ hiểu hơn.

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

+0

Tôi không chắc chắn tôi theo dõi. Dường như với tôi mã này ngăn OS X đi ngủ; trong khi câu hỏi của tôi là làm cách nào để kiểm tra xem màn hình có tắt hay không (vì cài đặt tiết kiệm năng lượng, v.v.). Trong khi hai vấn đề có liên quan, tôi không thể tìm cách sử dụng mã này để kiểm tra xem màn hình có tắt hay không. Có lẽ tôi đang thiếu một cái gì đó? Bạn có thể xây dựng thêm một chút không? –

Các vấn đề liên quan