1 # -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
2 # vim: set filetype=python sw=4 sts=4 expandtab autoindent:
4 # Backupninja python reimplementation, based on original backupninja program
6 # Copyright (C) 2010 Matthijs Kooijman <matthijs@stdin.nl>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 """ Load configuration for backupninja and configured actions """
24 import os, ConfigParser
29 def get_global_config(opts):
31 Returns the global configuration, in a SafeConfigParser object.
32 If the configuration file can not be found, logs an error and
35 opts are the parsed commandline options.
37 global_config = os.path.join(opts.config_dir, opts.global_config)
38 return _load_config(global_config)
40 def get_action_config(opts, action):
42 Returns the configuration for the named action, in a
43 SafeConfigParser object. If the configuration file can not be found,
44 logs an error and returns None.
46 opts are the parsed commandline options.
48 actions_dir = os.path.join(opts.config_dir, opts.actions_dir)
49 return _load_config(os.path.join(actions_dir, action))
51 def list_actions(opts):
53 Lists all actions defined in the configuration directory. Returns a
54 list of action names that can be passed to get_action_config.
55 opts are the parsed commandline options.
57 actions_dir = os.path.join(opts.config_dir, opts.actions_dir)
58 return [f for f in os.listdir(actions_dir) if not f.startswith('.')]
60 def _load_config(filename):
61 # Open a file and read it
62 config = ConfigParser.SafeConfigParser()
63 log.debug('Reading config file "%s"', filename)
65 file = open(filename, 'r')
67 # Log the error and return None
68 msg = 'Unable to open configuration file "%s": %s' % (filename, e)