X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fprojects%2Fbackupninja.git;a=blobdiff_plain;f=src%2Flib%2Fbackupninja%2Fconfig.py;h=5f1b283094a8f27641ab327a622163650ef58e26;hp=af3d4dae3b7ddda15d3b4b22a1328eddfb93571e;hb=101bff5ef5fec6382ab572a286c4e79d0312c6cd;hpb=38783916e32faa5653f43620f9e31acc9418721e diff --git a/src/lib/backupninja/config.py b/src/lib/backupninja/config.py index af3d4da..5f1b283 100644 --- a/src/lib/backupninja/config.py +++ b/src/lib/backupninja/config.py @@ -23,12 +23,20 @@ import os, ConfigParser -default_config_dir = '/etc/backupninja' -default_global_config = 'backupninja.conf' -default_actions_dir = 'actions' +# Defaults for global configuration values +default_global_config = {} import logging as log +class ConfigError(Exception): + """ + An exception thrown when something is wrong with the config. + This is not thrown by the config module, but it is meant to be + thrown by handlers when they find something wrong with the + configuration contents. + """ + pass + def get_global_config(opts): """ Returns the global configuration, in a SafeConfigParser object. @@ -37,32 +45,32 @@ def get_global_config(opts): opts are the parsed commandline options. """ - global_config = os.path.join(default_config_dir, default_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(default_config_dir, default_actions_dir) - return _load_config(os.path.join(actions_dir, action)) + global_config = os.path.join(opts.config_dir, opts.global_config) + 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(default_config_dir, default_actions_dir) - return os.listdir(actions_dir) + actions_dir = os.path.join(opts.config_dir, opts.actions_dir) + 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') @@ -74,3 +82,23 @@ 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(): + # Interpret None as "no default", since ConfigParser doesn't + # like non-string values. + if not value is None: + parser.set(section, option, value)