Implement foldersort in a python3 compatible way

By default we sort folders alphabetically (for IMAP) according to their
transposed names. For python3, we need to bend a bit backwards to still
allow the use of a cmp() function for foldersort. While going through, I
discovered that we never sort folders for Maildir.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-02-06 17:33:50 +01:00
parent 5c598d7e74
commit 19c014c6cd
3 changed files with 26 additions and 9 deletions

View File

@ -496,13 +496,14 @@ remoteuser = username
# one. For example:
# folderincludes = ['debian.user', 'debian.personal']
# You can specify foldersort to determine how folders are sorted.
# You can specify 'foldersort' to determine how folders are sorted.
# This affects order of synchronization and mbnames. The expression
# should return -1, 0, or 1, as the default Python cmp() does. The
# two arguments, x and y, are strings representing the names of the folders
# to be sorted. The sorting is applied *AFTER* nametrans, if any.
#
# To reverse the sort:
# should return -1, 0, or 1, as the default Python cmp() does. The two
# arguments, x and y, are strings representing the names of the folders
# to be sorted. The sorting is applied *AFTER* nametrans, if any. The
# default is to sort IMAP folders alphabetically
# (case-insensitive). Usually, you should never have to modify this. To
# eg. reverse the sort:
#
# foldersort = lambda x, y: -cmp(x, y)

View File

@ -45,7 +45,7 @@ class BaseRepository(CustomConfig.ConfigHelperMixin, object):
self.nametrans = lambda foldername: foldername
self.folderfilter = lambda foldername: 1
self.folderincludes = []
self.foldersort = cmp
self.foldersort = None
if self.config.has_option(self.getsection(), 'nametrans'):
self.nametrans = self.localeval.eval(
self.getconf('nametrans'), {'re': re})

View File

@ -308,8 +308,24 @@ class IMAPRepository(BaseRepository):
self))
finally:
self.imapserver.releaseconnection(imapobj)
retval.sort(lambda x, y: self.foldersort(x.getvisiblename(), y.getvisiblename()))
if self.foldersort is None:
# default sorting by case insensitive transposed name
retval.sort(key=lambda x: str.lower(x.getvisiblename()))
else:
# do foldersort in a python3-compatible way
# http://bytes.com/topic/python/answers/844614-python-3-sorting-comparison-function
def cmp2key(mycmp):
"""Converts a cmp= function into a key= function
We need to keep cmp functions for backward compatibility"""
class K:
def __init__(self, obj, *args):
self.obj = obj
def __cmp__(self, other):
return mycmp(self.obj, other.obj)
return K
retval.sort(key=cmp2key(self.foldersort))
self.folders = retval
return self.folders