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
|
from sat.core.log import getLogger
|
||||||
log = getLogger(__name__)
|
log = getLogger(__name__)
|
||||||
from sat.core import exceptions
|
from sat.core import exceptions
|
||||||
|
|
||||||
from sat_frontends.quick_frontend.constants import Const as C
|
from sat_frontends.quick_frontend.constants import Const as C
|
||||||
|
|
||||||
|
|
||||||
|
NEW_INSTANCE_SUFF = '_new_instance_'
|
||||||
classes_map = {}
|
classes_map = {}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -79,10 +80,20 @@ class QuickWidgetsManager(object):
|
||||||
raise exceptions.InternalError("There is not class registered for {}".format(class_))
|
raise exceptions.InternalError("There is not class registered for {}".format(class_))
|
||||||
return cls
|
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
|
"""Get all subclassed widgets instances
|
||||||
|
|
||||||
@param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget]
|
@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
|
@param profiles(iterable, None): if not None, filter on instances linked to these profiles
|
||||||
@return: iterator on widgets
|
@return: iterator on widgets
|
||||||
"""
|
"""
|
||||||
|
|
@ -92,8 +103,14 @@ class QuickWidgetsManager(object):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
else:
|
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 profiles is None or w.profiles.intersection(profiles):
|
||||||
|
if filter_hash is not None and self.getRootHash(w_hash) != filter_hash:
|
||||||
|
continue
|
||||||
yield w
|
yield w
|
||||||
|
|
||||||
def getWidget(self, class_, target=None, profiles=None):
|
def getWidget(self, class_, target=None, profiles=None):
|
||||||
|
|
@ -218,7 +235,7 @@ class QuickWidgetsManager(object):
|
||||||
new_kwargs['on_existing_widget'] = C.WIDGET_RAISE
|
new_kwargs['on_existing_widget'] = C.WIDGET_RAISE
|
||||||
hash_idx = 1
|
hash_idx = 1
|
||||||
while True:
|
while True:
|
||||||
new_kwargs['force_hash'] = "{}_new_instance_{}".format(hash_, hash_idx)
|
new_kwargs['force_hash'] = "{}{}{}".format(hash_, NEW_INSTANCE_SUFF, hash_idx)
|
||||||
try:
|
try:
|
||||||
widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs)
|
widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs)
|
||||||
except WidgetAlreadyExistsError:
|
except WidgetAlreadyExistsError:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue