Wed, 31 Jan 2018 00:06:54 +0000
Pruning support
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: |
97 | 34 | def __init__(self, when): |
35 | self._value=when | |
36 | ||
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
37 | def realtime(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 | def monotonic(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
41 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
42 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
43 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
44 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
45 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
46 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
47 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
48 | def now(cls): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
49 | return cls(cls._now()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
50 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
51 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
52 | def from_realtime(cls, realtime): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
53 | return cls(realtime-time.time()+cls._now()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
54 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
55 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
56 | def from_monotonic(cls, monotonic): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
57 | return cls(monotonic-time.monotonic()+cls._now()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
58 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
59 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
60 | def after(cls, seconds): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
61 | return cls(cls._now()+seconds) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
62 | |
97 | 63 | @classmethod |
64 | def after_other(cls, other, seconds): | |
65 | if isinstance(other, cls): | |
66 | return cls(other._value+seconds) | |
67 | else: | |
68 | return cls.from_monotonic(other.monotonic()+seconds) | |
69 | ||
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
70 | def datetime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
71 | return datetime.datetime.fromtimestamp(self.realtime()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
72 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
73 | def seconds_to(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
74 | return self._value-self._now() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
75 | |
97 | 76 | def isoformat(self): |
77 | return self.datetime().isoformat() | |
78 | ||
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
79 | def __lt__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
80 | return self.monotonic() < other.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
81 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
82 | def __gt__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
83 | return self.monotonic() > other.monotonic() |
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 __le__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
86 | return self.monotonic() <= other.monotonic() |
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 __ge__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
89 | return self.monotonic() >= other.monotonic() |
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 | def __eq__(self, other): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
92 | return self.monotonic() == other.realtime() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
93 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
94 | class RealTime(Time): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
95 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
96 | return self._value |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
97 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
98 | def monotonic(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
99 | return self._value+(time.monotonic()-time.time()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
100 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
101 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
102 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
103 | return time.time() |
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 | class MonotonicTime(Time): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
106 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
107 | return self._value+(time.time()-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 | def monotonic(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
110 | return self._value |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
111 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
112 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
113 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
114 | return time.monotonic() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
115 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
116 | class DreamTime(Time): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
117 | def realtime(self): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
118 | return self._value+(time.time()-dreamtime()) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
119 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
120 | # 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
|
121 | # 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
|
122 | # to a specific time.monotonic() realisation |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
123 | def monotonic(self): |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
124 | return self._value+dreamtime_difference() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
125 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
126 | @staticmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
127 | def _now(): |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
128 | return dreamtime() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
129 | |
97 | 130 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
131 | if platform.system()=='Darwin': |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
132 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
133 | import Foundation |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
134 | import AppKit |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
135 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
136 | # |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
137 | # Wake up / sleep handling |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
138 | # |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
139 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
140 | class SleepHandler(Foundation.NSObject): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
141 | """ Handle wake/sleep notifications """ |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
142 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
143 | def init(self): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
144 | self.__sleeptime=None |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
145 | self.__slept=0 |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
146 | self.__epoch=time.monotonic() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
147 | self.__lock=threading.Lock() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
148 | self.__callbacks=weakref.WeakKeyDictionary() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
149 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
150 | return self |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
151 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
152 | def handleSleepNotification_(self, aNotification): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
153 | logger.info("System going to sleep") |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
154 | now=time.monotonic() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
155 | with self.__lock: |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
156 | self.__sleeptime=now |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
157 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
158 | def handleWakeNotification_(self, aNotification): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
159 | logger.info("System waking up from sleep") |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
160 | try: |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
161 | now=time.monotonic() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
162 | with self.__lock: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
163 | if self.__sleeptime: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
164 | 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
|
165 | 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
|
166 | 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
|
167 | self.__sleeptime=None |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
168 | callbacks=self.__callbacks.copy() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
169 | except: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
170 | 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
|
171 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
172 | 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
|
173 | try: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
174 | callback() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
175 | except Exception: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
176 | 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
|
177 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
178 | # 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
|
179 | def diff(self): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
180 | with self.__lock: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
181 | 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
|
182 | return diff |
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 | # 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
|
185 | 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
|
186 | with self.__lock: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
187 | self.__callbacks[obj]=callback |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
188 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
189 | # 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
|
190 | 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
|
191 | global _dreamtime_monitor |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
192 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
193 | monitor=_dreamtime_monitor |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
194 | if not monitor: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
195 | 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
|
196 | else: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
197 | monitor.addForObj_aCallback_(obj, callback) |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
198 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
199 | def start_monitoring(): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
200 | global _dreamtime_monitor |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
201 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
202 | logger.debug("Starting to monitor system sleep") |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
203 | workspace = AppKit.NSWorkspace.sharedWorkspace() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
204 | notification_center = workspace.notificationCenter() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
205 | _dreamtime_monitor = SleepHandler.new() |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
206 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
207 | 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
|
208 | _dreamtime_monitor, |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
209 | "handleSleepNotification:", |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
210 | AppKit.NSWorkspaceWillSleepNotification, |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
211 | None) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
212 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
213 | 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
|
214 | _dreamtime_monitor, |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
215 | "handleWakeNotification:", |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
216 | AppKit.NSWorkspaceDidWakeNotification, |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
217 | None) |
94
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 | else: # Not on macOS |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
220 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
221 | 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
|
222 | pass |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
223 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
224 | def start_monitoring(): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
225 | 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
|
226 | % platform.system())) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
227 |