sleep.py

Fri, 26 Jan 2018 09:27:07 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 26 Jan 2018 09:27:07 +0000
changeset 69
8705e296c7a0
parent 68
72f821e17123
child 76
4b08fca3ce34
permissions
-rw-r--r--

Scheduling list fix and simplifications

68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Wake/sleep detection for scheduling adjustments
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 import Foundation
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6 import AppKit
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 import borgend
69
8705e296c7a0 Scheduling list fix and simplifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 68
diff changeset
8 import platform
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 logger=borgend.logger.getChild(__name__)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 class SleepHandler(Foundation.NSObject):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 """ Handle wake/sleep notifications """
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 def handleSleepNotification(self, aNotification):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 logger.info("System going to sleep")
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 def handleWakeNotification(self, aNotification):
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19 logger.info("System waking up from sleep")
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 if platform.system()=='Darwin':
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 workspace = AppKit.NSWorkspace.sharedWorkspace()
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 notification_center = workspace.notificationCenter()
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 sleep_handler = SleepHandler.new()
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 notification_center.addObserver_selector_name_object_(
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 sleep_handler,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 "handleSleepNotification",
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 AppKit.NSWorkspaceWillSleepNotification,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 None)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 notification_center.addObserver_selector_name_object_(
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 sleep_handler,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 "handleWakeNotification",
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 AppKit.NSWorkspaceDidWakeNotification,
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 None)
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37

mercurial