17
17
from listeners import listeners_logger
18
18
19
19
20
- # =============================================================================
21
- # >> ALL DECLARATION
22
- # =============================================================================
23
- __all__ = ('tick_delays' ,
24
- )
25
-
26
-
27
20
# =============================================================================
28
21
# >> GLOBAL VARIABLES
29
22
# =============================================================================
34
27
# =============================================================================
35
28
# >> CLASSES
36
29
# =============================================================================
37
- class _Delay (object ):
30
+ class Delay (object ):
38
31
39
32
"""Stores a callback to be called at a later time."""
40
33
41
34
def __init__ (self , seconds , callback , * args , ** kwargs ):
42
35
"""Called when an instance is instantiated."""
43
36
# Log the init message
44
37
listeners_tick_delays_logger .log_info (
45
- '_Delay .__init__ <{0}> <{1}> <{2}> <{3}>' .format (
38
+ 'Delay .__init__ <{0}> <{1}> <{2}> <{3}>' .format (
46
39
seconds , callback , args , kwargs ))
47
40
48
41
# Store the time to execute the callback
49
- self .exec_time = time .time () + seconds
42
+ self ._exec_time = time .time () + seconds
50
43
51
44
# Store the callback, arguments, and keywords
52
45
self .callback = callback
@@ -57,7 +50,7 @@ def __call__(self):
57
50
"""Call the delay with the proper arguments and keywords."""
58
51
# Log the call message
59
52
listeners_tick_delays_logger .log_info (
60
- '_Delay .__call__ - Try to call - <{0}> <{1}> <{2}>' .format (
53
+ 'Delay .__call__ - Try to call - <{0}> <{1}> <{2}>' .format (
61
54
self .callback , self .args , self .kwargs ))
62
55
63
56
# Use try/except in case an error is encountered
@@ -72,6 +65,15 @@ def __call__(self):
72
65
# Print the exception to the console
73
66
except_hooks .print_exception ()
74
67
68
+ @property
69
+ def exec_time (self ):
70
+ """Return the time to execute the delayed function."""
71
+ return self ._exec_time
72
+
73
+ def cancel (self ):
74
+ """Cancel the delay."""
75
+ tick_delays .cancel_delay (self )
76
+
75
77
76
78
class _Times (list ):
77
79
@@ -157,11 +159,11 @@ def __delitem__(self, item):
157
159
158
160
def delay (self , seconds , callback , * args , ** kwargs ):
159
161
"""Create a delay."""
160
- # Get the _Delay instance for the given arguments
161
- delay_object = _Delay (seconds , callback , * args , ** kwargs )
162
+ # Get the Delay instance for the given arguments
163
+ delay_object = Delay (seconds , callback , * args , ** kwargs )
162
164
163
- # Add the _Delay instance to the dictionary using its execution time
164
- self [delay_object .exec_time ].append (delay_object )
165
+ # Add the Delay instance to the dictionary using its execution time
166
+ self [delay_object ._exec_time ].append (delay_object )
165
167
166
168
# Return the object
167
169
return delay_object
@@ -199,37 +201,37 @@ def cancel_delay(self, delay_object):
199
201
listeners_tick_delays_logger .log_info (
200
202
'tick_delays.cancel_delay <{0}>' .format (delay_object ))
201
203
202
- # Is the given argument a _Delay object?
203
- if not isinstance (delay_object , _Delay ):
204
+ # Is the given argument a Delay object?
205
+ if not isinstance (delay_object , Delay ):
204
206
205
207
# If not, raise an error
206
208
raise TypeError (
207
- 'tick_delays.cancel_delay requires a _Delay instance.' )
209
+ 'tick_delays.cancel_delay requires a Delay instance.' )
208
210
209
- # Is the given _Delay object's time no longer in the dictionary?
210
- if delay_object .exec_time not in self :
211
+ # Is the given Delay object's time no longer in the dictionary?
212
+ if delay_object ._exec_time not in self :
211
213
212
214
# If not, raise an error
213
215
raise KeyError ('Object is no longer registered.' )
214
216
215
217
# Log the removing from list message
216
218
listeners_tick_delays_logger .log_info (
217
219
'tick_delays.cancel_delay - Removing from '
218
- '<{0}>' .format (delay_object .exec_time ))
220
+ '<{0}>' .format (delay_object ._exec_time ))
219
221
220
222
# Remove the delay from its time
221
- self [delay_object .exec_time ].remove (delay_object )
223
+ self [delay_object ._exec_time ].remove (delay_object )
222
224
223
225
# Does the delay's time have any remaining objects?
224
- if not self [delay_object .exec_time ]:
226
+ if not self [delay_object ._exec_time ]:
225
227
226
228
# Log the deletion of the time from the dictionary message
227
229
listeners_tick_delays_logger .log_info (
228
230
'tick_delays.cancel_delay - Removing <{0}> '
229
- 'from dictionary' .format (delay_object .exec_time ))
231
+ 'from dictionary' .format (delay_object ._exec_time ))
230
232
231
233
# If not, remove the delay's time from the dictionary
232
- del self [delay_object .exec_time ]
234
+ del self [delay_object ._exec_time ]
233
235
234
236
# Are there any remaining delays?
235
237
if not self :
0 commit comments