Allow custom sorting of mailboxes for mbnames

mutt-sidebar and, probably, other MUA show mailboxes in the order
they are listed in the file written by mbnames.  Therefore, to allow
customization of the order with which mailboxes are listed, introduce
the new 'sort_keyfunc' directive in the [mbnames] section.

'sort_keyfunc' must be a function that will be called once for each
mailbox.  It must accept the only argument -- a dict with 2 items,
'accountname' and 'foldername', and should return an object that
will be used as the sorting key for each mailbox.

Default key function returns (d['accountname'], d['foldername']),
thus sorting by account name and then by the folder name.

Signed-off-by: Johan Herland <johan@herland.net>
Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
This commit is contained in:
Johan Herland 2013-05-19 12:13:57 +02:00 committed by Eygene Ryabinkin
parent 0d992ee7d3
commit 56b0c5dbac
3 changed files with 24 additions and 2 deletions

View File

@ -14,6 +14,8 @@ WIP (add new stuff for the next release)
* SIGHUP is now handled as the termination notification rather than * SIGHUP is now handled as the termination notification rather than
the signal to reread the configuration (Dmitrijs Ledkovs) the signal to reread the configuration (Dmitrijs Ledkovs)
* Honor the timezone of emails (Tobias Thierer) * Honor the timezone of emails (Tobias Thierer)
* Allow mbnames output to be sorted by a custom sort key by specifying
a 'sort_keyfunc' function in the [mbnames] section of the config.
OfflineIMAP v6.5.5-rc1 (2012-09-05) OfflineIMAP v6.5.5-rc1 (2012-09-05)
=================================== ===================================

View File

@ -145,6 +145,20 @@ footer = "\n"
# #
# Note that this filter can be used only to further restrict mbnames # Note that this filter can be used only to further restrict mbnames
# to a subset of folders that pass the account's folderfilter. # to a subset of folders that pass the account's folderfilter.
#
#
# You can customize the order in which mailbox names are listed in the
# generated file by specifying a sort_keyfunc, which takes a single
# dict argument containing keys 'accountname' and 'foldername'. This
# function will be called once for each mailbox, and should return a
# suitable sort key that defines this mailbox' position in the custom
# ordering.
#
# This is useful with e.g. Mutt-sidebar, which uses the mailbox order
# from the generated file when listing mailboxes in the sidebar.
#
# Default setting is
# sort_keyfunc = lambda d: (d['accountname'], d['foldername'])
################################################## ##################################################

View File

@ -58,13 +58,19 @@ def genmbnames():
if config.has_option("mbnames", "folderfilter"): if config.has_option("mbnames", "folderfilter"):
folderfilter = localeval.eval(config.get("mbnames", "folderfilter"), folderfilter = localeval.eval(config.get("mbnames", "folderfilter"),
{'re': re}) {'re': re})
mb_sort_keyfunc = lambda d: (d['accountname'], d['foldername'])
if config.has_option("mbnames", "sort_keyfunc"):
mb_sort_keyfunc = localeval.eval(config.get("mbnames", "sort_keyfunc"),
{'re': re})
itemlist = [] itemlist = []
for accountname in boxes.keys(): for accountname in boxes.keys():
for foldername in boxes[accountname]: for foldername in boxes[accountname]:
if folderfilter(accountname, foldername): if folderfilter(accountname, foldername):
itemlist.append(config.get("mbnames", "peritem", raw=1) % \ itemlist.append({'accountname': accountname,
{'accountname': accountname,
'foldername': foldername}) 'foldername': foldername})
itemlist.sort(key = mb_sort_keyfunc)
format_string = config.get("mbnames", "peritem", raw=1)
itemlist = [format_string % d for d in itemlist]
file.write(localeval.eval(config.get("mbnames", "sep")).join(itemlist)) file.write(localeval.eval(config.get("mbnames", "sep")).join(itemlist))
file.write(localeval.eval(config.get("mbnames", "footer"))) file.write(localeval.eval(config.get("mbnames", "footer")))
file.close() file.close()