changeset 2039:6353deb1bd73

quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
author Goffi <goffi@goffi.org>
date Sun, 21 Aug 2016 17:03:18 +0200
parents 3a5badbb443d
children f607349a01a4
files frontends/src/quick_frontend/quick_widgets.py
diffstat 1 files changed, 21 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_widgets.py	Sun Aug 21 12:27:19 2016 +0200
+++ b/frontends/src/quick_frontend/quick_widgets.py	Sun Aug 21 17:03:18 2016 +0200
@@ -20,9 +20,10 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.core import exceptions
-
 from sat_frontends.quick_frontend.constants import Const as C
 
+
+NEW_INSTANCE_SUFF = '_new_instance_'
 classes_map = {}
 
 
@@ -79,10 +80,20 @@
             raise exceptions.InternalError("There is not class registered for {}".format(class_))
         return cls
 
-    def getWidgets(self, class_, profiles=None):
+    def getRootHash(self, hash_):
+        """Return root hash (i.e. hash without new instance suffix for recreated widgets
+
+        @param hash_(immutable): hash of a widget
+        @return (unicode): root hash (transtyped to unicode)
+        """
+        return unicode(hash_).split(NEW_INSTANCE_SUFF)[0]
+
+    def getWidgets(self, class_, target=None, profiles=None):
         """Get all subclassed widgets instances
 
         @param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget]
+        @param target: if not None, construct a hash with this target and filter corresponding widgets
+            recreated widgets (with new instance suffix) are handled
         @param profiles(iterable, None): if not None, filter on instances linked to these profiles
         @return: iterator on widgets
         """
@@ -92,8 +103,14 @@
         except KeyError:
             return
         else:
-            for w in widgets_map.itervalues():
+            if target is not None:
+                filter_hash = unicode(class_.getWidgetHash(target, profiles))
+            else:
+                filter_hash = None
+            for w_hash, w in widgets_map.iteritems():
                 if profiles is None or w.profiles.intersection(profiles):
+                    if filter_hash is not None and self.getRootHash(w_hash) != filter_hash:
+                        continue
                     yield w
 
     def getWidget(self, class_, target=None, profiles=None):
@@ -218,7 +235,7 @@
                 new_kwargs['on_existing_widget'] = C.WIDGET_RAISE
                 hash_idx = 1
                 while True:
-                    new_kwargs['force_hash'] = "{}_new_instance_{}".format(hash_, hash_idx)
+                    new_kwargs['force_hash'] = "{}{}{}".format(hash_, NEW_INSTANCE_SUFF, hash_idx)
                     try:
                         widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs)
                     except WidgetAlreadyExistsError: