comparison frontends/src/quick_frontend/quick_widgets.py @ 1938:011eff37e21d

quick frontend, primitivus: quickContactList refactored to handle several profiles at once
author Goffi <goffi@goffi.org>
date Mon, 18 Apr 2016 18:31:13 +0200
parents 2daf7b4c6756
children 633b5c21aefd
comparison
equal deleted inserted replaced
1937:14a33c2b1b2a 1938:011eff37e21d
91 except KeyError: 91 except KeyError:
92 return iter([]) 92 return iter([])
93 else: 93 else:
94 return widgets_map.itervalues() 94 return widgets_map.itervalues()
95 95
96 def getWidget(self, class_, target, profile): 96 def getWidget(self, class_, target=None, profiles=None):
97 """Get a widget without creating it if it doesn't exist. 97 """Get a widget without creating it if it doesn't exist.
98 98
99 @param class_(class): class of the widget to create 99 @param class_(class): class of the widget to create
100 @param target: target depending of the widget, usually a JID instance 100 @param target: target depending of the widget, usually a JID instance
101 @param profile (unicode): %(doc_profile)s 101 @param profiles (unicode, iterable[unicode], None): profile(s) to use (may or may not be
102 used, depending of the widget class)
102 @return: a class_ instance or None if the widget doesn't exist 103 @return: a class_ instance or None if the widget doesn't exist
103 """ 104 """
105 assert (target is not None) or (profiles is not None)
106 if profiles is not None and isinstance(profiles, unicode):
107 profiles = [profiles]
104 class_ = self.getRealClass(class_) 108 class_ = self.getRealClass(class_)
105 hash_ = class_.getWidgetHash(target, profile) 109 hash_ = class_.getWidgetHash(target, profiles)
106 try: 110 try:
107 return self._widgets[class_.__name__][hash_] 111 return self._widgets[class_.__name__][hash_]
108 except KeyError: 112 except KeyError:
109 return None 113 return None
110 114
138 _args = [self.host, target] + list(args) or [] # FIXME: check if it's really necessary to use optional args 142 _args = [self.host, target] + list(args) or [] # FIXME: check if it's really necessary to use optional args
139 _kwargs = kwargs or {} 143 _kwargs = kwargs or {}
140 if 'profiles' in _kwargs and 'profile' in _kwargs: 144 if 'profiles' in _kwargs and 'profile' in _kwargs:
141 raise ValueError("You can't have 'profile' and 'profiles' keys at the same time") 145 raise ValueError("You can't have 'profile' and 'profiles' keys at the same time")
142 try: 146 try:
143 _kwargs['profiles'] = _kwargs.pop('profile') 147 _kwargs['profiles'] = [_kwargs.pop('profile')]
144 except KeyError: 148 except KeyError:
145 if not 'profiles' in _kwargs: 149 if not 'profiles' in _kwargs:
146 _kwargs['profiles'] = None 150 _kwargs['profiles'] = None
147 151
148 #on_new_widget tell what to do for the new widget creation 152 #on_new_widget tell what to do for the new widget creation
264 self.addProfile(profiles) 268 self.addProfile(profiles)
265 elif profiles is None: 269 elif profiles is None:
266 if not self.PROFILES_ALLOW_NONE: 270 if not self.PROFILES_ALLOW_NONE:
267 raise ValueError("profiles can't have a value of None") 271 raise ValueError("profiles can't have a value of None")
268 else: 272 else:
269 if not self.PROFILES_MULTIPLE: 273 if not self.PROFILES_MULTIPLE and len(profiles) != 1:
270 raise ValueError("multiple profiles are not allowed") 274 raise ValueError("multiple profiles are not allowed")
271 for profile in profiles: 275 for profile in profiles:
272 self.addProfile(profile) 276 self.addProfile(profile)
273 277
274 @property 278 @property