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, an exception is thrown.
50 modname = 'backupninja.handlers.%s' % ty
51 # Load the handler if it is not loaded yet
52 if not modname in sys.modules:
54 __import__(modname, globals(), locals(), [])
55 except ImportError, e:
56 # Add some extra info, since the default exception does not
57 # show the full module name.
58 raise ImportError('Cannot load module %s: %s' % (modname, e))
59 # Get the module from the module table
60 module = sys.modules[modname]
62 # Check that the module has a "handler" top level function, which
63 # should create a new Handler object.
64 if not hasattr(module, 'handler'):
65 raise ImportError('%s is not valid: it '
66 'does not have a "handler" top level function.'
69 # Call the "handler" function to create the actual handler
70 handler = module.handler(conf)
72 # Check if the handler returned is really a subclass of Handler
73 if not isinstance(handler, Handler):
74 raise TypeError('%s is not valid, %s.handler did not return a '
75 'subclass of backupninja.handlers.Handler.'
76 % (module.__file__, modname))