]>
code.delx.au - offlineimap/blob - offlineimap/CustomConfig.py
1 # Copyright (C) 2003 John Goerzen
2 # <jgoerzen@complete.org>
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.
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.
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
18 from ConfigParser
import ConfigParser
19 from offlineimap
.localeval
import LocalEval
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
)
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
)
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
)
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
),
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)
56 def getlocaleval(self
):
57 if self
.has_option("general", "pythonfile"):
58 path
= os
.path
.expanduser(self
.get("general", "pythonfile"))
61 return LocalEval(path
)
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"."""
70 return [x
[len(key
):] for x
in self
.sections() \
73 def CustomConfigDefault():
74 """Just a sample constant that won't occur anywhere else to use for the
78 class ConfigHelperMixin
:
79 def _confighelper_runner(self
, option
, default
, defaultfunc
, mainfunc
):
80 if default
!= CustomConfigDefault
:
81 return apply(defaultfunc
, [self
.getsection(), option
, default
])
83 return apply(mainfunc
, [self
.getsection(), option
])
85 def getconf(self
, option
, default
= CustomConfigDefault
):
86 return self
._confighelper
_runner
(option
, default
,
87 self
.getconfig().getdefault
,
90 def getconfboolean(self
, option
, default
= CustomConfigDefault
):
91 return self
._confighelper
_runner
(option
, default
,
92 self
.getconfig().getdefaultboolean
,
93 self
.getconfig().getboolean
)
95 def getconfint(self
, option
, default
= CustomConfigDefault
):
96 return self
._confighelper
_runner
(option
, default
,
97 self
.getconfig().getdefaultint
,
98 self
.getconfig().getint
)
100 def getconffloat(self
, option
, default
= CustomConfigDefault
):
101 return self
._confighelper
_runner
(option
, default
,
102 self
.getconfig().getdefaultfloat
,
103 self
.getconfig().getfloat
)