comparison sat_frontends/quick_frontend/quick_app.py @ 3167:d0fb79f97466

quick frontends (app): added an "ignore_missing" argument to RemoveListener: normally an error is logged when removeListener is call on a inexisting listener, but sometimes listeners are added only in specific scenarios, and it may be handy to try to delete them without having to explictly check that they have been used.
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 19:44:05 +0100
parents 122075ceaa53
children 1cb232c9e845
comparison
equal deleted inserted replaced
3166:122075ceaa53 3167:d0fb79f97466
579 listener will be callable only by one of the given profiles. 579 listener will be callable only by one of the given profiles.
580 """ 580 """
581 assert type_ in C.LISTENERS 581 assert type_ in C.LISTENERS
582 self._listeners.setdefault(type_, {})[callback] = profiles_filter 582 self._listeners.setdefault(type_, {})[callback] = profiles_filter
583 583
584 def removeListener(self, type_, callback): 584 def removeListener(self, type_, callback, ignore_missing=False):
585 """Remove a callback from listeners 585 """Remove a callback from listeners
586 586
587 @param type_: same as for [addListener] 587 @param type_(str): same as for [addListener]
588 @param callback: callback to remove 588 @param callback(callable): callback to remove
589 @param ignore_missing(bool): if True, don't log error if the listener doesn't
590 exist
589 """ 591 """
590 assert type_ in C.LISTENERS 592 assert type_ in C.LISTENERS
591 try: 593 try:
592 self._listeners[type_].pop(callback) 594 self._listeners[type_].pop(callback)
593 except KeyError: 595 except KeyError:
594 log.error( 596 if not ignore_missing:
595 f"Trying to remove an inexisting listener (type = {type_}): {callback}") 597 log.error(
598 f"Trying to remove an inexisting listener (type = {type_}): "
599 f"{callback}")
596 600
597 def callListeners(self, type_, *args, **kwargs): 601 def callListeners(self, type_, *args, **kwargs):
598 """Call the methods which listen type_ event. If a profiles filter has 602 """Call the methods which listen type_ event. If a profiles filter has
599 been register with a listener and profile argument is not None, the 603 been register with a listener and profile argument is not None, the
600 listener will be called only if profile is in the profiles filter list. 604 listener will be called only if profile is in the profiles filter list.