我想使用PyObjC编写以下Objective-C代码的等效Python代码。不知道该怎么办?任何帮助都会受到高度赞赏,因为它将Objective-C代码桥接到Python中。
#import <IOKit/pwr_mgt/IOPMLib.h>
...
// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep
//reasonForActivity is a descriptive string used by the system whenever it needs
// to tell the user why the system is not sleeping. For example,
// "Mail Compacting Mailboxes" would be a useful string.
// NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");
IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
if (success == kIOReturnSuccess)
{
//Add the work you need to do without
// the system sleeping here.
success = IOPMAssertionRelease(assertionID);
//The system will be able to sleep again.
}
...
答案 0 :(得分:2)
首先需要使用IOKit.framework
命令为gen_bridge_metadata
生成桥文件。
如果需要,可以将文件内容硬编码到Python变量中。
然后使用objc.parseBridgeSupport()
objc.parseBridgeSupport(BRIDGE_FILE_STRING, globals(), objc.pathForFramework("/System/Library/Frameworks/IOKit.framework"))`
Here is an example几乎完全符合您的要求。