diff frontends/src/quick_frontend/quick_widgets.py @ 1308:f079e6ed1e69 frontends_multi_profiles

quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget
author Goffi <goffi@goffi.org>
date Fri, 06 Feb 2015 20:43:28 +0100
parents 9512590dc3d7
children d0d5ba3b4d64
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_widgets.py	Fri Feb 06 20:09:01 2015 +0100
+++ b/frontends/src/quick_frontend/quick_widgets.py	Fri Feb 06 20:43:28 2015 +0100
@@ -111,6 +111,7 @@
                 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 @@
                 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 @@
                     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):