action: Create and use a different Logger for each action.
[matthijs/projects/backupninja.git] / src / lib / backupninja / handlers / __init__.py
index f6559f9b692294b401244f7dc65eb47c203a8779..22f9589ee5c97636df2d0817236096c9f2d0aa54 100644 (file)
@@ -24,6 +24,20 @@ import logging as log
 
 from backupninja import config
 
 
 from backupninja import config
 
+def fail_on_exception(f):
+    """
+    This is a decorator meant for methods on the Action class. It
+    catches any exceptions thrown, sets the failed attribute to True and
+    rethrows the exception.
+    """
+    def inner(self, *args, **kwargs):
+        try:
+            f(self, *args, **kwargs)
+        except:
+            self.failed = True
+            raise
+    return inner
+
 class Action(object):
     """
     Subclasses of Action represent handlers for various action types.
 class Action(object):
     """
     Subclasses of Action represent handlers for various action types.
@@ -33,11 +47,18 @@ class Action(object):
     which is a combination of a action type and a specific action
     configuration).
     """
     which is a combination of a action type and a specific action
     configuration).
     """
-    def __init__(self):
+    def __init__(self, logger):
         # Subclasses should overwrite this with their default config
         # See backupninja.config.load_config for the structure of this
         # value.
         self.default_config = {}
         # Subclasses should overwrite this with their default config
         # See backupninja.config.load_config for the structure of this
         # value.
         self.default_config = {}
+        # Assume we'll run succesfully. If anything fails in the
+        # meanwhile, set this to True.
+        self.failed = False
+        # A logger object for this action. In the future, this might
+        # become a specific logger, that includes the action name and
+        # type.
+        self.log = logger
 
     def run(self, **kwargs):
         """
 
     def run(self, **kwargs):
         """
@@ -86,10 +107,11 @@ class Action(object):
         except ConfigParser.NoOptionError:
             raise config.ConfigError("Option '%s' in section '%s' is mandatory, please configure it" % (option, section))
 
         except ConfigParser.NoOptionError:
             raise config.ConfigError("Option '%s' in section '%s' is mandatory, please configure it" % (option, section))
 
-def create_action(ty):
+def create_action(ty, **kwargs):
     """
     Create a new (subclass of) Action object for an action with the
     """
     Create a new (subclass of) Action object for an action with the
-    given type.
+    given type. Any extra keyword arguments are passed to the
+    constructor.
 
     If the handler class for this type cannot be loaded, an exception is
     thrown.
 
     If the handler class for this type cannot be loaded, an exception is
     thrown.
@@ -116,7 +138,7 @@ def create_action(ty):
                           % (module.__file__))
 
     # Call the "handler" function to create the actual action
                           % (module.__file__))
 
     # Call the "handler" function to create the actual action
-    action = module.handler()
+    action = module.handler(**kwargs)
    
     # Check if the handler returned is really a subclass of Action
     if not isinstance(action, Action):
    
     # Check if the handler returned is really a subclass of Action
     if not isinstance(action, Action):