quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
This commit is contained in:
parent
93c860a443
commit
8a7029b549
1 changed files with 21 additions and 4 deletions
|
|
@ -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 @@ class QuickWidgetsManager(object):
|
|||
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 @@ class QuickWidgetsManager(object):
|
|||
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 @@ class QuickWidgetsManager(object):
|
|||
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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue