borgend/dreamtime.py

Tue, 06 Feb 2018 20:16:59 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Tue, 06 Feb 2018 20:16:59 +0000
changeset 111
c3bc27cf5ece
parent 106
a7bdc239ef62
child 113
6993964140bd
permissions
-rw-r--r--

Changed dreamtime calculation.
The monotonic clock (time.monotonic()) sometimes advances unreliably
in sleep, so isn't reliable for calculating sleeping time. We have to
use time.time(). But this may be changed by the user, so we only use
it to track sleep periods, and still use time.monotonic() to track
wake periods.

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
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
62 def __init__(self, when):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
63 self._value=when
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
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
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
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
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
93 if isinstance(other, cls):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
94 return cls(other._value+seconds)
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
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
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
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
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
105 def isoformat(self):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
106 return self.datetime().isoformat()
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
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
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
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 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
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
218 # We need to use the actual time.time() to monitor sleep, as
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
219 # time.monotonic() many not run during sleep. But time.time()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
220 # may encounter adjustments, so we also use time.monotonic() to
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
221 # monitor wake periods.
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
222
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
223 def init(self):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
224 self.__sleeptime=None
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
225 self.__dreamtime_last_sleep=0
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
226 self.__monotonic_last_wakeup=time.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
227 self.__slept=0 # Only used to store the statistic
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
228 self.__lock=threading.Lock()
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
229 self.__callbacks=weakref.WeakKeyDictionary()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
230
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
231 return self
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
233 @protect_noreturn
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
234 def handleSleepNotification_(self, aNotification):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
235 logger.info("System going to sleep")
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
236 with self.__lock:
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
237 self.__sleeptime=time.time()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
238 now_m=time.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
239 self.__dreamtime_last_sleep=(self.__dreamtime_last_sleep+now_m
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
240 -self.__monotonic_last_wakeup)
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
241 callbacks=self.__callbacks.copy()
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
242 do_callbacks(callbacks, False)
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
243
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
244 @protect_noreturn
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
245 def handleWakeNotification_(self, aNotification):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
246 logger.info("System waking up from sleep")
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
247 with self.__lock:
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
248 if self.__sleeptime:
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
249 self.__monotonic_last_wakeup=time.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
250 now=time.time()
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
251 slept=max(0, now-self.__sleeptime)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
252 logger.info("Slept %f seconds" % slept)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
253 self.__slept=self.__slept+slept
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
254 self.__sleeptime=None
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
255 callbacks=self.__callbacks.copy()
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
256 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
257
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
258 # Return dreamtime
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
259 def dreamtime_sleeping(self, snapshot=Snapshot()):
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
260 with self.__lock:
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
261 # 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
262 # 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
263 # 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
264 # 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
265 # we should be sleeping!
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
266 if self.__sleeptime is not None:
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
267 sleeping=True
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
268 now_dreamtime=self.__dreamtime_last_sleep
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
269 else:
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
270 sleeping=False
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
271 now=snapshot.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
272 now_dreamtime=(self.__dreamtime_last_sleep
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
273 +now-self.__monotonic_last_wakeup);
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
274 return now_dreamtime, sleeping
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
275
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
276 # 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
277 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
278 with self.__lock:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
279 self.__callbacks[obj]=callback
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
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 # 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
282 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
283 global _dreamtime_monitor
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
284
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
285 monitor=_dreamtime_monitor
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
286 if not monitor:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
287 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
288 else:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
289 monitor.addForObj_aCallback_(obj, callback)
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
290
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
291 def start_monitoring():
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
292 global _dreamtime_monitor
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
293
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
294 logger.debug("Starting to monitor system sleep")
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
295 workspace = AppKit.NSWorkspace.sharedWorkspace()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
296 notification_center = workspace.notificationCenter()
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
297 _dreamtime_monitor = SleepHandler.new()
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
298
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
299 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
300 _dreamtime_monitor,
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
301 "handleSleepNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
302 AppKit.NSWorkspaceWillSleepNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
303 None)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
304
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
305 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
306 _dreamtime_monitor,
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
307 "handleWakeNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
308 AppKit.NSWorkspaceDidWakeNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
309 None)
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
310
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
311 else: # Not on macOS
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
312
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
313 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
314 pass
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
315
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
316 def start_monitoring():
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
317 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
318 % platform.system()))
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
319

mercurial