]> code.delx.au - offlineimap/blob - offlineimap/mbnames.py
Update FSF address
[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 write():
40 # See if we're ready to write it out.
41 for account in accounts:
42 if account not in boxes:
43 return
44
45 genmbnames()
46
47 def genmbnames():
48 """Takes a configparser object and a boxlist, which is a list of hashes
49 containing 'accountname' and 'foldername' keys."""
50 mblock.acquire()
51 try:
52 localeval = config.getlocaleval()
53 if not config.getdefaultboolean("mbnames", "enabled", 0):
54 return
55 file = open(os.path.expanduser(config.get("mbnames", "filename")), "wt")
56 file.write(localeval.eval(config.get("mbnames", "header")))
57 folderfilter = lambda accountname, foldername: 1
58 if config.has_option("mbnames", "folderfilter"):
59 folderfilter = localeval.eval(config.get("mbnames", "folderfilter"),
60 {'re': re})
61 itemlist = []
62 for accountname in boxes.keys():
63 for foldername in boxes[accountname]:
64 if folderfilter(accountname, foldername):
65 itemlist.append(config.get("mbnames", "peritem", raw=1) % \
66 {'accountname': accountname,
67 'foldername': foldername})
68 file.write(localeval.eval(config.get("mbnames", "sep")).join(itemlist))
69 file.write(localeval.eval(config.get("mbnames", "footer")))
70 file.close()
71 finally:
72 mblock.release()
73
74