diff options
author | mathieui <mathieui@mathieui.net> | 2012-08-01 01:06:57 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2012-08-01 01:06:57 +0200 |
commit | 71f813af289ff4df6e4721f56c907d78aa609e69 (patch) | |
tree | 77034550713e6da64516c3bf691ef0626e9abbea /src/roster.py | |
parent | 1625a4f41fa6727e41b0546687d72a3101b2b2bf (diff) | |
download | poezio-71f813af289ff4df6e4721f56c907d78aa609e69.tar.gz poezio-71f813af289ff4df6e4721f56c907d78aa609e69.tar.bz2 poezio-71f813af289ff4df6e4721f56c907d78aa609e69.tar.xz poezio-71f813af289ff4df6e4721f56c907d78aa609e69.zip |
Add a roster_group_sort option, which works like roster_sort
- defaults to "name" (sort by group name)
- document it
- also, micro-optimize get_nb_connected_contacts()
Diffstat (limited to 'src/roster.py')
-rw-r--r-- | src/roster.py | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/src/roster.py b/src/roster.py index 84771678..8d59e345 100644 --- a/src/roster.py +++ b/src/roster.py @@ -18,6 +18,30 @@ from contact import Contact from sleekxmpp.xmlstream.stanzabase import JID from sleekxmpp.exceptions import IqError + +def sort_group_name(group): + return group.name.lower() + +def sort_group_folded(group): + return group.folded + +def sort_group_connected(group): + return - group.get_nb_connected_contacts() + +def sort_group_size(group): + return - len(group) + +def sort_group_none(group): + return 0 if group.name != 'none' else 1 + +GROUP_SORTING_METHODS = { + 'name': sort_group_name, + 'fold': sort_group_folded, + 'connected': sort_group_connected, + 'size': sort_group_size, + 'none': sort_group_none, +} + class Roster(object): """ The proxy class to get the roster from SleekXMPP. @@ -93,9 +117,17 @@ class Roster(object): """Set the SleekXMPP RosterSingle for our roster""" self.__node = value - def get_groups(self): + def get_groups(self, sort=''): """Return a list of the RosterGroups""" - return [group for group in self.groups.values() if group] + group_list = sorted(filter(lambda x: bool(x), self.groups.values()), key=lambda x: x.name.lower()) + + for sorting in sort.split('_'): + if sorting == 'reverse': + group_list = list(reversed(group_list)) + else: + method = GROUP_SORTING_METHODS.get(sorting, lambda x: 0) + group_list = sorted(group_list, key=method) + return group_list def get_group(self, name): """Return a group or create it if not present""" @@ -273,16 +305,16 @@ class RosterGroup(object): """Return the group contacts, filtered and sorted""" contact_list = self.contacts.copy() if not contact_filter\ else [contact for contact in self.contacts.copy() if contact_filter[0](contact, contact_filter[1])] + contact_list = sorted(contact_list, key=SORTING_METHODS['name']) for sorting in sort.split('_'): - method = SORTING_METHODS.get(sorting, lambda x: 0) if sorting == 'reverse': contact_list = list(reversed(contact_list)) else: + method = SORTING_METHODS.get(sorting, lambda x: 0) contact_list = sorted(contact_list, key=method) return contact_list - def toggle_folded(self): """Fold/unfold the group in the roster""" self.folded = not self.folded @@ -295,7 +327,7 @@ class RosterGroup(object): def get_nb_connected_contacts(self): """Return the number of connected contacts""" - return len([1 for contact in self.contacts if contact.resources]) + return len([1 for contact in self.contacts if len(contact)]) # Shared roster object |