--- /dev/null
+# -*- 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 <matthijs@stdin.nl>
+#
+# 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