Introduce CustomConfig method that applies set of transforms
It is a bit cleaner than making chains of calls like {{{ value = os.path.expanduser(value) value = os.path.abspath(value) }}} since we do see all transformations to be applied in a single iterable and have no repeated code like in the above example. Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
This commit is contained in:
parent
dbf683ed38
commit
8f3c22e227
@ -94,14 +94,17 @@ class CustomConfigParser(SafeConfigParser):
|
||||
return default
|
||||
|
||||
def getmetadatadir(self):
|
||||
metadatadir = os.path.expanduser(self.getdefault("general", "metadata", "~/.offlineimap"))
|
||||
xforms = [os.path.expanduser]
|
||||
d = self.getdefault("general", "metadata", "~/.offlineimap")
|
||||
metadatadir = self.apply_xforms(d, xforms)
|
||||
if not os.path.exists(metadatadir):
|
||||
os.mkdir(metadatadir, 0o700)
|
||||
return metadatadir
|
||||
|
||||
def getlocaleval(self):
|
||||
xforms = [os.path.expanduser]
|
||||
if self.has_option("general", "pythonfile"):
|
||||
path = os.path.expanduser(self.get("general", "pythonfile"))
|
||||
path = self.apply_xforms(self.get("general", "pythonfile"), xforms)
|
||||
else:
|
||||
path = None
|
||||
return LocalEval(path)
|
||||
@ -132,6 +135,26 @@ class CustomConfigParser(SafeConfigParser):
|
||||
self.set(section, option, value)
|
||||
|
||||
|
||||
def apply_xforms(self, string, transforms):
|
||||
"""
|
||||
Applies set of transformations to a string.
|
||||
|
||||
Arguments:
|
||||
- string: source string; if None, then no processing will
|
||||
take place.
|
||||
- transforms: iterable that returns transformation function
|
||||
on each turn.
|
||||
|
||||
Returns transformed string.
|
||||
|
||||
"""
|
||||
if string == None:
|
||||
return None
|
||||
for f in transforms:
|
||||
string = f(string)
|
||||
return string
|
||||
|
||||
|
||||
|
||||
def CustomConfigDefault():
|
||||
"""
|
||||
@ -161,9 +184,10 @@ class ConfigHelperMixin:
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def _confighelper_runner(self, option, default, defaultfunc, mainfunc, *args):
|
||||
"""
|
||||
Return configuration or default value for option
|
||||
Returns configuration or default value for option
|
||||
that contains in section identified by getsection().
|
||||
|
||||
Arguments:
|
||||
@ -213,27 +237,96 @@ class ConfigHelperMixin:
|
||||
|
||||
|
||||
def getconf(self, option, default = CustomConfigDefault):
|
||||
"""
|
||||
Retrieves string from the configuration.
|
||||
|
||||
Arguments:
|
||||
- option: option name whose value is to be retrieved;
|
||||
- default: default return value if no such option
|
||||
exists.
|
||||
|
||||
"""
|
||||
return self._confighelper_runner(option, default,
|
||||
self.getconfig().getdefault,
|
||||
self.getconfig().get)
|
||||
|
||||
|
||||
def getconf_xform(self, option, xforms, default = CustomConfigDefault):
|
||||
"""
|
||||
Retrieves string from the configuration transforming the result.
|
||||
|
||||
Arguments:
|
||||
- option: option name whose value is to be retrieved;
|
||||
- xforms: iterable that returns transform functions
|
||||
to be applied to the value of the option,
|
||||
both retrieved and default one;
|
||||
- default: default value for string if no such option
|
||||
exists.
|
||||
|
||||
"""
|
||||
value = self.getconf(option, default)
|
||||
return self.getconfig().apply_xforms(value, xforms)
|
||||
|
||||
|
||||
def getconfboolean(self, option, default = CustomConfigDefault):
|
||||
"""
|
||||
Retrieves boolean value from the configuration.
|
||||
|
||||
Arguments:
|
||||
- option: option name whose value is to be retrieved;
|
||||
- default: default return value if no such option
|
||||
exists.
|
||||
|
||||
"""
|
||||
return self._confighelper_runner(option, default,
|
||||
self.getconfig().getdefaultboolean,
|
||||
self.getconfig().getboolean)
|
||||
|
||||
|
||||
def getconfint(self, option, default = CustomConfigDefault):
|
||||
"""
|
||||
Retrieves integer value from the configuration.
|
||||
|
||||
Arguments:
|
||||
- option: option name whose value is to be retrieved;
|
||||
- default: default return value if no such option
|
||||
exists.
|
||||
|
||||
"""
|
||||
return self._confighelper_runner(option, default,
|
||||
self.getconfig().getdefaultint,
|
||||
self.getconfig().getint)
|
||||
|
||||
|
||||
def getconffloat(self, option, default = CustomConfigDefault):
|
||||
"""
|
||||
Retrieves floating-point value from the configuration.
|
||||
|
||||
Arguments:
|
||||
- option: option name whose value is to be retrieved;
|
||||
- default: default return value if no such option
|
||||
exists.
|
||||
|
||||
"""
|
||||
return self._confighelper_runner(option, default,
|
||||
self.getconfig().getdefaultfloat,
|
||||
self.getconfig().getfloat)
|
||||
|
||||
|
||||
def getconflist(self, option, separator_re,
|
||||
default = CustomConfigDefault):
|
||||
"""
|
||||
Retrieves strings from the configuration and splits it
|
||||
into the list of strings.
|
||||
|
||||
Arguments:
|
||||
- option: option name whose value is to be retrieved;
|
||||
- separator_re: regular expression for separator
|
||||
to be used for split operation;
|
||||
- default: default return value if no such option
|
||||
exists.
|
||||
|
||||
"""
|
||||
return self._confighelper_runner(option, default,
|
||||
self.getconfig().getdefaultlist,
|
||||
self.getconfig().getlist, separator_re)
|
||||
|
@ -201,11 +201,11 @@ class IMAPRepository(BaseRepository):
|
||||
|
||||
def getsslcacertfile(self):
|
||||
"""Return the absolute path of the CA certfile to use, if any"""
|
||||
cacertfile = self.getconf('sslcacertfile', get_os_sslcertfile())
|
||||
xforms = [os.path.expanduser, os.path.abspath]
|
||||
cacertfile = self.getconf_xform('sslcacertfile', xforms,
|
||||
get_os_sslcertfile())
|
||||
if cacertfile is None:
|
||||
return None
|
||||
cacertfile = os.path.expanduser(cacertfile)
|
||||
cacertfile = os.path.abspath(cacertfile)
|
||||
if not os.path.isfile(cacertfile):
|
||||
raise SyntaxWarning("CA certfile for repository '%s' could "
|
||||
"not be found. No such file: '%s'" \
|
||||
|
@ -61,7 +61,8 @@ class MaildirRepository(BaseRepository):
|
||||
os.utime(cur_dir, (cur_atime, os.path.getmtime(cur_dir)))
|
||||
|
||||
def getlocalroot(self):
|
||||
return os.path.expanduser(self.getconf('localfolders'))
|
||||
xforms = [os.path.expanduser]
|
||||
return self.getconf_xform('localfolders', xforms)
|
||||
|
||||
def debug(self, msg):
|
||||
self.ui.debug('maildir', msg)
|
||||
|
Loading…
Reference in New Issue
Block a user