comparison 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
comparison
equal deleted inserted replaced
1307:9512590dc3d7 1308:f079e6ed1e69
109 None: do nothing 109 None: do nothing
110 if 'on_existing_widget' is present it can have the following values: 110 if 'on_existing_widget' is present it can have the following values:
111 C.WIDGET_KEEP [default]: return the existing widget 111 C.WIDGET_KEEP [default]: return the existing widget
112 C.WIDGET_RAISE: raise WidgetAlreadyExistsError 112 C.WIDGET_RAISE: raise WidgetAlreadyExistsError
113 C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH* 113 C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH*
114 [callable]: this method will be called with existing widget as argument
114 if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash 115 if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash
115 other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass, 116 other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass,
116 it will be used to create a new QuickChat instance). 117 it will be used to create a new QuickChat instance).
117 @return: a class_ instance, either new or already existing 118 @return: a class_ instance, either new or already existing
118 """ 119 """
171 on_new_widget(widget) 172 on_new_widget(widget)
172 else: 173 else:
173 assert on_new_widget is None 174 assert on_new_widget is None
174 else: 175 else:
175 # the widget already exists 176 # the widget already exists
177 if on_existing_widget == C.WIDGET_KEEP:
178 pass
176 if on_existing_widget == C.WIDGET_RAISE: 179 if on_existing_widget == C.WIDGET_RAISE:
177 raise WidgetAlreadyExistsError(hash_) 180 raise WidgetAlreadyExistsError(hash_)
178 elif on_existing_widget == C.WIDGET_RECREATE: 181 elif on_existing_widget == C.WIDGET_RECREATE:
179 # we use getOrCreateWidget to recreate the new widget 182 # we use getOrCreateWidget to recreate the new widget
180 # /!\ we use args and kwargs and not _args and _kwargs because we need the original args 183 # /!\ we use args and kwargs and not _args and _kwargs because we need the original args
198 except WidgetAlreadyExistsError: 201 except WidgetAlreadyExistsError:
199 hash_idx += 1 202 hash_idx += 1
200 else: 203 else:
201 log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash'])) 204 log.debug(u"Widget already exists, a new one has been recreated with hash {}".format(new_kwargs['force_hash']))
202 break 205 break
206 elif callable(on_existing_widget):
207 on_existing_widget(widget)
208 else:
209 raise exceptions.InternalError("Unexpected on_existing_widget value ({})".format(on_existing_widget))
210
203 return widget 211 return widget
204 212
205 def deleteWidget(self, widget_to_delete): 213 def deleteWidget(self, widget_to_delete):
206 """Delete a widget 214 """Delete a widget
207 215