def _get_callable_ref(listener, callback=None): """Returns a callable reference for the given listener""" meth = getattr(listener, 'event_received', None) if meth is not None and is_callable(meth): return weakref.ref(listener, callback) else: return CallableRef(listener, callback)
def _fire_event(self, event_type, event_value, listeners=None): """Sends an event to all listeners or a specific one""" if listeners is None: listeners = self._listeners if listeners is None: return if not isinstance(listeners, collections.Sequence): listeners = listeners, for listener in listeners: if isinstance(listener, weakref.ref) or \ isinstance(listener, BoundMethodWeakref): real_listener = listener() else: real_listener = listener if real_listener is None: continue meth = getattr(real_listener, 'event_received', None) if meth is not None and is_callable(meth): real_listener.event_received(self, event_type, event_value) elif is_callable(real_listener): real_listener(self, event_type, event_value)