handlers: Make create_handler throw exceptions on errors.
[matthijs/projects/backupninja.git] / src / lib / backupninja / handlers / __init__.py
1 #
2 #    Backupninja python reimplementation, based on original backupninja program
3 #    by riseup.net.
4 #    Copyright (C) 2010  Matthijs Kooijman <matthijs@stdin.nl>
5 #
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.
10 #
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.
15 #
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.
19
20 """ Handler superclass with common functionality """
21
22 import sys
23 import logging as log
24
25 class Handler(object):
26     def __init__(self, conf):
27         self.conf = conf
28
29     def run(self):
30         """
31         Run this handler for a single target. Override this method
32         in a subclass
33         """
34         pass
35
36     def finish(self):
37         """
38         Called when all targets have been processed. Can be overridden
39         in a subclass.
40         """
41         pass
42
43 def create_handler(ty, conf):
44     """
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.
47
48     If the handler cannot be loaded, an exception is thrown.
49     """
50     modname = 'backupninja.handlers.%s' % ty
51     # Load the handler if it is not loaded yet
52     if not modname in sys.modules:
53         try:
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]
61
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.' 
67                           % (module.__file__))
68
69     # Call the "handler" function to create the actual handler
70     handler = module.handler(conf)
71    
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))
77     return handler