Implement CustomConfig.set_if_not_exists()

A convenience helper function that allows to set a configuration value
if the user has not explicitly configured anything ie the option does
not exist yet in the configuration. It won't do anything, if the option
exists.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2011-09-15 13:08:04 +02:00
parent 5979cc8ff9
commit a8c6407f50

View File

@ -71,6 +71,14 @@ class CustomConfigParser(SafeConfigParser):
return [x[len(key):] for x in self.sections() \
if x.startswith(key)]
def set_if_not_exists(self, section, option, value):
"""Set a value if it does not exist yet
This allows to set default if the user has not explicitly
configured anything."""
if not self.has_option(section, option):
self.set(section, option, value)
def CustomConfigDefault():
"""Just a constant that won't occur anywhere else.