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=af3d4dae3b7ddda15d3b4b22a1328eddfb93571e;hp=0000000000000000000000000000000000000000;hb=38783916e32faa5653f43620f9e31acc9418721e;hpb=ece57a3973b3afeb138ac0eaab2da742955a2bad diff --git a/src/lib/backupninja/config.py b/src/lib/backupninja/config.py new file mode 100644 index 0000000..af3d4da --- /dev/null +++ b/src/lib/backupninja/config.py @@ -0,0 +1,76 @@ +# -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*- +# vim: set filetype=python sw=4 sts=4 expandtab autoindent: +# +# Backupninja python reimplementation, based on original backupninja program +# by riseup.net. +# Copyright (C) 2010 Matthijs Kooijman +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" Load configuration for backupninja and configured actions """ + +import os, ConfigParser + +default_config_dir = '/etc/backupninja' +default_global_config = 'backupninja.conf' +default_actions_dir = 'actions' + +import logging as log + +def get_global_config(opts): + """ + Returns the global configuration, in a SafeConfigParser object. + If the configuration file can not be found, logs an error and + returns None. + + 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)) + +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. + opts are the parsed commandline options. + """ + actions_dir = os.path.join(default_config_dir, default_actions_dir) + return os.listdir(actions_dir) + +def _load_config(filename): + # Open a file and read it + config = ConfigParser.SafeConfigParser() + log.debug('Reading config file "%s"', filename) + try: + file = open(filename, 'r') + except IOError, e: + # Log the error and return None + msg = 'Unable to open configuration file "%s": %s' % (filename, e) + log.error(msg) + return None + + config.readfp(file) + return config