1 # Copyright (C) 1998-2007 by the Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 from Mailman
.configuration
import config
20 """A `safe' dictionary for string interpolation."""
24 # XXX This module should go away.
29 """Dictionary which returns a default value for unknown keys.
31 This is used in maketext so that editing templates is a bit more robust.
33 def __init__(self
, d
='', charset
=None, lang
=None):
34 super(SafeDict
, self
).__init
__(d
)
38 self
.cset
= config
.languages
.get_charset(lang
)
40 self
.cset
= 'us-ascii'
42 def __getitem__(self
, key
):
44 return super(SafeDict
, self
).__getitem
__(key
)
46 if isinstance(key
, basestring
):
49 return '<Missing key: %s>' % `key`
51 def interpolate(self
, template
):
52 for k
, v
in self
.items():
53 if isinstance(v
, str):
54 self
.__setitem
__(k
, unicode(v
, self
.cset
))
55 return template
% self
59 class MsgSafeDict(SafeDict
):
60 def __init__(self
, msg
, d
=None):
64 super(MsgSafeDict
, self
).__init
__(d
)
66 def __getitem__(self
, key
):
67 if key
.startswith('msg_'):
68 return self
.__msg
.get(key
[4:], 'n/a')
69 elif key
.startswith('allmsg_'):
71 all
= self
.__msg
.get_all(key
[7:], missing
)
74 return COMMASPACE
.join(all
)
76 return super(MsgSafeDict
, self
).__getitem
__(key
)
79 d
= super(MsgSafeDict
, self
).copy()
80 for k
in self
.__msg
.keys():
81 vals
= self
.__msg
.get_all(k
)
83 d
['msg_'+k
.lower()] = vals
[0]
85 d
['allmsg_'+k
.lower()] = COMMASPACE
.join(vals
)