config: Skip action configs starting with a dot.
[matthijs/projects/backupninja.git] / src / lib / backupninja / config.py
1 # -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
2 # vim: set filetype=python sw=4 sts=4 expandtab autoindent:
3 #
4 #    Backupninja python reimplementation, based on original backupninja program
5 #    by riseup.net.
6 #    Copyright (C) 2010  Matthijs Kooijman <matthijs@stdin.nl>
7 #
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.
12 #
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.
17 #
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.
21
22 """ Load configuration for backupninja and configured actions """
23
24 import os, ConfigParser
25
26 default_config_dir = '/etc/backupninja'
27 default_global_config = 'backupninja.conf'
28 default_actions_dir = 'actions'
29
30 import logging as log
31
32 def get_global_config(opts):
33     """
34     Returns the global configuration, in a SafeConfigParser object.
35     If the configuration file can not be found, logs an error and
36     returns None.
37
38     opts are the parsed commandline options.
39     """
40     global_config = os.path.join(opts.config_dir, opts.global_config)
41     return _load_config(global_config)
42
43 def get_action_config(opts, action):
44     """
45     Returns the configuration for the named action, in a
46     SafeConfigParser object. If the configuration file can not be found,
47     logs an error and returns None.
48
49     opts are the parsed commandline options.
50     """
51     actions_dir = os.path.join(opts.config_dir, opts.actions_dir)
52     return _load_config(os.path.join(actions_dir, action))
53
54 def list_actions(opts):
55     """
56     Lists all actions defined in the configuration directory. Returns a
57     list of action names that can be passed to get_action_config.
58     opts are the parsed commandline options.
59     """
60     actions_dir = os.path.join(opts.config_dir, opts.actions_dir)
61     return [f for f in os.listdir(actions_dir) if not f.startswith('.')]
62     
63 def _load_config(filename):
64     # Open a file and read it
65     config = ConfigParser.SafeConfigParser()
66     log.debug('Reading config file "%s"', filename)
67     try:
68         file = open(filename, 'r')
69     except IOError, e:
70         # Log the error and return None
71         msg = 'Unable to open configuration file "%s": %s' % (filename, e)
72         log.error(msg)
73         return None
74
75     config.readfp(file)
76     return config