]> code.delx.au - offlineimap/blob - offlineimap/mbnames.py
Sort mbnames for each account before writing
[offlineimap] / offlineimap / mbnames.py
1 # Mailbox name generator
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 import os.path
20 import re # for folderfilter
21 from threading import *
22
23 boxes = {}
24 config = None
25 accounts = None
26 mblock = Lock()
27
28 def init(conf, accts):
29 global config, accounts
30 config = conf
31 accounts = accts
32
33 def add(accountname, foldername):
34 if not accountname in boxes:
35 boxes[accountname] = []
36 if not foldername in boxes[accountname]:
37 boxes[accountname].append(foldername)
38
39 def sort(accountname, foldersort):
40 boxes[accountname].sort(foldersort)
41
42 def write():
43 # See if we're ready to write it out.
44 for account in accounts:
45 if account not in boxes:
46 return
47
48 genmbnames()
49
50 def genmbnames():
51 """Takes a configparser object and a boxlist, which is a list of hashes
52 containing 'accountname' and 'foldername' keys."""
53 mblock.acquire()
54 try:
55 localeval = config.getlocaleval()
56 if not config.getdefaultboolean("mbnames", "enabled", 0):
57 return
58 file = open(os.path.expanduser(config.get("mbnames", "filename")), "wt")
59 file.write(localeval.eval(config.get("mbnames", "header")))
60 folderfilter = lambda accountname, foldername: 1
61 if config.has_option("mbnames", "folderfilter"):
62 folderfilter = localeval.eval(config.get("mbnames", "folderfilter"),
63 {'re': re})
64 itemlist = []
65 for accountname in boxes.keys():
66 for foldername in boxes[accountname]:
67 if folderfilter(accountname, foldername):
68 itemlist.append(config.get("mbnames", "peritem", raw=1) % \
69 {'accountname': accountname,
70 'foldername': foldername})
71 file.write(localeval.eval(config.get("mbnames", "sep")).join(itemlist))
72 file.write(localeval.eval(config.get("mbnames", "footer")))
73 file.close()
74 finally:
75 mblock.release()
76
77