def _tick(self): """Called every tick when the listener is registered.""" # Get the current time current_time = time.time() # Loop through each item in the ordered list for item in self: # Should the delays be called? if item > current_time: # If not, no need to continue looping break # Remove the item from the dictionary del self[item] # Is the dictionary now empty? if not self: # Log the tick listener unregistering message listeners_tick_delays_logger.log_debug( 'tick_delays._tick - Unregistering Tick Listener') # Unregister the tick listener tick_listener_manager.unregister_listener(self._tick)
def cancel_delay(self, delay_object): """Cancel a delay.""" # Log the canceling message listeners_tick_delays_logger.log_debug( 'tick_delays.cancel_delay <{0}>'.format(delay_object)) # Is the given argument a Delay object? if not isinstance(delay_object, Delay): # If not, raise an error raise TypeError( 'tick_delays.cancel_delay requires a Delay instance.') # Is the given Delay object's time no longer in the dictionary? if delay_object._exec_time not in self: # If not, raise an error raise KeyError('Object is no longer registered.') # Log the removing from list message listeners_tick_delays_logger.log_debug( 'tick_delays.cancel_delay - Removing from ' '<{0}>'.format(delay_object._exec_time)) # Remove the delay from its time self[delay_object._exec_time].remove(delay_object) # Does the delay's time have any remaining objects? if not self[delay_object._exec_time]: # Log the deletion of the time from the dictionary message listeners_tick_delays_logger.log_debug( 'tick_delays.cancel_delay - Removing <{0}> ' 'from dictionary'.format(delay_object._exec_time)) # If not, remove the delay's time from the dictionary del self[delay_object._exec_time] # Are there any remaining delays? if not self: # Log the tick listener unregistering message listeners_tick_delays_logger.log_debug( 'tick_delays.cancel_delay - Unregistering Tick Listener') # Unregister the listener tick_listener_manager.unregister_listener(self._tick)