Mon, 05 Feb 2018 10:25:17 +0000
Added exeption protection decorators to callbacks.
If callbacks crash, there's rarely anything in the logs otherwise.
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 |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
13 | import math |
80
a409242121d5
Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
79
diff
changeset
|
14 | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
15 | from .exprotect import protect_noreturn |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
16 | |
86
2fe66644c50d
Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents:
80
diff
changeset
|
17 | logger=logging.getLogger(__name__) |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
19 | _dreamtime_monitor=None |
76
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 | # |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
22 | # Support classes for dealing with different times |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
23 | # |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
24 | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
25 | # Time snapshotting to helps to create stable comparisons of different |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
26 | # subclasses of Time. |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
27 | class Snapshot: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
28 | def __init__(self): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
29 | self._monotonic=None |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
30 | self._realtime=None |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
31 | self._dreamtime=None |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
32 | self._sleeping=None |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
33 | |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
34 | def monotonic(self): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
35 | if self._monotonic is None: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
36 | self._monotonic=time.monotonic() |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
37 | return self._monotonic |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
38 | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
39 | def realtime(self): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
40 | if self._realtime is None: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
41 | self._realtime=time.time() |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
42 | return self._realtime |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
43 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
44 | def dreamtime_sleeping(self): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
45 | if self._dreamtime is None: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
46 | if _dreamtime_monitor: |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
47 | self._dreamtime, self._sleeping=_dreamtime_monitor.dreamtime_sleeping(snapshot=self) |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
48 | else: |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
49 | self._dreamtime, self._sleeping=self.monotonic(), False |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
50 | return self._dreamtime, self._sleeping |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
51 | |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
52 | def dreamtime(self): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
53 | time, _=self.dreamtime_sleeping() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
54 | return time |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
55 | |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
56 | def sleeping(self): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
57 | _, sleeping=self.dreamtime_sleeping() |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
58 | return sleeping |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
59 | |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
60 | # The main Time class, for time advancing in various paces |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
61 | class Time: |
97 | 62 | def __init__(self, when): |
63 | self._value=when | |
64 | ||
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
65 | def _monotonic(self, snapshot): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
66 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
67 | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
68 | def _realtime(self, snapshot): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
69 | raise NotImplementedError |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
70 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
71 | @staticmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
72 | def _now(snapshot): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
73 | raise NotImplementedError |
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 | @classmethod |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
76 | def now(cls): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
77 | return cls(cls._now(Snapshot())) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
78 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
79 | @classmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
80 | def from_realtime(cls, realtime, snapshot=Snapshot()): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
81 | return cls(realtime-snapshot.realtime()+cls._now(snapshot)) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
82 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
83 | @classmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
84 | def from_monotonic(cls, monotonic, snapshot=Snapshot()): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
85 | return cls(monotonic-snapshot.monotonic()+cls._now(snapshot)) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
86 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
87 | @classmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
88 | def after(cls, seconds, snapshot=Snapshot()): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
89 | return cls(cls._now(snapshot)+seconds) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
90 | |
97 | 91 | @classmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
92 | def after_other(cls, other, seconds, snapshot=Snapshot()): |
97 | 93 | if isinstance(other, cls): |
94 | return cls(other._value+seconds) | |
95 | else: | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
96 | return cls.from_monotonic(other._monotonic(snapshot)+seconds, |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
97 | snapshot) |
97 | 98 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
99 | def datetime(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
100 | return datetime.datetime.fromtimestamp(self._realtime(snapshot)) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
101 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
102 | def seconds_to(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
103 | return self._value-self._now(snapshot) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
104 | |
97 | 105 | def isoformat(self): |
106 | return self.datetime().isoformat() | |
107 | ||
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
108 | def realtime(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
109 | return self._realtime(snapshot) |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
110 | |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
111 | def monotonic(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
112 | return self._monotonic(snapshot) |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
113 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
114 | # Counted from the monotonic epoch, how far is this event? Usually should |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
115 | # equal self.monotonic(), but Dreamtime can be stopped by system sleep, |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
116 | # and will return ∞ (math.inf). |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
117 | def horizon(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
118 | return self._monotonic(snapshot) |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
119 | |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
120 | def __compare(self, other, fn): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
121 | if isinstance(other, self.__class__): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
122 | return fn(self._value, other._value) |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
123 | else: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
124 | snapshot=Snapshot() |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
125 | return fn(self._monotonic(snapshot), other._monotonic(snapshot)) |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
126 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
127 | def __lt__(self, other): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
128 | return self.__compare(other, lambda x, y: x < y) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
129 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
130 | def __gt__(self, other): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
131 | return self.__compare(other, lambda x, y: x > y) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
132 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
133 | def __le__(self, other): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
134 | return self.__compare(other, lambda x, y: x <= y) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
135 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
136 | def __ge__(self, other): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
137 | return self.__compare(other, lambda x, y: x >= y) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
138 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
139 | def __eq__(self, other): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
140 | return self.__compare(other, lambda x, y: x == y) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
141 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
142 | class RealTime(Time): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
143 | def _realtime(self, snapshot): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
144 | return self._value |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
145 | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
146 | def _monotonic(self, snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
147 | return self._value+(snapshot.monotonic()-snapshot.realtime()) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
148 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
149 | @staticmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
150 | def _now(snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
151 | return snapshot.realtime() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
152 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
153 | class MonotonicTime(Time): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
154 | def _realtime(self, snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
155 | return self._value+(snapshot.realtime()-snapshot.monotonic()) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
156 | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
157 | def _monotonic(self, snapshot): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
158 | return self._value |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
159 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
160 | @staticmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
161 | def _now(snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
162 | return snapshot.monotonic() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
163 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
164 | # class Infinity(Time): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
165 | # def __init__(self): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
166 | # super().__init__(math.inf) |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
167 | # |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
168 | # def _realtime(self, snapshot): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
169 | # return math.inf |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
170 | # |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
171 | # def _monotonic(self, snapshot): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
172 | # return math.inf |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
173 | # |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
174 | # @staticmethod |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
175 | # def _now(snapshot): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
176 | # return 0 |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
177 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
178 | class DreamTime(Time): |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
179 | def _realtime(self, snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
180 | return self._value+(snapshot.realtime()-snapshot.dreamtime()) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
181 | |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
182 | # 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
|
183 | # 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
|
184 | # to a specific time.monotonic() realisation |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
185 | def _monotonic(self, snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
186 | return self._value+(snapshot.monotonic()-snapshot.dreamtime()) |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
187 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
188 | def horizon(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
189 | if snapshot.sleeping(): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
190 | return math.inf |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
191 | else: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
192 | return self._monotonic(snapshot) |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
193 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
194 | @staticmethod |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
195 | def _now(snapshot): |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
196 | return snapshot.dreamtime() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
197 | |
97 | 198 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
199 | if platform.system()=='Darwin': |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
200 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
201 | import Foundation |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
202 | import AppKit |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
203 | |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
204 | def do_callbacks(callbacks, woke): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
205 | for callback in callbacks.values(): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
206 | try: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
207 | callback(woke) |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
208 | except Exception: |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
209 | logger.exception("Error in sleep/wake notification callback") |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
210 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
211 | # |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
212 | # Wake up / sleep handling |
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 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
215 | class SleepHandler(Foundation.NSObject): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
216 | """ Handle wake/sleep notifications """ |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
217 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
218 | def init(self): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
219 | self.__sleeptime=None |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
220 | self.__slept=0 |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
221 | self.__epoch=time.monotonic() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
222 | self.__lock=threading.Lock() |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
223 | self.__callbacks=weakref.WeakKeyDictionary() |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
224 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
225 | return self |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
226 | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
227 | @protect_noreturn |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
228 | def handleSleepNotification_(self, aNotification): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
229 | logger.info("System going to sleep") |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
230 | now=time.monotonic() |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
231 | with self.__lock: |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
232 | self.__sleeptime=now |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
233 | callbacks=self.__callbacks.copy() |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
234 | do_callbacks(callbacks, False) |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
235 | |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
236 | @protect_noreturn |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
237 | def handleWakeNotification_(self, aNotification): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
238 | logger.info("System waking up from sleep") |
106
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
239 | now=time.monotonic() |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
240 | with self.__lock: |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
241 | if self.__sleeptime: |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
242 | slept=max(0, now-self.__sleeptime) |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
243 | logger.info("Slept %f seconds" % slept) |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
244 | self.__slept=self.__slept+slept |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
245 | self.__sleeptime=None |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
246 | callbacks=self.__callbacks.copy() |
a7bdc239ef62
Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
101
diff
changeset
|
247 | do_callbacks(callbacks, True) |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
248 | |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
249 | # Return dreamtime |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
250 | def dreamtime_sleeping(self, snapshot=Snapshot()): |
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
251 | sleeping=False |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
252 | with self.__lock: |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
253 | # macOS "sleep" signals / status are complete bollocks: at least |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
254 | # when plugged in, the system is actually sometimes running all |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
255 | # night with the lid closed and sleepNotification delivered. |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
256 | # Therefore, we need our timers to work in a sane manner when |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
257 | # we should be sleeping! |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
258 | if self.__sleeptime is not None: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
259 | now_monotonic=self.__sleeptime |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
260 | sleeping=True |
100
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
261 | else: |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
262 | now_monotonic=snapshot.monotonic() |
b141bed9e718
macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents:
97
diff
changeset
|
263 | now_dreamtime=max(0, now_monotonic-self.__epoch-self.__slept) |
101
3068b0de12ee
Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents:
100
diff
changeset
|
264 | return now_dreamtime, sleeping |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
265 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
266 | # 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
|
267 | 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
|
268 | with self.__lock: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
269 | self.__callbacks[obj]=callback |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
270 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
271 | # 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
|
272 | 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
|
273 | global _dreamtime_monitor |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
274 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
275 | monitor=_dreamtime_monitor |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
276 | if not monitor: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
277 | 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
|
278 | else: |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
279 | monitor.addForObj_aCallback_(obj, callback) |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
280 | |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
281 | def start_monitoring(): |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
282 | global _dreamtime_monitor |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
283 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
284 | logger.debug("Starting to monitor system sleep") |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
285 | workspace = AppKit.NSWorkspace.sharedWorkspace() |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
286 | notification_center = workspace.notificationCenter() |
77
e8773133bf79
DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents:
76
diff
changeset
|
287 | _dreamtime_monitor = SleepHandler.new() |
68
72f821e17123
Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
288 | |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
289 | 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
|
290 | _dreamtime_monitor, |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
291 | "handleSleepNotification:", |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
292 | AppKit.NSWorkspaceWillSleepNotification, |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
293 | None) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
294 | |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
295 | 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
|
296 | _dreamtime_monitor, |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
297 | "handleWakeNotification:", |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
298 | AppKit.NSWorkspaceDidWakeNotification, |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
299 | None) |
94
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
300 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
301 | else: # Not on macOS |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
302 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
303 | 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
|
304 | pass |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
305 | |
2a11fd29c192
Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents:
89
diff
changeset
|
306 | def start_monitoring(): |
76
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
307 | 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
|
308 | % platform.system())) |
4b08fca3ce34
Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents:
69
diff
changeset
|
309 |