Skip to content

Commit c87e739

Browse files
committed
Fixes for issue #142.
Changed TickRepeat/TickRepeatStatus to Repeat/RepeatStatus to match Delay class naming convention. Added time_remaining/time_elapsed properties to Delay class. Added execute_on_start argument to Repeat.start to know if the function needs to be executed before any delays occur. Changed Repeat properties remaining, count, limit, and elapsed to remaining_loops, loop_count, loop_limit, and time_elapsed to better describe what the property is for. Added Repeat properties delay_time_remaining/delay_time_elapsed to return the values for the current delay.
1 parent daf06d0 commit c87e739

File tree

2 files changed

+91
-61
lines changed

2 files changed

+91
-61
lines changed

addons/source-python/packages/source-python/listeners/tick.py

Lines changed: 87 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
# =============================================================================
3333
__all__ = ('Delay',
3434
'GameThread',
35-
'TickRepeat',
36-
'TickRepeatStatus',
35+
'Repeat',
36+
'RepeatStatus',
3737
)
3838

3939

@@ -122,7 +122,8 @@ def __init__(self, delay, callback, *args, **kwargs):
122122
raise ValueError('Given callback is not callable.')
123123

124124
self.delay = delay
125-
self.exec_time = time.time() + delay
125+
self._start_time = time.time()
126+
self.exec_time = self._start_time + delay
126127
self.callback = callback
127128
self.args = args
128129
self.kwargs = kwargs
@@ -153,6 +154,20 @@ def running(self):
153154
"""Return True if the delay running."""
154155
return self in _delay_manager
155156

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+
156171
def _unload_instance(self):
157172
with suppress(ValueError):
158173
self.cancel()
@@ -161,15 +176,15 @@ def _unload_instance(self):
161176
# =============================================================================
162177
# >> REPEAT CLASSES
163178
# =============================================================================
164-
class TickRepeatStatus(IntEnum):
165-
"""Class used to store TickRepeatStatus values."""
179+
class RepeatStatus(IntEnum):
180+
"""Class used to store RepeatStatus values."""
166181

167182
STOPPED = 1
168183
RUNNING = 2
169184
PAUSED = 3
170185

171186

172-
class TickRepeat(AutoUnload):
187+
class Repeat(AutoUnload):
173188
"""Class used to create and call repeats."""
174189

175190
def __init__(self, callback, *args, **kwargs):
@@ -181,41 +196,41 @@ def __init__(self, callback, *args, **kwargs):
181196

182197
# Log the __init__ message
183198
listeners_tick_logger.log_debug(
184-
'TickRepeat.__init__: <{0}> <{1}> <{2}>'.format(
199+
'Repeat.__init__: <{0}> <{1}> <{2}>'.format(
185200
self.callback, self.args, self.kwargs))
186201

187202
# Set up private attributes
188203
self._interval = 0
189204
self._limit = 0
190205
self._count = 0
191206
self._adjusted = 0
192-
self._status = TickRepeatStatus.STOPPED
207+
self._status = RepeatStatus.STOPPED
193208
self._delay = None
194209
self._loop_time = None
195210

196-
def start(self, interval, limit):
211+
def start(self, interval, limit, execute_on_start=False):
197212
"""Start the repeat loop."""
198213
# Log the start message
199214
listeners_tick_logger.log_debug(
200-
'TickRepeat.start: <{0}> <{1}>'.format(interval, limit))
215+
'Repeat.start: <{0}> <{1}>'.format(interval, limit))
201216

202217
# Is the repeat already running?
203-
if self._status is TickRepeatStatus.RUNNING:
218+
if self._status is RepeatStatus.RUNNING:
204219

205220
# Log the status
206221
listeners_tick_logger.log_debug(
207-
'TickRepeat.start - TickRepeatStatus.RUNNING')
222+
'Repeat.start - RepeatStatus.RUNNING')
208223

209224
# Do not start the repeat
210225
return
211226

212227
# Log starting the repeat
213228
listeners_tick_logger.log_debug(
214-
'TickRepeat.start - !TickRepeatStatus' +
215-
'.RUNNING - Starting TickRepeat')
229+
'Repeat.start - !RepeatStatus' +
230+
'.RUNNING - Starting Repeat')
216231

217232
# Set the status to running
218-
self._status = TickRepeatStatus.RUNNING
233+
self._status = RepeatStatus.RUNNING
219234

220235
# Set the given attributes
221236
self._interval = interval
@@ -228,35 +243,39 @@ def start(self, interval, limit):
228243
# Start the delay
229244
self._delay = Delay(self._interval, self._execute)
230245

