# HG changeset patch # User Tuomo Valkonen # Date 1516925507 0 # Node ID 72f821e171231ebc4c0600593602601ba59c424c # Parent 75e514746980f287a493687f9c96778273661401 Added basic sleep/wake detection code for MacOS (Still need to make use of it to adjust timers.) diff -r 75e514746980 -r 72f821e17123 sleep.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sleep.py Fri Jan 26 00:11:47 2018 +0000 @@ -0,0 +1,36 @@ +# +# Wake/sleep detection for scheduling adjustments +# + +import Foundation +import AppKit +import borgend + +logger=borgend.logger.getChild(__name__) + +class SleepHandler(Foundation.NSObject): + """ Handle wake/sleep notifications """ + + def handleSleepNotification(self, aNotification): + logger.info("System going to sleep") + + def handleWakeNotification(self, aNotification): + logger.info("System waking up from sleep") + +if platform.system()=='Darwin': + workspace = AppKit.NSWorkspace.sharedWorkspace() + notification_center = workspace.notificationCenter() + sleep_handler = SleepHandler.new() + + notification_center.addObserver_selector_name_object_( + sleep_handler, + "handleSleepNotification", + AppKit.NSWorkspaceWillSleepNotification, + None) + + notification_center.addObserver_selector_name_object_( + sleep_handler, + "handleWakeNotification", + AppKit.NSWorkspaceDidWakeNotification, + None) +