quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget

This commit is contained in:
Goffi 2015-02-06 20:43:28 +01:00
commit 95faddd84c

View file

@ -111,6 +111,7 @@ class QuickWidgetsManager(object):
C.WIDGET_KEEP [default]: return the existing widget
C.WIDGET_RAISE: raise WidgetAlreadyExistsError
C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH*
[callable]: this method will be called with existing widget as argument
if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash
other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass,
it will be used to create a new QuickChat instance).
@ -173,6 +174,8 @@ class QuickWidgetsManager(object):
assert on_new_widget is None
else:
# the widget already exists
if on_existing_widget == C.WIDGET_KEEP:
pass
if on_existing_widget == C.WIDGET_RAISE:
raise WidgetAlreadyExistsError(hash_)
elif on_existing_widget == C.WIDGET_RECREATE:
@ -200,6 +203,11 @@ class QuickWidgetsManager(object):
else:
log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash']))
break
elif callable(on_existing_widget):
on_existing_widget(widget)
else:
raise exceptions.InternalError("Unexpected on_existing_widget value ({})".format(on_existing_widget))
return widget
def deleteWidget(self, widget_to_delete):