handlers: Rename Handler class to Action.
[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 """ Action superclass with common functionality """
21
22 import sys
23 import logging as log
24
25 from backupninja import config
26
27 class Action(object):
28     """
29     Subclasses of Action represent handlers for various action types.
30     This class is called Action instead of Handler, since even though the
31     classes could be referred to as handlers, the instances of this
32     class are really actions (i.e., it represents a specific action,
33     which is a combination of a action type and a specific action
34     configuration).
35     """
36     def __init__(self):
37         # Subclasses should overwrite this with their default config
38         # See backupninja.config.load_config for the structure of this
39         # value.
40         self.default_config = {}
41
42     def run(self, **kwargs):
43         """
44         Run this action for a single target. Override this method
45         in a subclass
46         """
47         pass
48
49     def finish(self, **kwargs):
50         """
51         Called when all targets have been processed. Can be overridden
52         in a subclass.
53         """
54         pass
55
56     def load_config(self, filename):
57         """
58         Load the configuration for this action from the given filename.
59         """
60         self.conf = config.load_config(filename, self.default_config)
61
62         
63
64 def create_action(ty):
65     """
66     Create a new (subclass of) Action object for an action with the
67     given type.
68
69     If the handler class for this type cannot be loaded, an exception is
70     thrown.
71     """
72     modname = 'backupninja.handlers.%s' % ty
73     # Load the handler if it is not loaded yet
74     if not modname in sys.modules:
75         log.debug('Loading handler for type "%s"', ty)
76         try:
77             __import__(modname, globals(), locals(), [])
78         except ImportError, e:
79             # Add some extra info, since the default exception does not
80             # show the full module name.
81             raise ImportError('Cannot load module %s: %s' % (modname, e))
82         log.debug('Loaded handler for type "%s" from "%s"', ty, sys.modules[modname].__file__)
83     # Get the module from the module table
84     module = sys.modules[modname]
85
86     # Check that the module has a "handler" top level function, which
87     # should create a new Action object.
88     if not hasattr(module, 'handler'):
89         raise ImportError('%s is not valid: it '
90                           'does not have a "handler" top level function.' 
91                           % (module.__file__))
92
93     # Call the "handler" function to create the actual action
94     action = module.handler()
95    
96     # Check if the handler returned is really a subclass of Action
97     if not isinstance(action, Action):
98         raise TypeError('%s is not valid, %s.handler did not return a '
99                         'subclass of backupninja.handlers.Handler.'
100                         % (module.__file__, modname))
101     return action