मैक/पायथन के तहत सिस्टम प्राथमिकताओं में एनर्जी सेवर सेटिंग्स के कारण स्क्रीन कैसे बंद है या नहीं?मैक/पायथन में स्क्रीन बंद होने की जांच कैसे करें?
उत्तर
त्वरित और गंदे समाधान: 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
से प्राप्त करने के लिए ctypes
का उपयोग करें।
एक ही रास्ता मैं बंद बारे में सोच सकते 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 पर निर्भर करता है, जो कार्यों दावे बनाने के लिए, अनुसूची बिजली की घटनाओं, माप थर्मल, और अधिक।
अजगर के माध्यम से इन कार्यों फोन के लिए, हम IOKit फ्रेमवर्क से गुजरना होगा।
http://developer.apple.com/library/mac/#documentation/devicedrivers/conceptual/IOKitFundamentals/
आदेश हमें अजगर में सी डेटा प्रकार में हेरफेर करने के लिए, हम एक विदेशी समारोह इंटरफ़ेस ctypes कहा जाता है का उपयोग करेंगे।
http://python.net/crew/theller/ctypes/
यहाँ आवरण लेखक पृष्ठ पर के वर्णन है, Michael Lynn द्वारा लिखा गया। उपरोक्त लेखक के लिंक से मैंने जो कोड पोस्ट किया है, वह इसे और अधिक समझने योग्य बनाने के लिए इस कोड का एक पुनर्लेख है।
मुझे यकीन नहीं है कि मैं अनुसरण करता हूं। ऐसा लगता है कि यह कोड ओएस एक्स को सोने से रोकने से रोकता है; जबकि मेरा सवाल यह है कि यह जांचने के लिए कि स्क्रीन बंद है या नहीं, ऊर्जा बचत सेटिंग्स आदि के कारण)। जबकि दोनों मुद्दे संबंधित हैं, मैं यह नहीं समझ सकता कि स्क्रीन बंद है या नहीं, यह जांचने के लिए इस कोड का उपयोग कैसे करें। हो सकता है कि मुझसे कुछ छूट रहा हो? क्या आप थोड़ा और विस्तार कर सकते हैं? –
बहुत बढ़िया, धन्यवाद! बीटीडब्ल्यू, मेरे मैक पर 'ioreg' का आउटपुट किसी भी कारण से फिसल गया है और' CurrentPowerState 'नहीं दिखाएगा। मुझे इसे दिखाने के लिए 'ioreg' के पहले तर्क के रूप में' -w 0' जोड़ना पड़ा। –
@ceilingcat मैंने अभी जवाब '-w 0' पैरामीटर के साथ अद्यतन किया है। –