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 class Handler(object):
26 def __init__(self, conf):
31 Run this handler for a single target. Override this method
38 Called when all targets have been processed. Can be overridden
43 def create_handler(ty, conf):
45 Create a new (subclass of) Handler object for an action with the
46 given type. conf is the configuration to pass to the handler.
48 If the handler cannot be loaded, it is logged and None is returned
49 (but any exceptions raised by the handler code itself are not
52 modname = 'backupninja.handlers.%s' % ty
53 # Load the handler if it is not loaded yet
54 if not modname in sys.modules:
56 __import__(modname, globals(), locals(), [])
57 except ImportError, e:
58 log.error('Cannot load action handler for "%s": %s'
61 # Get the module from the module table
62 module = sys.modules[modname]
64 # Check that the module has a "handler" top level function, which
65 # should create a new Handler object.
66 if not hasattr(module, 'handler'):
67 log.error('Action handler for "%s" (in "%s) is not valid: it '
68 'does not have a "handler" top level function.'
69 , ty, module.__file__)
72 # Call the "handler" function to create the actual handler
73 handler = module.handler(conf)
75 # Check if the handler returned is really a subclass of Handler
76 if not isinstance(handler, Handler):
77 log.error('Action handler for "%s" (in "%s) is not valid: it '
78 'does not return a subclass of backupninja.handlers.Handler.'
79 , ty, module.__file__)