]> code.delx.au - offlineimap/blob - offlineimap/CustomConfig.py
Update FSF address
[offlineimap] / offlineimap / CustomConfig.py
1 # Copyright (C) 2003 John Goerzen
2 # <jgoerzen@complete.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18 from ConfigParser import ConfigParser
19 from offlineimap.localeval import LocalEval
20 import os
21
22 class CustomConfigParser(ConfigParser):
23 def getdefault(self, section, option, default, *args, **kwargs):
24 """Same as config.get, but returns the "default" option if there
25 is no such option specified."""
26 if self.has_option(section, option):
27 return apply(self.get, [section, option] + list(args), kwargs)
28 else:
29 return default
30
31 def getdefaultint(self, section, option, default, *args, **kwargs):
32 if self.has_option(section, option):
33 return apply(self.getint, [section, option] + list(args), kwargs)
34 else:
35 return default
36
37 def getdefaultfloat(self, section, option, default, *args, **kwargs):
38 if self.has_option(section, option):
39 return apply(self.getfloat, [section, option] + list(args), kwargs)
40 else:
41 return default
42
43 def getdefaultboolean(self, section, option, default, *args, **kwargs):
44 if self.has_option(section, option):
45 return apply(self.getboolean, [section, option] + list(args),
46 kwargs)
47 else:
48 return default
49
50 def getmetadatadir(self):
51 metadatadir = os.path.expanduser(self.getdefault("general", "metadata", "~/.offlineimap"))
52 if not os.path.exists(metadatadir):
53 os.mkdir(metadatadir, 0700)
54 return metadatadir
55
56 def getlocaleval(self):
57 if self.has_option("general", "pythonfile"):
58 path = os.path.expanduser(self.get("general", "pythonfile"))
59 else:
60 path = None
61 return LocalEval(path)
62
63 def getsectionlist(self, key):
64 """Returns a list of sections that start with key + " ". That is,
65 if key is "Account", returns all section names that start with
66 "Account ", but strips off the "Account ". For instance, for
67 "Account Test", returns "Test"."""
68
69 key = key + ' '
70 return [x[len(key):] for x in self.sections() \
71 if x.startswith(key)]
72
73 def CustomConfigDefault():
74 """Just a sample constant that won't occur anywhere else to use for the
75 default."""
76 pass
77
78 class ConfigHelperMixin:
79 def _confighelper_runner(self, option, default, defaultfunc, mainfunc):
80 if default != CustomConfigDefault:
81 return apply(defaultfunc, [self.getsection(), option, default])
82 else:
83 return apply(mainfunc, [self.getsection(), option])
84
85 def getconf(self, option, default = CustomConfigDefault):
86 return self._confighelper_runner(option, default,
87 self.getconfig().getdefault,
88 self.getconfig().get)
89
90 def getconfboolean(self, option, default = CustomConfigDefault):
91 return self._confighelper_runner(option, default,
92 self.getconfig().getdefaultboolean,
93 self.getconfig().getboolean)
94
95 def getconfint(self, option, default = CustomConfigDefault):
96 return self._confighelper_runner(option, default,
97 self.getconfig().getdefaultint,
98 self.getconfig().getint)
99
100 def getconffloat(self, option, default = CustomConfigDefault):
101 return self._confighelper_runner(option, default,
102 self.getconfig().getdefaultfloat,
103 self.getconfig().getfloat)
104