X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fprojects%2Fbackupninja.git;a=blobdiff_plain;f=src%2Flib%2Fbackupninja%2Fconfig.py;fp=src%2Flib%2Fbackupninja%2Fconfig.py;h=0720f0aa05e36f6c38824728de1d86bee957ca41;hp=1e29290d23fc229600c47776928d7013af66a1dd;hb=d64d30066c2cbcf02e1c4d05198ca430584b5cbd;hpb=a2f36355bac67e7369ad224e2f4276a1768e4f50 diff --git a/src/lib/backupninja/config.py b/src/lib/backupninja/config.py index 1e29290..0720f0a 100644 --- a/src/lib/backupninja/config.py +++ b/src/lib/backupninja/config.py @@ -23,6 +23,8 @@ import os, ConfigParser +# Defaults for global configuration values +default_global_config = {} import logging as log @@ -35,31 +37,31 @@ def get_global_config(opts): opts are the parsed commandline options. """ global_config = os.path.join(opts.config_dir, opts.global_config) - return _load_config(global_config) - -def get_action_config(opts, action): - """ - Returns the configuration for the named action, in a - SafeConfigParser object. If the configuration file can not be found, - logs an error and returns None. - - opts are the parsed commandline options. - """ - actions_dir = os.path.join(opts.config_dir, opts.actions_dir) - return _load_config(os.path.join(actions_dir, action)) + return load_config(global_config, default_global_config) def list_actions(opts): """ Lists all actions defined in the configuration directory. Returns a - list of action names that can be passed to get_action_config. + list of full paths to action configuration files. + opts are the parsed commandline options. """ actions_dir = os.path.join(opts.config_dir, opts.actions_dir) - return [f for f in os.listdir(actions_dir) if not f.startswith('.')] + return [os.path.join(actions_dir, f) + for f in os.listdir(actions_dir) + if not f.startswith('.')] -def _load_config(filename): +def load_config(filename, defaults): + """ + Load a configuration file, using the given default values. + + The defaults argument contains a dictionary of sections. Each key is + a section name, each value is a dictionary of values (where the key + is the value name and the value is the actual value). + """ # Open a file and read it config = ConfigParser.SafeConfigParser() + _set_default_config(config, defaults) log.debug('Reading config file "%s"', filename) try: file = open(filename, 'r') @@ -71,3 +73,20 @@ def _load_config(filename): config.readfp(file) return config + +def _set_default_config(parser, values): + """ + Saves the values given to the ConfigParser given. This can be used + to store a set of default values to a ConfigParser before loading a + file (The defaults argument to the ConfigParser constructor only + sets the values of the "DEFAULT" section). + + The values argument contains a dictionary of sections. Each key is a + section name, each value is a dictionary of values (where the key is + the value name and the value is the actual value). + """ + for section, options in values.items(): + if not parser.has_section(section): + parser.add_section(section) + for option, value in options.items(): + parser.set(section, option, value)