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=990c4717a860d1c48d5c02c4716f37d5fca17b5c;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hb=96f5b950cf70ac2c4bb7a6432b9eee24f1daef77;hpb=65d7c555aae3dce393aea69096f2395c523c2add diff --git a/src/lib/backupninja/handlers/__init__.py b/src/lib/backupninja/handlers/__init__.py index e69de29..990c471 100644 --- a/src/lib/backupninja/handlers/__init__.py +++ b/src/lib/backupninja/handlers/__init__.py @@ -0,0 +1,77 @@ +# +# Backupninja python reimplementation, based on original backupninja program +# by riseup.net. +# Copyright (C) 2010 Matthijs Kooijman +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +""" Handler superclass with common functionality """ + +import sys +import logging as log + +class Handler(object): + def __init__(self, conf): + self.conf = conf + + def run(self): + """ + Run this handler for a single target. Override this method + in a subclass + """ + pass + + def finish(self): + """ + Called when all targets have been processed. Can be overridden + in a subclass. + """ + pass + +def create_handler(ty, conf): + """ + Create a new (subclass of) Handler object for an action with the + given type. conf is the configuration to pass to the handler. + + If the handler cannot be loaded, an exception is thrown. + """ + modname = 'backupninja.handlers.%s' % ty + # Load the handler if it is not loaded yet + if not modname in sys.modules: + try: + __import__(modname, globals(), locals(), []) + except ImportError, e: + # Add some extra info, since the default exception does not + # show the full module name. + raise ImportError('Cannot load module %s: %s' % (modname, e)) + # Get the module from the module table + module = sys.modules[modname] + + # Check that the module has a "handler" top level function, which + # should create a new Handler object. + if not hasattr(module, 'handler'): + raise ImportError('%s is not valid: it ' + 'does not have a "handler" top level function.' + % (module.__file__)) + + # Call the "handler" function to create the actual handler + handler = module.handler(conf) + + # Check if the handler returned is really a subclass of Handler + if not isinstance(handler, Handler): + raise TypeError('%s is not valid, %s.handler did not return a ' + 'subclass of backupninja.handlers.Handler.' + % (module.__file__, modname)) + return handler