2 # Backupninja python reimplementation, based on original backupninja program
4 # Copyright (C) 2010 Matthijs Kooijman <matthijs@stdin.nl>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 """ Handler superclass with common functionality """
25 from backupninja import config
27 class Handler(object):
29 # Subclasses should overwrite this with their default config
30 # See backupninja.config.load_config for the structure of this
32 self.default_config = {}
34 def run(self, **kwargs):
36 Run this handler for a single target. Override this method
41 def finish(self, **kwargs):
43 Called when all targets have been processed. Can be overridden
48 def load_config(self, filename):
50 Load the configuration for this action from the given filename.
52 self.conf = config.load_config(filename, self.default_config)
56 def create_handler(ty):
58 Create a new (subclass of) Handler object for an action with the
61 If the handler cannot be loaded, an exception is thrown.
63 modname = 'backupninja.handlers.%s' % ty
64 # Load the handler if it is not loaded yet
65 if not modname in sys.modules:
66 log.debug('Loading handler for type "%s"', ty)
68 __import__(modname, globals(), locals(), [])
69 except ImportError, e:
70 # Add some extra info, since the default exception does not
71 # show the full module name.
72 raise ImportError('Cannot load module %s: %s' % (modname, e))
73 log.debug('Loaded handler for type "%s" from "%s"', ty, sys.modules[modname].__file__)
74 # Get the module from the module table
75 module = sys.modules[modname]
77 # Check that the module has a "handler" top level function, which
78 # should create a new Handler object.
79 if not hasattr(module, 'handler'):
80 raise ImportError('%s is not valid: it '
81 'does not have a "handler" top level function.'
84 # Call the "handler" function to create the actual handler
85 handler = module.handler()
87 # Check if the handler returned is really a subclass of Handler
88 if not isinstance(handler, Handler):
89 raise TypeError('%s is not valid, %s.handler did not return a '
90 'subclass of backupninja.handlers.Handler.'
91 % (module.__file__, modname))