borgend/dreamtime.py

Mon, 29 Jan 2018 14:02:55 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 29 Jan 2018 14:02:55 +0000
changeset 94
2a11fd29c192
parent 89
51cc2e25af38
child 97
96d5adbe0205
permissions
-rw-r--r--

Make dreamtime module not throw up on non-macOS systems

68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
89
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
2 # Borgend by Tuomo Valkonen, 2018
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
3 #
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
4 # This file implements system wake/sleep detection for scheduling adjustments.
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 #
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
7 import platform
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
8 import time
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
9 import threading
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
10 import weakref
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
11 import datetime
86
2fe66644c50d Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 80
diff changeset
12 import logging
80
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
13
86
2fe66644c50d Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 80
diff changeset
14 logger=logging.getLogger(__name__)
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
16 _dreamtime_monitor=None
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
17
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
18 #
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
19 # Support classes for dealing with different times
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
20 #
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
21
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
22 # Return difference (delay) of "dreamtime" to monotonic time
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
23 def dreamtime_difference():
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
24 if _dreamtime_monitor:
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
25 return _dreamtime_monitor.diff()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
26 else:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
27 return time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
28
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
29 # Return "dreamtime"
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
30 def dreamtime():
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
31 return max(0, time.monotonic()-dreamtime_difference())
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
32
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
33 class Time:
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
34 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
35 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
36
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
37 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
38 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
39
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
40 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
41 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
42 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
43
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
44 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
45 def now(cls):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
46 return cls(cls._now())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
47
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
48 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
49 def from_realtime(cls, realtime):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
50 return cls(realtime-time.time()+cls._now())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
51
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
52 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
53 def from_monotonic(cls, monotonic):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
54 return cls(monotonic-time.monotonic()+cls._now())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
55
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
56 @classmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
57 def after(cls, seconds):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
58 return cls(cls._now()+seconds)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
59
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
60 def datetime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
61 return datetime.datetime.fromtimestamp(self.realtime())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
62
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
63 def seconds_to(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
64 return self._value-self._now()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
65
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
66 def __lt__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
67 return self.monotonic() < other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
68
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
69 def __gt__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
70 return self.monotonic() > other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
71
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
72 def __le__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
73 return self.monotonic() <= other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
74
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
75 def __ge__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
76 return self.monotonic() >= other.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
77
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
78 def __eq__(self, other):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
79 return self.monotonic() == other.realtime()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
80
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
81 class RealTime(Time):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
82 def __init__(self, when):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
83 self._value=when
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
84
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
85 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
86 return self._value
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
87
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
88 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
89 return self._value+(time.monotonic()-time.time())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
90
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
91 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
92 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
93 return time.time()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
94
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
95 class MonotonicTime(Time):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
96 def __init__(self, when):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
97 self._value=when
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
98
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
99 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
100 return self._value+(time.time()-time.monotonic())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
101
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
102 def monotonic(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
103 return self._value
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
104
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
105 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
106 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
107 return time.monotonic()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
108
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
109 class DreamTime(Time):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
110 def __init__(self, when):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
111 self._value=when
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
112
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
113 def realtime(self):
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
114 return self._value+(time.time()-dreamtime())
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
115
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
116 # Important: monotonic is "static" within a wakeup period
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
117 # and does not need to call time.monotonic(), as it gets compared
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
118 # to a specific time.monotonic() realisation
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
119 def monotonic(self):
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
120 return self._value+dreamtime_difference()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
121
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
122 @staticmethod
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
123 def _now():
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
124 return dreamtime()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
125
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
126 if platform.system()=='Darwin':
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
127
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
128 import Foundation
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
129 import AppKit
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
130
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
131 #
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
132 # Wake up / sleep handling
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
133 #
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
134
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
135 class SleepHandler(Foundation.NSObject):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
136 """ Handle wake/sleep notifications """
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
138 def init(self):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
139 self.__sleeptime=None
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
140 self.__slept=0
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
141 self.__epoch=time.monotonic()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
142 self.__lock=threading.Lock()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
143 self.__callbacks=weakref.WeakKeyDictionary()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
144
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
145 return self
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
147 def handleSleepNotification_(self, aNotification):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
148 logger.info("System going to sleep")
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
149 now=time.monotonic()
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
150 with self.__lock:
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
151 self.__sleeptime=now
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
153 def handleWakeNotification_(self, aNotification):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
154 logger.info("System waking up from sleep")
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
155 try:
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
156 now=time.monotonic()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
157 with self.__lock:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
158 if self.__sleeptime:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
159 slept=max(0, now-self.__sleeptime)
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
160 logger.info("Slept %f seconds" % slept)
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
161 self.__slept=self.__slept+slept
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
162 self.__sleeptime=None
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
163 callbacks=self.__callbacks.copy()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
164 except:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
165 logger.exception("Bug in wakeup handler")
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
166
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
167 for callback in callbacks.values():
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
168 try:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
169 callback()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
170 except Exception:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
171 logger.exception("Error in wake notification callback")
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
173 # Return difference to time.monotonic()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
174 def diff(self):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
175 with self.__lock:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
176 diff=self.__epoch+self.__slept
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
177 return diff
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
178
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
179 # Weirdo (Py)ObjC naming to stop it form choking up
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
180 def addForObj_aCallback_(self, obj, callback):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
181 with self.__lock:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
182 self.__callbacks[obj]=callback
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
183
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
184 # obj is to use a a key in a weak key dictionary
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
185 def add_callback(obj, callback):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
186 global _dreamtime_monitor
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
187
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
188 monitor=_dreamtime_monitor
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
189 if not monitor:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
190 raise Exception("Dreamtime monitor not started")
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
191 else:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
192 monitor.addForObj_aCallback_(obj, callback)
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
194 def start_monitoring():
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
195 global _dreamtime_monitor
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
196
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
197 logger.debug("Starting to monitor system sleep")
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
198 workspace = AppKit.NSWorkspace.sharedWorkspace()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
199 notification_center = workspace.notificationCenter()
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
200 _dreamtime_monitor = SleepHandler.new()
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
202 notification_center.addObserver_selector_name_object_(
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
203 _dreamtime_monitor,
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
204 "handleSleepNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
205 AppKit.NSWorkspaceWillSleepNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
206 None)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
207
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
208 notification_center.addObserver_selector_name_object_(
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
209 _dreamtime_monitor,
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
210 "handleWakeNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
211 AppKit.NSWorkspaceDidWakeNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
212 None)
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
213
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
214 else: # Not on macOS
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
215
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
216 def add_callback(obj, callback):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
217 pass
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
218
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
219 def start_monitoring():
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
220 logger.warning(("No system sleep monitor implemented for '%s'"
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
221 % platform.system()))
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
222

mercurial