diff --git a/offlineimap.conf b/offlineimap.conf index 2656ef6..b4f6a99 100644 --- a/offlineimap.conf +++ b/offlineimap.conf @@ -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) diff --git a/offlineimap/repository/Base.py b/offlineimap/repository/Base.py index cbbc5f4..cca5cf3 100644 --- a/offlineimap/repository/Base.py +++ b/offlineimap/repository/Base.py @@ -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}) diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py index f008592..1b13d79 100644 --- a/offlineimap/repository/IMAP.py +++ b/offlineimap/repository/IMAP.py @@ -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