def collect(self, *roots): ''' For contacts which are in metacontacts, remove them from the original protocol groups and add them to a new group. Returns that new group full of DGroups holding MetaContacts. ''' # Remove meta contacts mc_root = DGroup('Root', protocols=[], ids=[]) b2m = self.buddies_to_metas # = = defaultdict(set) #map buddy description to set of metas groupnames = oset() # For each protocol root group cs = defaultdict(list) mc_gnames = self.groupnames metacontact_objs = self.metacontact_objs def maybe_remove_contact(contact, group): if (contact.name.lower(), contact.service) in b2m: for meta in b2m[(contact.name.lower(), contact.service)]: cs[meta.id].append(contact) group.remove(contact) return True return False from contacts.Group import GroupTypes for root in roots: # Find the corresponding group for group in list(root): gflag = False if group is root: continue if isinstance(group, GroupTypes): for elem in list(group): gflag |= maybe_remove_contact(elem, group) if gflag and (group.name not in groupnames): groupnames.add(group.name) else: # contact elem = group if maybe_remove_contact(elem, root): groupnames.add(get_fakeroot_name()) assert not set(cs.keys()) - set(self.keys()) for id in self.iterkeys(): elems = cs[id] order = [b.tag for b in self[id].buddies] elems = list( sorted(elems, key=lambda elem: order.index( (elem.name.lower(), elem.service)))) out = [] hidden = [] for tag in order: online = False while elems and (elems[0].name.lower(), elems[0].service) == tag: b = elems.pop(0) if not online: out.append(b) online = True else: hidden.append(b) if not online: old = [ o for o in metacontact_objs[id] if (isinstance(o, OfflineBuddy) and (o.name.lower(), o.service) == tag) ] if old: out.append(old[0]) else: out.append(OfflineBuddy(*tag)) metacontact_objs[id].set_new(out, hidden) groups = {} for m in metacontact_objs.itervalues(): if any(not isinstance(b, OfflineBuddy) for b in m): for gname in self[m.id].groups: try: g = groups[gname[0]] except KeyError: groups[gname[0]] = g = DGroup(gname[0]) g.append(m) glen = len(groups) nextroot = DGroup('Root') for gname in groupnames: if gname in groups: nextroot.append(groups.pop(gname)) for gname in set(g[0] for g in mc_gnames) - set(groupnames): if gname in groups: nextroot.append(groups.pop(gname)) mc_root.extend(nextroot) # assert len(nextroot) == glen return mc_root
def collect(self, *roots): ''' For contacts which are in metacontacts, remove them from the original protocol groups and add them to a new group. Returns that new group full of DGroups holding MetaContacts. ''' # Remove meta contacts mc_root = DGroup('Root', protocols = [], ids = []) b2m = self.buddies_to_metas# = = defaultdict(set) #map buddy description to set of metas groupnames = oset() # For each protocol root group cs = defaultdict(list) mc_gnames = self.groupnames metacontact_objs = self.metacontact_objs def maybe_remove_contact(contact, group): if (contact.name.lower(), contact.service) in b2m: for meta in b2m[(contact.name.lower(), contact.service)]: cs[meta.id].append(contact) group.remove(contact) return True return False from contacts.Group import GroupTypes for root in roots: # Find the corresponding group for group in list(root): gflag = False if group is root: continue if isinstance(group, GroupTypes): for elem in list(group): gflag |= maybe_remove_contact(elem, group) if gflag and (group.name not in groupnames): groupnames.add(group.name) else: # contact elem = group if maybe_remove_contact(elem, root): groupnames.add(get_fakeroot_name()) assert not set(cs.keys()) - set(self.keys()) for id in self.iterkeys(): elems = cs[id] order = [b.tag for b in self[id].buddies] elems = list(sorted(elems, key = lambda elem: order.index((elem.name.lower(), elem.service)))) out = [] hidden = [] for tag in order: online = False while elems and (elems[0].name.lower(), elems[0].service) == tag: b = elems.pop(0) if not online: out.append(b) online = True else: hidden.append(b) if not online: old = [o for o in metacontact_objs[id] if (isinstance(o, OfflineBuddy) and (o.name.lower(), o.service) == tag)] if old: out.append(old[0]) else: out.append(OfflineBuddy(*tag)) metacontact_objs[id].set_new(out, hidden) groups = {} for m in metacontact_objs.itervalues(): if any(not isinstance(b, OfflineBuddy) for b in m): for gname in self[m.id].groups: try: g = groups[gname[0]] except KeyError: groups[gname[0]] = g = DGroup(gname[0]) g.append(m) glen = len(groups) nextroot = DGroup('Root') for gname in groupnames: if gname in groups: nextroot.append(groups.pop(gname)) for gname in set(g[0] for g in mc_gnames) - set(groupnames): if gname in groups: nextroot.append(groups.pop(gname)) mc_root.extend(nextroot) # assert len(nextroot) == glen return mc_root