projects
/
matthijs
/
projects
/
backupninja.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
config: Restructure config loading to allow defaults.
[matthijs/projects/backupninja.git]
/
src
/
lib
/
backupninja
/
handlers
/
__init__.py
diff --git
a/src/lib/backupninja/handlers/__init__.py
b/src/lib/backupninja/handlers/__init__.py
index da07b6d495146a615e29ca4c8a22b807d8de39bb..ccb5c8c142ec1b8b9fd0dca30df8d71ed7d8b311 100644
(file)
--- a/
src/lib/backupninja/handlers/__init__.py
+++ b/
src/lib/backupninja/handlers/__init__.py
@@
-22,60
+22,71
@@
import sys
import logging as log
import sys
import logging as log
+from backupninja import config
+
class Handler(object):
class Handler(object):
- def __init__(self, conf):
- self.conf = conf
+ def __init__(self):
+ # Subclasses should overwrite this with their default config
+ # See backupninja.config.load_config for the structure of this
+ # value.
+ self.default_config = {}
- def run(self):
+ def run(self
, **kwargs
):
"""
Run this handler for a single target. Override this method
in a subclass
"""
pass
"""
Run this handler for a single target. Override this method
in a subclass
"""
pass
- def finish(self):
+ def finish(self
, **kwargs
):
"""
Called when all targets have been processed. Can be overridden
in a subclass.
"""
pass
"""
Called when all targets have been processed. Can be overridden
in a subclass.
"""
pass
-def create_handler(ty, conf):
+ def load_config(self, filename):
+ """
+ Load the configuration for this action from the given filename.
+ """
+ self.conf = config.load_config(filename, self.default_config)
+
+
+
+def create_handler(ty):
"""
Create a new (subclass of) Handler object for an action with the
"""
Create a new (subclass of) Handler object for an action with the
- given type.
conf is the configuration to pass to the handler.
+ given type.
- If the handler cannot be loaded, it is logged and None is returned
- (but any exceptions raised by the handler code itself are not
- handled).
+ 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:
"""
modname = 'backupninja.handlers.%s' % ty
# Load the handler if it is not loaded yet
if not modname in sys.modules:
+ log.debug('Loading handler for type "%s"', ty)
try:
__import__(modname, globals(), locals(), [])
except ImportError, e:
try:
__import__(modname, globals(), locals(), [])
except ImportError, e:
- log.error('Cannot load action handler for "%s": %s'
- , ty, e)
- return None
+ # Add some extra info, since the default exception does not
+ # show the full module name.
+ raise ImportError('Cannot load module %s: %s' % (modname, e))
+ log.debug('Loaded handler for type "%s" from "%s"', ty, sys.modules[modname].__file__)
# 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'):
# 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'):
- log.error('Action handler for "%s" (in "%s) is not valid: it '
- 'does not have a "handler" top level function.'
- , ty, module.__file__)
- return None
+ 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
# Call the "handler" function to create the actual handler
- handler = module.handler(
conf
)
+ handler = module.handler()
# Check if the handler returned is really a subclass of Handler
if not isinstance(handler, Handler):
# Check if the handler returned is really a subclass of Handler
if not isinstance(handler, Handler):
- log.error('Action handler for "%s" (in "%s) is not valid: it '
- 'does not return a subclass of backupninja.handlers.Handler.'
- , ty, module.__file__)
- return None
+ raise TypeError('%s is not valid, %s.handler did not return a '
+ 'subclass of backupninja.handlers.Handler.'
+ % (module.__file__, modname))
return handler
return handler