246+
# Call the callback if set to execute on start
247+
if execute_on_start:
248+
self.callback(*self.args, **self.kwargs)
249+
231250
def stop(self):
232251
"""Stop the repeat loop."""
233252
# Log the stop message
234-
listeners_tick_logger.log_debug('TickRepeat.stop')
253+
listeners_tick_logger.log_debug('Repeat.stop')
235254

236255
# Is the repeat running?
237-
if self._status is not TickRepeatStatus.RUNNING:
256+
if self._status is not RepeatStatus.RUNNING:
238257

239258
# Log the status
240259
listeners_tick_logger.log_debug(
241-
'TickRepeat.stop - !TickRepeatStatus.RUNNING')
260+
'Repeat.stop - !RepeatStatus.RUNNING')
242261

243262
# No need to stop it
244263
return
245264

246265
# Log stopping the repeat
247266
listeners_tick_logger.log_debug(
248-
'TickRepeat.stop - TickRepeatStatus.RUNNING - Stopping TickRepeat')
267+
'Repeat.stop - RepeatStatus.RUNNING - Stopping Repeat')
249268

250269
# Set the status to stopped
251-
self._status = TickRepeatStatus.STOPPED
270+
self._status = RepeatStatus.STOPPED
252271

253272
# Cancel the delay
254273
self._delay.cancel()
255274

256275
def restart(self):
257276
"""Restart the repeat."""
258277
# Log restarting the repeat
259-
listeners_tick_logger.log_debug('TickRepeat.restart')
278+
listeners_tick_logger.log_debug('Repeat.restart')
260279

261280
# Stop the repeat
262281
self.stop()
@@ -270,24 +289,24 @@ def pause(self):
270289
Pausing allows the repeat to be resumed.
271290
"""
272291
# Log the pause message
273-
listeners_tick_logger.log_debug('TickRepeat.pause')
292+
listeners_tick_logger.log_debug('Repeat.pause')
274293

275294
# Is the repeat running?
276-
if self._status is not TickRepeatStatus.RUNNING:
295+
if self._status is not RepeatStatus.RUNNING:
277296

278297
# Log the status
279298
listeners_tick_logger.log_debug(
280-
'TickRepeat.pause - !TickRepeatStatus.RUNNING')
299+
'Repeat.pause - !RepeatStatus.RUNNING')
281300

282301
# No need to pause
283302
return
284303

285304
# Log pausing the repeat
286305
listeners_tick_logger.log_debug(
287-
'TickRepeat.pause - TickRepeatStatus.RUNNING - Pausing TickRepeat')
306+
'Repeat.pause - RepeatStatus.RUNNING - Pausing Repeat')
288307

289308
# Set the status to paused
290-
self._status = TickRepeatStatus.PAUSED
309+
self._status = RepeatStatus.PAUSED
291310

292311
# Set the remaining time in the current loop
293312
self._loop_time = self._delay.exec_time - time.time()
@@ -301,40 +320,40 @@ def resume(self):
301320
Can only resume if in paused status.
302321
"""
303322
# Log the resume message
304-
listeners_tick_logger.log_debug('TickRepeat.resume')
323+
listeners_tick_logger.log_debug('Repeat.resume')
305324

306325
# Is the repeat paused?
307-
if self._status is not TickRepeatStatus.PAUSED:
326+
if self._status is not RepeatStatus.PAUSED:
308327

309328
# Log the status
310329
listeners_tick_logger.log_debug(
311-
'TickRepeat.resume - !TickRepeatStatus.PAUSED')
330+
'Repeat.resume - !RepeatStatus.PAUSED')
312331

313332
# Do not resume
314333
return
315334

316335
# Log resuming the repeat
317336
listeners_tick_logger.log_debug(
318-
'TickRepeat.resume - TickRepeatStatus.' +
319-
'PAUSED - Resuming TickRepeat')
337+
'Repeat.resume - RepeatStatus.' +
338+
'PAUSED - Resuming Repeat')
320339

321340
# Set the status to running
322-
self._status = TickRepeatStatus.RUNNING
341+
self._status = RepeatStatus.RUNNING
323342

324343
# Start the delay
325344
self._delay = Delay(self._loop_time, self._execute)
326345

327346
def extend(self, adjustment):
328347
"""Add to the number of loops to be made."""
329348
# Log the extend message
330-
listeners_tick_logger.log_debug('TickRepeat.extend')
349+
listeners_tick_logger.log_debug('Repeat.extend')
331350

332351
# Is there a limit for this repeat?
333-
if not self.limit:
352+
if not self.loop_limit:
334353

