comparison frontends/src/quick_frontend/quick_widgets.py @ 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 666b42c957b5
children f3940f6a3222
comparison
equal deleted inserted replaced
2038:3a5badbb443d 2039:6353deb1bd73
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.log import getLogger 20 from sat.core.log import getLogger
21 log = getLogger(__name__) 21 log = getLogger(__name__)
22 from sat.core import exceptions 22 from sat.core import exceptions
23
24 from sat_frontends.quick_frontend.constants import Const as C 23 from sat_frontends.quick_frontend.constants import Const as C
25 24
25
26 NEW_INSTANCE_SUFF = '_new_instance_'
26 classes_map = {} 27 classes_map = {}
27 28
28 29
29 try: 30 try:
30 # FIXME: to be removed when an acceptable solution is here 31 # FIXME: to be removed when an acceptable solution is here
77 cls = class_ 78 cls = class_
78 if cls is None: 79 if cls is None:
79 raise exceptions.InternalError("There is not class registered for {}".format(class_)) 80 raise exceptions.InternalError("There is not class registered for {}".format(class_))
80 return cls 81 return cls
81 82
82 def getWidgets(self, class_, profiles=None): 83 def getRootHash(self, hash_):
84 """Return root hash (i.e. hash without new instance suffix for recreated widgets
85
86 @param hash_(immutable): hash of a widget
87 @return (unicode): root hash (transtyped to unicode)
88 """
89 return unicode(hash_).split(NEW_INSTANCE_SUFF)[0]
90
91 def getWidgets(self, class_, target=None, profiles=None):
83 """Get all subclassed widgets instances 92 """Get all subclassed widgets instances
84 93
85 @param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget] 94 @param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget]
95 @param target: if not None, construct a hash with this target and filter corresponding widgets
96 recreated widgets (with new instance suffix) are handled
86 @param profiles(iterable, None): if not None, filter on instances linked to these profiles 97 @param profiles(iterable, None): if not None, filter on instances linked to these profiles
87 @return: iterator on widgets 98 @return: iterator on widgets
88 """ 99 """
89 class_ = self.getRealClass(class_) 100 class_ = self.getRealClass(class_)
90 try: 101 try:
91 widgets_map = self._widgets[class_.__name__] 102 widgets_map = self._widgets[class_.__name__]
92 except KeyError: 103 except KeyError:
93 return 104 return
94 else: 105 else:
95 for w in widgets_map.itervalues(): 106 if target is not None:
107 filter_hash = unicode(class_.getWidgetHash(target, profiles))
108 else:
109 filter_hash = None
110 for w_hash, w in widgets_map.iteritems():
96 if profiles is None or w.profiles.intersection(profiles): 111 if profiles is None or w.profiles.intersection(profiles):
112 if filter_hash is not None and self.getRootHash(w_hash) != filter_hash:
113 continue
97 yield w 114 yield w
98 115
99 def getWidget(self, class_, target=None, profiles=None): 116 def getWidget(self, class_, target=None, profiles=None):
100 """Get a widget without creating it if it doesn't exist. 117 """Get a widget without creating it if it doesn't exist.
101 118
216 233
217 # XXX: keep up-to-date if new special kwargs are added (i.e.: delete these keys here) 234 # XXX: keep up-to-date if new special kwargs are added (i.e.: delete these keys here)
218 new_kwargs['on_existing_widget'] = C.WIDGET_RAISE 235 new_kwargs['on_existing_widget'] = C.WIDGET_RAISE
219 hash_idx = 1 236 hash_idx = 1
220 while True: 237 while True:
221 new_kwargs['force_hash'] = "{}_new_instance_{}".format(hash_, hash_idx) 238 new_kwargs['force_hash'] = "{}{}{}".format(hash_, NEW_INSTANCE_SUFF, hash_idx)
222 try: 239 try:
223 widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs) 240 widget = self.getOrCreateWidget(class_, target, *args, **new_kwargs)
224 except WidgetAlreadyExistsError: 241 except WidgetAlreadyExistsError:
225 hash_idx += 1 242 hash_idx += 1
226 else: 243 else: