borgend/dreamtime.py

Mon, 17 Sep 2018 19:31:22 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 17 Sep 2018 19:31:22 -0500
changeset 117
b509a4e34d7f
parent 113
6993964140bd
permissions
-rw-r--r--

xdg include fix?

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
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
60 # Python's default arguments are purely idiotic (aka. pythonic): generated
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
61 # only once. This makes sense in a purely functional language, which Python
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
62 # lightyears away from, but severely limits their usefulness in an imperative
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
63 # language. Decorators also seem clumsy for this, as one would have to tell
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
64 # the number of positional arguments for things to work nice, being able to
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
65 # pass the snapshot both positionally and as keyword. No luck.
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
66 # So have to do things the old-fashioned hard way.
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
67 def ensure_snapshot(snapshot):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
68 if not snapshot:
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
69 return Snapshot()
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
70 else:
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
71 return snapshot
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
72
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
73 # 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
74 class Time:
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
75 def __init__(self, when):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
76 self._value=when
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
77
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
78 @staticmethod
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
79 def _now(snapshot):
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
80 raise NotImplementedError
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
81
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
82 def remaining(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
83 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
84 return self._value-self._now(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
85
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
86 # Mostly equal to remaining() but can be ∞ (math.inf) for DreamTime.
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
87 def horizon(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
88 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
89 return self.remaining(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
90
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
91 def realtime(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
92 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
93 return snapshot.realtime()+self.remaining(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
94
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
95 def monotonic(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
96 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
97 return snapshot.monotonic()+self.remaining(snapshot)
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
98
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
99 @classmethod
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
100 def now(cls, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
101 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
102 return cls(cls._now(snapshot))
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
103
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
104 @classmethod
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
105 def from_realtime(cls, realtime, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
106 snapshot=ensure_snapshot(snapshot)
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
107 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
108
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
109 @classmethod
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
110 def from_monotonic(cls, monotonic, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
111 snapshot=ensure_snapshot(snapshot)
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
112 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
113
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
114 @classmethod
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
115 def after(cls, seconds, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
116 snapshot=ensure_snapshot(snapshot)
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
117 return cls(cls._now(snapshot)+seconds)
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
118
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
119 @classmethod
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
120 def after_other(cls, other, seconds, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
121 snapshot=ensure_snapshot(snapshot)
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
122 if isinstance(other, cls):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
123 return cls(other._value+seconds)
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
124 else:
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
125 return cls.from_monotonic(other.monotonic(snapshot)+seconds,
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
126 snapshot)
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
127
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
128 def datetime(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
129 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
130 return datetime.datetime.fromtimestamp(self.realtime(snapshot))
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
131
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
132 def isoformat(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
133 snapshot=ensure_snapshot(snapshot)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
134 return self.datetime(snapshot).isoformat()
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
135
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
136 def __compare(self, other, fn):
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
137 if isinstance(other, self.__class__):
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
138 return fn(self._value, other._value)
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
139 else:
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
140 snapshot=Snapshot()
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
141 return fn(self.monotonic(snapshot), other.monotonic(snapshot))
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
142
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
143 def __lt__(self, other):
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
144 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
145
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
146 def __gt__(self, other):
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
147 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
148
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
149 def __le__(self, other):
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
150 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
151
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
152 def __ge__(self, other):
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
153 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
154
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
155 def __eq__(self, other):
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
156 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
157
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
158
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
159 class RealTime(Time):
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.realtime()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
163
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
164 def realtime(self, snapshot=None):
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
165 return self._value
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
166
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
167
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
168 class MonotonicTime(Time):
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
169 @staticmethod
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
170 def _now(snapshot):
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
171 return snapshot.monotonic()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
172
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
173 def monotonic(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
174 return self._value
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
175
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
176
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
177 class DreamTime(Time):
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
178 @staticmethod
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
179 def _now(snapshot):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
180 return snapshot.dreamtime()
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
181
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
182 def horizon(self, snapshot=None):
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
183 snapshot=ensure_snapshot(snapshot)
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
184 if snapshot.sleeping():
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
185 return math.inf
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
186 else:
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
187 return self.remaining(snapshot)
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
188
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
189
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 94
diff changeset
190
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
191 if platform.system()=='Darwin':
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
192
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
193 import Foundation
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
194 import AppKit
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
195
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
196 def do_callbacks(callbacks, woke):
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
197 for callback in callbacks.values():
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
198 try:
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
199 callback(woke)
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
200 except Exception:
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
201 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
202
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
203 #
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
204 # Wake up / sleep handling
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
205 #
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
206
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
207 class SleepHandler(Foundation.NSObject):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
208 """ Handle wake/sleep notifications """
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
210 # 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
211 # time.monotonic() many not run during sleep. But time.time()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
212 # may encounter adjustments, so we also use time.monotonic() to
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
213 # monitor wake periods.
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
214
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
215 def init(self):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
216 self.__sleeptime=None
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
217 self.__dreamtime_last_sleep=0
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
218 self.__monotonic_last_wakeup=time.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
219 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
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
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
225 @protect_noreturn
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
226 def handleSleepNotification_(self, aNotification):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
227 logger.info("System going to sleep")
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
228 with self.__lock:
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
229 self.__sleeptime=time.time()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
230 now_m=time.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
231 self.__dreamtime_last_sleep=(self.__dreamtime_last_sleep+now_m
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
232 -self.__monotonic_last_wakeup)
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
233 #logger.debug("Sleeping; monotonic time now: %f; dreamtime_last_sleep: %f" % (now_m, self.__dreamtime_last_sleep))
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
234 callbacks=self.__callbacks.copy()
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
235 do_callbacks(callbacks, False)
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
237 @protect_noreturn
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
238 def handleWakeNotification_(self, aNotification):
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
239 logger.info("System waking up from sleep")
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
240 with self.__lock:
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
241 if self.__sleeptime:
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
242 self.__monotonic_last_wakeup=time.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
243 now=time.time()
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
244 slept=max(0, now-self.__sleeptime)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
245 self.__slept=self.__slept+slept
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
246 self.__sleeptime=None
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
247 logger.info("Slept %f seconds", slept)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
248 #logger.debug("Slept %f seconds; total: %f; monotonic time now: %f" % (slept, self.__slept, self.__monotonic_last_wakeup))
106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
249 callbacks=self.__callbacks.copy()
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents: 101
diff changeset
250 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
251
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
252 # Return dreamtime
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
253 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
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:
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
261 sleeping=True
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
262 now_dreamtime=self.__dreamtime_last_sleep
100
b141bed9e718 macOS "sleep" signals / status are complete bollocks:
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
263 else:
111
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
264 sleeping=False
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
265 now=snapshot.monotonic()
c3bc27cf5ece Changed dreamtime calculation.
Tuomo Valkonen <tuomov@iki.fi>
parents: 106
diff changeset
266 now_dreamtime=(self.__dreamtime_last_sleep
113
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
267 +now-self.__monotonic_last_wakeup)
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
268 #logger.debug("Dreamtime request: last_sleep: %f now: %f; last wakeup: %f => now %f dreamtime "
6993964140bd Time snapshot fixes.
Tuomo Valkonen <tuomov@iki.fi>
parents: 111
diff changeset
269 # % (self.__dreamtime_last_sleep, now, self.__monotonic_last_wakeup, now_dreamtime))
101
3068b0de12ee Part 2 of handling macOS sleep/wake signal brokenness.
Tuomo Valkonen <tuomov@iki.fi>
parents: 100
diff changeset
270 return now_dreamtime, sleeping
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
271
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
272 # 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
273 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
274 with self.__lock:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
275 self.__callbacks[obj]=callback
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
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 # 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
278 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
279 global _dreamtime_monitor
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
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 monitor=_dreamtime_monitor
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
282 if not monitor:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
283 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
284 else:
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
285 monitor.addForObj_aCallback_(obj, callback)
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
286
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
287 def start_monitoring():
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
288 global _dreamtime_monitor
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
289
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
290 logger.debug("Starting to monitor system sleep")
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
291 workspace = AppKit.NSWorkspace.sharedWorkspace()
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
292 notification_center = workspace.notificationCenter()
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
293 _dreamtime_monitor = SleepHandler.new()
68
72f821e17123 Added basic sleep/wake detection code for MacOS
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
294
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
295 notification_center.addObserver_selector_name_object_(
77
e8773133bf79 DreamTime.monotonic() no longer needs to call time.monotonic()
Tuomo Valkonen <tuomov@iki.fi>
parents: 76
diff changeset
296 _dreamtime_monitor,
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
297 "handleSleepNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
298 AppKit.NSWorkspaceWillSleepNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
299 None)
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
300
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
301 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
302 _dreamtime_monitor,
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
303 "handleWakeNotification:",
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
304 AppKit.NSWorkspaceDidWakeNotification,
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
305 None)
94
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
306
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
307 else: # Not on macOS
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
308
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
309 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
310 pass
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
311
2a11fd29c192 Make dreamtime module not throw up on non-macOS systems
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
312 def start_monitoring():
76
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
313 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
314 % platform.system()))
4b08fca3ce34 Dreamtime scheduling: discount system sleep periods
Tuomo Valkonen <tuomov@iki.fi>
parents: 69
diff changeset
315

mercurial