action: Let each action track a status and log it.
[matthijs/projects/backupninja.git] / src / lib / backupninja / handlers / __init__.py
index 72b30179a3a0017f9bfe3447b60d9e5d5365922a..dbc403e04585e21f167410da25d8da99f938bd06 100644 (file)
@@ -19,7 +19,7 @@
 
 """ Action superclass with common functionality """
 
 
 """ Action superclass with common functionality """
 
-import sys
+import sys, ConfigParser
 import logging as log
 
 from backupninja import config
 import logging as log
 
 from backupninja import config
@@ -38,6 +38,9 @@ class Action(object):
         # See backupninja.config.load_config for the structure of this
         # value.
         self.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
 
     def run(self, **kwargs):
         """
 
     def run(self, **kwargs):
         """
@@ -59,7 +62,32 @@ class Action(object):
         """
         self.conf = config.load_config(filename, self.default_config)
 
         """
         self.conf = config.load_config(filename, self.default_config)
 
-        
+    def get_config_optional(self, section, option):
+        """
+        Returns the value of the given option. If the option was not set
+        (and no default was set in self.default_config), return None.
+
+        This is a convenience wrapper for ConfigParser.get(), since that
+        throws an exception on unset options.
+        """
+        try:
+            return self.conf.get(section, option)
+        except ConfigParser.NoOptionError:
+            return None
+
+    def get_config_mandatory(self, section, option):
+        """
+        Returns the value of the given option. If the option was not set
+        (and no default was set in self.default_config), raises a
+        backupninja.config.ConfigError.
+
+        This is a convenience wrapper for ConfigParser.get(), since that
+        has a very generic exception message on unknown options.
+        """
+        try:
+            return self.conf.get(section, option)
+        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):
     """