comparison libervia/frontends/quick_frontend/quick_widgets.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 26b7ed2817da
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
53 pass 53 pass
54 54
55 55
56 class QuickWidgetsManager(object): 56 class QuickWidgetsManager(object):
57 """This class is used to manage all the widgets of a frontend 57 """This class is used to manage all the widgets of a frontend
58 A widget can be a window, a graphical thing, or someting else depending of the frontend""" 58 A widget can be a window, a graphical thing, or someting else depending of the frontend
59 """
59 60
60 def __init__(self, host): 61 def __init__(self, host):
61 self.host = host 62 self.host = host
62 self._widgets = {} 63 self._widgets = {}
63 64
255 pass 256 pass
256 else: 257 else:
257 recreate_args(_args, _kwargs) 258 recreate_args(_args, _kwargs)
258 widget = cls(*_args, **_kwargs) 259 widget = cls(*_args, **_kwargs)
259 widgets_map[hash_].append(widget) 260 widgets_map[hash_].append(widget)
260 log.debug("widget <{wid}> already exists, a new one has been recreated" 261 log.debug(
261 .format(wid=widget)) 262 "widget <{wid}> already exists, a new one has been recreated".format(
263 wid=widget
264 )
265 )
262 elif callable(on_existing_widget): 266 elif callable(on_existing_widget):
263 widget = on_existing_widget(widget) 267 widget = on_existing_widget(widget)
264 if widget is None: 268 if widget is None:
265 raise exceptions.InternalError( 269 raise exceptions.InternalError(
266 "on_existing_widget method must return the widget to use") 270 "on_existing_widget method must return the widget to use"
271 )
267 if widget not in widgets_map[hash_]: 272 if widget not in widgets_map[hash_]:
268 log.debug( 273 log.debug(
269 "the widget returned by on_existing_widget is new, adding it") 274 "the widget returned by on_existing_widget is new, adding it"
275 )
270 widgets_map[hash_].append(widget) 276 widgets_map[hash_].append(widget)
271 else: 277 else:
272 raise exceptions.InternalError( 278 raise exceptions.InternalError(
273 "Unexpected on_existing_widget value ({})".format(on_existing_widget)) 279 "Unexpected on_existing_widget value ({})".format(on_existing_widget)
280 )
274 281
275 return widget 282 return widget
276 283
277 def delete_widget(self, widget_to_delete, *args, **kwargs): 284 def delete_widget(self, widget_to_delete, *args, **kwargs):
278 """Delete a widget instance 285 """Delete a widget instance
294 the user or a leave signal, "all_instances" is usually set at 301 the user or a leave signal, "all_instances" is usually set at
295 the same time. 302 the same time.
296 """ 303 """
297 # TODO: all_instances must be independante kwargs, this is not possible with Python 2 304 # TODO: all_instances must be independante kwargs, this is not possible with Python 2
298 # but will be with Python 3 305 # but will be with Python 3
299 all_instances = kwargs.get('all_instances', False) 306 all_instances = kwargs.get("all_instances", False)
300 307
301 if all_instances: 308 if all_instances:
302 for w in self.get_widget_instances(widget_to_delete): 309 for w in self.get_widget_instances(widget_to_delete):
303 if w.on_delete(**kwargs) == False: 310 if w.on_delete(**kwargs) == False:
304 log.debug( 311 log.debug(
305 f"Deletion of {widget_to_delete} cancelled by widget itself") 312 f"Deletion of {widget_to_delete} cancelled by widget itself"
313 )
306 return 314 return
307 else: 315 else:
308 if widget_to_delete.on_delete(**kwargs) == False: 316 if widget_to_delete.on_delete(**kwargs) == False:
309 log.debug(f"Deletion of {widget_to_delete} cancelled by widget itself") 317 log.debug(f"Deletion of {widget_to_delete} cancelled by widget itself")
310 return 318 return
316 try: 324 try:
317 widgets_map = self._widgets[class_.__name__] 325 widgets_map = self._widgets[class_.__name__]
318 except KeyError: 326 except KeyError:
319 log.error("no widgets_map found for class {cls}".format(cls=class_)) 327 log.error("no widgets_map found for class {cls}".format(cls=class_))
320 return 328 return
321 widget_hash = str(class_.get_widget_hash(widget_to_delete.target, 329 widget_hash = str(
322 widget_to_delete.profiles)) 330 class_.get_widget_hash(widget_to_delete.target, widget_to_delete.profiles)
331 )
323 try: 332 try:
324 widget_instances = widgets_map[widget_hash] 333 widget_instances = widgets_map[widget_hash]
325 except KeyError: 334 except KeyError:
326 log.error(f"no instance of {class_.__name__} found with hash {widget_hash!r}") 335 log.error(f"no instance of {class_.__name__} found with hash {widget_hash!r}")
327 return 336 return
338 347
339 if not widget_instances: 348 if not widget_instances:
340 # all instances with this hash have been deleted 349 # all instances with this hash have been deleted
341 # we remove the hash itself 350 # we remove the hash itself
342 del widgets_map[widget_hash] 351 del widgets_map[widget_hash]
343 log.debug("All instances of {cls} with hash {widget_hash!r} have been deleted" 352 log.debug(
344 .format(cls=class_, widget_hash=widget_hash)) 353 "All instances of {cls} with hash {widget_hash!r} have been deleted".format(
354 cls=class_, widget_hash=widget_hash
355 )
356 )
345 self.host.call_listeners("widgetDeleted", widget_to_delete) 357 self.host.call_listeners("widgetDeleted", widget_to_delete)
346 358
347 359
348 class QuickWidget(object): 360 class QuickWidget(object):
349 """generic widget base""" 361 """generic widget base"""
362
350 # FIXME: sometime a single target is used, sometimes several ones 363 # FIXME: sometime a single target is used, sometimes several ones
351 # This should be sorted out in the same way as for profiles: a single 364 # This should be sorted out in the same way as for profiles: a single
352 # target should be possible when appropriate attribute is set. 365 # target should be possible when appropriate attribute is set.
353 # methods using target(s) and hash should be fixed accordingly 366 # methods using target(s) and hash should be fixed accordingly
354 367