X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fprojects%2Fbackupninja.git;a=blobdiff_plain;f=src%2Flib%2Fbackupninja%2Fhandlers%2F__init__.py;h=484677171e0e72ec57250bbdfeaf3b554acf226e;hp=72b30179a3a0017f9bfe3447b60d9e5d5365922a;hb=7cc646921e15310e86807c26f8c6462b41362e58;hpb=74f27713c34ce30c7eef87891f095a0d4bf15f50 diff --git a/src/lib/backupninja/handlers/__init__.py b/src/lib/backupninja/handlers/__init__.py index 72b3017..4846771 100644 --- a/src/lib/backupninja/handlers/__init__.py +++ b/src/lib/backupninja/handlers/__init__.py @@ -19,11 +19,25 @@ """ Action superclass with common functionality """ -import sys +import sys, ConfigParser import logging as log 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. @@ -38,6 +52,13 @@ class Action(object): # 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 = log def run(self, **kwargs): """ @@ -59,7 +80,32 @@ class Action(object): """ 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): """