borgend/dreamtime.py

Sun, 04 Feb 2018 01:27:38 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 04 Feb 2018 01:27:38 +0000
changeset 101
3068b0de12ee
parent 100
b141bed9e718
child 106
a7bdc239ef62
permissions
-rw-r--r--

Part 2 of handling macOS sleep/wake signal brokenness.
a) Added Time.horizon(), which indicates how far the Time event is from the
epoch, in monotonic time. For DreamTime, if the system is sleeping, this
returns ∞. b) The DreamTime monitor also signals sleep to callbacks, so
that the Scheduler can re-sort the events. The sorting is now done by
the horizon, so DreamTime events will be moved last and not activated when
the system is sleeping or "sleeping", but other events will be executed
normally if the system is merely "sleeping".

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

mercurial