comparison sat_frontends/quick_frontend/quick_widgets.py @ 3115:3018d153a615

quick frontend (widgets): some more errors handling in getOrCreateWidget
author Goffi <goffi@goffi.org>
date Fri, 17 Jan 2020 18:47:02 +0100
parents 060ac43627fc
children 328a6cea3802
comparison
equal deleted inserted replaced
3114:060ac43627fc 3115:3018d153a615
253 elif callable(on_existing_widget): 253 elif callable(on_existing_widget):
254 widget = on_existing_widget(widget) 254 widget = on_existing_widget(widget)
255 if widget is None: 255 if widget is None:
256 raise exceptions.InternalError( 256 raise exceptions.InternalError(
257 "on_existing_widget method must return the widget to use") 257 "on_existing_widget method must return the widget to use")
258 if widget not in widgets_map[hash_]:
259 log.debug(
260 "the widget returned by on_existing_widget is new, adding it")
261 widgets_map[hash_].append(widget)
258 else: 262 else:
259 raise exceptions.InternalError( 263 raise exceptions.InternalError(
260 "Unexpected on_existing_widget value ({})".format(on_existing_widget)) 264 "Unexpected on_existing_widget value ({})".format(on_existing_widget))
261 265
262 return widget 266 return widget
297 except KeyError: 301 except KeyError:
298 log.error("no widgets_map found for class {cls}".format(cls=class_)) 302 log.error("no widgets_map found for class {cls}".format(cls=class_))
299 return 303 return
300 widget_hash = str(class_.getWidgetHash(widget_to_delete.target, 304 widget_hash = str(class_.getWidgetHash(widget_to_delete.target,
301 widget_to_delete.profiles)) 305 widget_to_delete.profiles))
302 widget_instances = widgets_map[widget_hash] 306 try:
307 widget_instances = widgets_map[widget_hash]
308 except KeyError:
309 log.error(f"no instance of {class_.__name__} found with hash {widget_hash!r}")
310 return
303 if all_instances: 311 if all_instances:
304 widget_instances.clear() 312 widget_instances.clear()
305 else: 313 else:
306 try: 314 try:
307 widget_instances.remove(widget_to_delete) 315 widget_instances.remove(widget_to_delete)
313 321
314 if not widget_instances: 322 if not widget_instances:
315 # all instances with this hash have been deleted 323 # all instances with this hash have been deleted
316 # we remove the hash itself 324 # we remove the hash itself
317 del widgets_map[widget_hash] 325 del widgets_map[widget_hash]
318 log.debug("All instances of {cls} with hash {widget_hash} have been deleted" 326 log.debug("All instances of {cls} with hash {widget_hash!r} have been deleted"
319 .format(cls=class_, widget_hash=widget_hash)) 327 .format(cls=class_, widget_hash=widget_hash))
320 328
321 329
322 class QuickWidget(object): 330 class QuickWidget(object):
323 """generic widget base""" 331 """generic widget base"""