335354
# Log a message about no reducing
336355
listeners_tick_logger.log_debug(
337-
'Unable to extend, TickRepeat instance has no limit.')
356+
'Unable to extend, Repeat instance has no limit.')
338357

339358
# No need to go further
340359
return
@@ -351,14 +370,14 @@ def extend(self, adjustment):
351370
def reduce(self, adjustment):
352371
"""Reduce the number of loops to be made."""
353372
# Log the reduce message
354-
listeners_tick_logger.log_debug('TickRepeat.reduce')
373+
listeners_tick_logger.log_debug('Repeat.reduce')
355374

356375
# Is there a limit for this repeat?
357-
if not self.limit:
376+
if not self.loop_limit:
358377

359378
# Log a message about no reducing
360379
listeners_tick_logger.log_debug(
361-
'Unable to reduce, TickRepeat instance has no limit.')
380+
'Unable to reduce, Repeat instance has no limit.')
362381

363382
# No need to go further
364383
return
@@ -373,41 +392,41 @@ def reduce(self, adjustment):
373392
self._adjusted -= adjustment
374393

375394
# 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):
378397

379398
# Log the reduce-stopping message
380399
listeners_tick_logger.log_debug(
381-
'TickRepeat.reduce - Reduce caused repeat to stop')
400+
'Repeat.reduce - Reduce caused repeat to stop')
382401

383402
# Stop the repeat
384403
self.stop()
385404

386405
def _execute(self):
387406
"""Execute the repeat's callback with its arguments and keywords."""
388407
# Log the _execute message
389-
listeners_tick_logger.log_debug('TickRepeat._execute')
408+
listeners_tick_logger.log_debug('Repeat._execute')
390409

391410
# Add one to the current count
392411
self._count += 1
393412

394413
# Are any more loops to be made?
395-
if self.remaining or not self._limit:
414+
if self.remaining_loops or not self._limit:
396415

397416
# Is there no limit?
398417
if not self._limit:
399418

400419
# Log continuing the loop
401420
listeners_tick_logger.log_debug(
402-
'TickRepeat._execute - No limit')
421+
'Repeat._execute - No limit')
403422

404423
# Is there a limit?
405424
else:
406425

407426
# Log continuing the loop
408427
listeners_tick_logger.log_debug(
409-
'TickRepeat._execute - Remaining - {0}'.format(
410-
self.remaining))
428+
'Repeat._execute - Remaining - {0}'.format(
429+
self.remaining_loops))
411430

412431
# Call the delay again
413432
self._delay = Delay(self._interval, self._execute)
@@ -417,16 +436,16 @@ def _execute(self):
417436

418437
# Log stopping the repeat
419438
listeners_tick_logger.log_debug(
420-
'TickRepeat._execute - Stopping the loop')
439+
'Repeat._execute - Stopping the loop')
421440

422441
# Set the status to stopped
423-
self._status = TickRepeatStatus.STOPPED
442+
self._status = RepeatStatus.STOPPED
424443

425444
# Call the repeat's callback
426445
self.callback(*self.args, **self.kwargs)
427446

428447
@property
429-
def remaining(self):
448+
def remaining_loops(self):
430449
"""Return the remaining number of loops in the repeat."""
431450
# Is there no limit?
432451
if not self._limit:
@@ -435,15 +454,15 @@ def remaining(self):
435454
return self._limit
436455

437456
# Return the remaining number of loops
438-
return self.limit - self._count
457+
return self.loop_limit - self._count
439458

440459
@property
441-
def count(self):
460+
def loop_count(self):
442461
"""Return the current number of loops made in the repeat."""
443462
return self._count
444463

445464
@property
446-
def limit(self):
465+
def loop_limit(self):
447466
"""Return the total number of loops to be made."""
448467
# Is there no limit?
449468
if not self._limit:
@@ -455,25 +474,36 @@ def limit(self):
455474
return self._limit + self._adjusted
456475

457476
@property
458-
def timeleft(self):
477+
def time_left(self):
459478
"""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+
)
461483

462484
@property
463-
def elapsed(self):
485+
def time_elapsed(self):
464486
"""Return the elapsed time since the repeat started."""
465-
return self._count * self._interval
487+
return self.total_time - self.time_left
466488

467489
@property
468490
def total_time(self):
469491
"""Return the total time it will take to complete the repeat."""
470-
return self.limit * self._interval
492+
return self.loop_limit * self._interval
471493

472494
@property
473495
def status(self):
474496
"""Return the status of the repeat."""
475497
return self._status
476498

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+
477507
def _unload_instance(self):
478508
"""Stop the repeat with being unloaded."""
479509
self.stop()

0 commit comments

Comments
 (0)