[PATCH] mbnames: Allow custom sorting of mailboxes
Johan Herland
johan at herland.net
Sun May 19 13:23:48 BST 2013
mutt-sidebar shows 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 'sortkey' directive in
the [mbnames] section.
'sortkey' specifies a function that will be called once for each
mailbox (passing a dict with 2 items: 'accountname' and 'foldername'),
and should return a suitable sort key that mailbox.
If 'sortkey' is not given, we default to the current alphabetical
sorting provided by using (d['accountname'], d['foldername']) as sort
key.
Signed-off-by: Johan Herland <johan at herland.net>
---
(Please keep me CCed, as I'm not subscribed.)
This is just to scratch my own itch which I encountered while trying to
get offlineimap and mutt-sidebar to play nice together.
I already sent this as a pull request on GitHub [1] before reading the
SubmittingPatches document. Sorry about that.
Have fun! :)
...Johan
[1]: https://github.com/OfflineIMAP/offlineimap/pull/34
Changelog.rst | 3 +++
offlineimap.conf | 12 ++++++++++++
offlineimap/mbnames.py | 12 ++++++++----
3 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/Changelog.rst b/Changelog.rst
index 643e63e..cf6d7a4 100644
--- a/Changelog.rst
+++ b/Changelog.rst
@@ -7,6 +7,9 @@ ChangeLog
WIP (add new stuff for the next release)
========================================
+* Allow mbnames output to be sorted by a custom sort key by specifying a
+ 'sortkey' function in the [mbnames] section of the config.
+
OfflineIMAP v6.5.5-rc1 (2012-09-05)
===================================
diff --git a/offlineimap.conf b/offlineimap.conf
index fccceab..4b45757 100644
--- a/offlineimap.conf
+++ b/offlineimap.conf
@@ -145,6 +145,18 @@ footer = "\n"
#
# Note that this filter can be used only to further restrict mbnames
# 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 sortkey, which takes a single dict
+# argument containing the accountname and foldername. The sortkey 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.
+# If sortkey is not given, we default to alphabetical sorting of
+# mailboxes, equivalent to this:
+#
+# sortkey = lambda d: (d['accountname'], d['foldername'])
##################################################
diff --git a/offlineimap/mbnames.py b/offlineimap/mbnames.py
index fb8af74..b0f164e 100644
--- a/offlineimap/mbnames.py
+++ b/offlineimap/mbnames.py
@@ -58,17 +58,21 @@ def genmbnames():
if config.has_option("mbnames", "folderfilter"):
folderfilter = localeval.eval(config.get("mbnames", "folderfilter"),
{'re': re})
+ mb_sort_key = lambda d: (d['accountname'], d['foldername'])
+ if config.has_option("mbnames", "sortkey"):
+ mb_sort_key = localeval.eval(config.get("mbnames", "sortkey"),
+ {'re': re})
itemlist = []
for accountname in boxes.keys():
for foldername in boxes[accountname]:
if folderfilter(accountname, foldername):
- itemlist.append(config.get("mbnames", "peritem", raw=1) % \
- {'accountname': accountname,
+ itemlist.append({'accountname': accountname,
'foldername': foldername})
+ itemlist.sort(key = mb_sort_key)
+ 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", "footer")))
file.close()
finally:
mblock.release()
-
-
--
1.8.1.3.704.g33f7d4f
More information about the OfflineIMAP-project
mailing list