32
32
# =============================================================================
33
33
__all__ = ('Delay' ,
34
34
'GameThread' ,
35
- 'TickRepeat ' ,
36
- 'TickRepeatStatus ' ,
35
+ 'Repeat ' ,
36
+ 'RepeatStatus ' ,
37
37
)
38
38
39
39
@@ -122,7 +122,8 @@ def __init__(self, delay, callback, *args, **kwargs):
122
122
raise ValueError ('Given callback is not callable.' )
123
123
124
124
self .delay = delay
125
- self .exec_time = time .time () + delay
125
+ self ._start_time = time .time ()
126
+ self .exec_time = self ._start_time + delay
126
127
self .callback = callback
127
128
self .args = args
128
129
self .kwargs = kwargs
@@ -153,6 +154,20 @@ def running(self):
153
154
"""Return True if the delay running."""
154
155
return self in _delay_manager
155
156
157
+ @property
158
+ def time_remaining (self ):
159
+ if not self .running :
160
+ # TODO: what should we return here, or should we raise an error?
161
+ return None
162
+ return self .exec_time - time .time ()
163
+
164
+ @property
165
+ def time_elapsed (self ):
166
+ if not self .running :
167
+ # TODO: what should we return here, or should we raise an error?
168
+ return None
169
+ return time .time () - self ._start_time
170
+
156
171
def _unload_instance (self ):
157
172
with suppress (ValueError ):
158
173
self .cancel ()
@@ -161,15 +176,15 @@ def _unload_instance(self):
161
176
# =============================================================================
162
177
# >> REPEAT CLASSES
163
178
# =============================================================================
164
- class TickRepeatStatus (IntEnum ):
165
- """Class used to store TickRepeatStatus values."""
179
+ class RepeatStatus (IntEnum ):
180
+ """Class used to store RepeatStatus values."""
166
181
167
182
STOPPED = 1
168
183
RUNNING = 2
169
184
PAUSED = 3
170
185
171
186
172
- class TickRepeat (AutoUnload ):
187
+ class Repeat (AutoUnload ):
173
188
"""Class used to create and call repeats."""
174
189
175
190
def __init__ (self , callback , * args , ** kwargs ):
@@ -181,41 +196,41 @@ def __init__(self, callback, *args, **kwargs):
181
196
182
197
# Log the __init__ message
183
198
listeners_tick_logger .log_debug (
184
- 'TickRepeat .__init__: <{0}> <{1}> <{2}>' .format (
199
+ 'Repeat .__init__: <{0}> <{1}> <{2}>' .format (
185
200
self .callback , self .args , self .kwargs ))
186
201
187
202
# Set up private attributes
188
203
self ._interval = 0
189
204
self ._limit = 0
190
205
self ._count = 0
191
206
self ._adjusted = 0
192
- self ._status = TickRepeatStatus .STOPPED
207
+ self ._status = RepeatStatus .STOPPED
193
208
self ._delay = None
194
209
self ._loop_time = None
195
210
196
- def start (self , interval , limit ):
211
+ def start (self , interval , limit , execute_on_start = False ):
197
212
"""Start the repeat loop."""
198
213
# Log the start message
199
214
listeners_tick_logger .log_debug (
200
- 'TickRepeat .start: <{0}> <{1}>' .format (interval , limit ))
215
+ 'Repeat .start: <{0}> <{1}>' .format (interval , limit ))
201
216
202
217
# Is the repeat already running?
203
- if self ._status is TickRepeatStatus .RUNNING :
218
+ if self ._status is RepeatStatus .RUNNING :
204
219
205
220
# Log the status
206
221
listeners_tick_logger .log_debug (
207
- 'TickRepeat .start - TickRepeatStatus .RUNNING' )
222
+ 'Repeat .start - RepeatStatus .RUNNING' )
208
223
209
224
# Do not start the repeat
210
225
return
211
226
212
227
# Log starting the repeat
213
228
listeners_tick_logger .log_debug (
214
- 'TickRepeat .start - !TickRepeatStatus ' +
215
- '.RUNNING - Starting TickRepeat ' )
229
+ 'Repeat .start - !RepeatStatus ' +
230
+ '.RUNNING - Starting Repeat ' )
216
231
217
232
# Set the status to running
218
- self ._status = TickRepeatStatus .RUNNING
233
+ self ._status = RepeatStatus .RUNNING
219
234
220
235
# Set the given attributes
221
236
self ._interval = interval
@@ -228,35 +243,39 @@ def start(self, interval, limit):
228
243
# Start the delay
229
244
self ._delay = Delay (self ._interval , self ._execute )
230
245
246
+ # Call the callback if set to execute on start
247
+ if execute_on_start :
248
+ self .callback (* self .args , ** self .kwargs )
249
+
231
250
def stop (self ):
232
251
"""Stop the repeat loop."""
233
252
# Log the stop message
234
- listeners_tick_logger .log_debug ('TickRepeat .stop' )
253
+ listeners_tick_logger .log_debug ('Repeat .stop' )
235
254
236
255
# Is the repeat running?
237
- if self ._status is not TickRepeatStatus .RUNNING :
256
+ if self ._status is not RepeatStatus .RUNNING :
238
257
239
258
# Log the status
240
259
listeners_tick_logger .log_debug (
241
- 'TickRepeat .stop - !TickRepeatStatus .RUNNING' )
260
+ 'Repeat .stop - !RepeatStatus .RUNNING' )
242
261
243
262
# No need to stop it
244
263
return
245
264
246
265
# Log stopping the repeat
247
266
listeners_tick_logger .log_debug (
248
- 'TickRepeat .stop - TickRepeatStatus .RUNNING - Stopping TickRepeat ' )
267
+ 'Repeat .stop - RepeatStatus .RUNNING - Stopping Repeat ' )
249
268
250
269
# Set the status to stopped
251
- self ._status = TickRepeatStatus .STOPPED
270
+ self ._status = RepeatStatus .STOPPED
252
271
253
272
# Cancel the delay
254
273
self ._delay .cancel ()
255
274
256
275
def restart (self ):
257
276
"""Restart the repeat."""
258
277
# Log restarting the repeat
259
- listeners_tick_logger .log_debug ('TickRepeat .restart' )
278
+ listeners_tick_logger .log_debug ('Repeat .restart' )
260
279
261
280
# Stop the repeat
262
281
self .stop ()
@@ -270,24 +289,24 @@ def pause(self):
270
289
Pausing allows the repeat to be resumed.
271
290
"""
272
291
# Log the pause message
273
- listeners_tick_logger .log_debug ('TickRepeat .pause' )
292
+ listeners_tick_logger .log_debug ('Repeat .pause' )
274
293
275
294
# Is the repeat running?
276
- if self ._status is not TickRepeatStatus .RUNNING :
295
+ if self ._status is not RepeatStatus .RUNNING :
277
296
278
297
# Log the status
279
298
listeners_tick_logger .log_debug (
280
- 'TickRepeat .pause - !TickRepeatStatus .RUNNING' )
299
+ 'Repeat .pause - !RepeatStatus .RUNNING' )
281
300
282
301
# No need to pause
283
302
return
284
303
285
304
# Log pausing the repeat
286
305
listeners_tick_logger .log_debug (
287
- 'TickRepeat .pause - TickRepeatStatus .RUNNING - Pausing TickRepeat ' )
306
+ 'Repeat .pause - RepeatStatus .RUNNING - Pausing Repeat ' )
288
307
289
308
# Set the status to paused
290
- self ._status = TickRepeatStatus .PAUSED
309
+ self ._status = RepeatStatus .PAUSED
291
310
292
311
# Set the remaining time in the current loop
293
312
self ._loop_time = self ._delay .exec_time - time .time ()
@@ -301,40 +320,40 @@ def resume(self):
301
320
Can only resume if in paused status.
302
321
"""
303
322
# Log the resume message
304
- listeners_tick_logger .log_debug ('TickRepeat .resume' )
323
+ listeners_tick_logger .log_debug ('Repeat .resume' )
305
324
306
325
# Is the repeat paused?
307
- if self ._status is not TickRepeatStatus .PAUSED :
326
+ if self ._status is not RepeatStatus .PAUSED :
308
327
309
328
# Log the status
310
329
listeners_tick_logger .log_debug (
311
- 'TickRepeat .resume - !TickRepeatStatus .PAUSED' )
330
+ 'Repeat .resume - !RepeatStatus .PAUSED' )
312
331
313
332
# Do not resume
314
333
return
315
334
316
335
# Log resuming the repeat
317
336
listeners_tick_logger .log_debug (
318
- 'TickRepeat .resume - TickRepeatStatus .' +
319
- 'PAUSED - Resuming TickRepeat ' )
337
+ 'Repeat .resume - RepeatStatus .' +
338
+ 'PAUSED - Resuming Repeat ' )
320
339
321
340
# Set the status to running
322
- self ._status = TickRepeatStatus .RUNNING
341
+ self ._status = RepeatStatus .RUNNING
323
342
324
343
# Start the delay
325
344
self ._delay = Delay (self ._loop_time , self ._execute )
326
345
327
346
def extend (self , adjustment ):
328
347
"""Add to the number of loops to be made."""
329
348
# Log the extend message
330
- listeners_tick_logger .log_debug ('TickRepeat .extend' )
349
+ listeners_tick_logger .log_debug ('Repeat .extend' )
331
350
332
351
# Is there a limit for this repeat?
333
- if not self .limit :
352
+ if not self .loop_limit :
334
353
335
354
# Log a message about no reducing
336
355
listeners_tick_logger .log_debug (
337
- 'Unable to extend, TickRepeat instance has no limit.' )
356
+ 'Unable to extend, Repeat instance has no limit.' )
338
357
339
358
# No need to go further
340
359
return
@@ -351,14 +370,14 @@ def extend(self, adjustment):
351
370
def reduce (self , adjustment ):
352
371
"""Reduce the number of loops to be made."""
353
372
# Log the reduce message
354
- listeners_tick_logger .log_debug ('TickRepeat .reduce' )
373
+ listeners_tick_logger .log_debug ('Repeat .reduce' )
355
374
356
375
# Is there a limit for this repeat?
357
- if not self .limit :
376
+ if not self .loop_limit :
358
377
359
378
# Log a message about no reducing
360
379
listeners_tick_logger .log_debug (
361
- 'Unable to reduce, TickRepeat instance has no limit.' )
380
+ 'Unable to reduce, Repeat instance has no limit.' )
362
381
363
382
# No need to go further
364
383
return
@@ -373,41 +392,41 @@ def reduce(self, adjustment):
373
392
self ._adjusted -= adjustment
374
393
375
394
# Are no more loops to be made?
376
- if (self .remaining <= 0 and
377
- self .status is TickRepeatStatus .RUNNING ):
395
+ if (self .remaining_loops <= 0 and
396
+ self .status is RepeatStatus .RUNNING ):
378
397
379
398
# Log the reduce-stopping message
380
399
listeners_tick_logger .log_debug (
381
- 'TickRepeat .reduce - Reduce caused repeat to stop' )
400
+ 'Repeat .reduce - Reduce caused repeat to stop' )
382
401
383
402
# Stop the repeat
384
403
self .stop ()
385
404
386
405
def _execute (self ):
387
406
"""Execute the repeat's callback with its arguments and keywords."""
388
407
# Log the _execute message
389
- listeners_tick_logger .log_debug ('TickRepeat ._execute' )
408
+ listeners_tick_logger .log_debug ('Repeat ._execute' )
390
409
391
410
# Add one to the current count
392
411
self ._count += 1
393
412
394
413
# Are any more loops to be made?
395
- if self .remaining or not self ._limit :
414
+ if self .remaining_loops or not self ._limit :
396
415
397
416
# Is there no limit?
398
417
if not self ._limit :
399
418
400
419
# Log continuing the loop
401
420
listeners_tick_logger .log_debug (
402
- 'TickRepeat ._execute - No limit' )
421
+ 'Repeat ._execute - No limit' )
403
422
404
423
# Is there a limit?
405
424
else :
406
425
407
426
# Log continuing the loop
408
427
listeners_tick_logger .log_debug (
409
- 'TickRepeat ._execute - Remaining - {0}' .format (
410
- self .remaining ))
428
+ 'Repeat ._execute - Remaining - {0}' .format (
429
+ self .remaining_loops ))
411
430
412
431
# Call the delay again
413
432
self ._delay = Delay (self ._interval , self ._execute )
@@ -417,16 +436,16 @@ def _execute(self):
417
436
418
437
# Log stopping the repeat
419
438
listeners_tick_logger .log_debug (
420
- 'TickRepeat ._execute - Stopping the loop' )
439
+ 'Repeat ._execute - Stopping the loop' )
421
440
422
441
# Set the status to stopped
423
- self ._status = TickRepeatStatus .STOPPED
442
+ self ._status = RepeatStatus .STOPPED
424
443
425
444
# Call the repeat's callback
426
445
self .callback (* self .args , ** self .kwargs )
427
446
428
447
@property
429
- def remaining (self ):
448
+ def remaining_loops (self ):
430
449
"""Return the remaining number of loops in the repeat."""
431
450
# Is there no limit?
432
451
if not self ._limit :
@@ -435,15 +454,15 @@ def remaining(self):
435
454
return self ._limit
436
455
437
456
# Return the remaining number of loops
438
- return self .limit - self ._count
457
+ return self .loop_limit - self ._count
439
458
440
459
@property
441
- def count (self ):
460
+ def loop_count (self ):
442
461
"""Return the current number of loops made in the repeat."""
443
462
return self ._count
444
463
445
464
@property
446
- def limit (self ):
465
+ def loop_limit (self ):
447
466
"""Return the total number of loops to be made."""
448
467
# Is there no limit?
449
468
if not self ._limit :
@@ -455,25 +474,36 @@ def limit(self):
455
474
return self ._limit + self ._adjusted
456
475
457
476
@property
458
- def timeleft (self ):
477
+ def time_left (self ):
459
478
"""Return the remaining time till the end of the repeat."""
460
- return self .remaining * self ._interval
479
+ return (
480
+ self .remaining_loops * self ._interval +
481
+ self .delay_time_remaining
482
+ )
461
483
462
484
@property
463
- def elapsed (self ):
485
+ def time_elapsed (self ):
464
486
"""Return the elapsed time since the repeat started."""
465
- return self ._count * self ._interval
487
+ return self .total_time - self .time_left
466
488
467
489
@property
468
490
def total_time (self ):
469
491
"""Return the total time it will take to complete the repeat."""
470
- return self .limit * self ._interval
492
+ return self .loop_limit * self ._interval
471
493
472
494
@property
473
495
def status (self ):
474
496
"""Return the status of the repeat."""
475
497
return self ._status
476
498
499
+ @property
500
+ def delay_time_remaining (self ):
501
+ return self ._delay .time_remaining
502
+
503
+ @property
504
+ def delay_time_elapsed (self ):
505
+ return self ._delay .time_elapsed
506
+
477
507
def _unload_instance (self ):
478
508
"""Stop the repeat with being unloaded."""
479
509
self .stop ()
0 commit comments