# HG changeset patch # User Goffi # Date 1471791798 -7200 # Node ID 6353deb1bd73c75d2142ab2f1ecf930703e4e291 # Parent 3a5badbb443dd4c437feb753bcc2b51b987bba29 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too diff -r 3a5badbb443d -r 6353deb1bd73 frontends/src/quick_frontend/quick_widgets.py --- 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: