diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2013-04-10 17:53:05 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2013-04-10 17:53:05 -0500 |
commit | 761e26bb29623f58ebf2496b7fd7e945fad4e6ed (patch) | |
tree | 4ec1003f12b2dd38d90ad23f5430208bf74eebcd /mediagoblin/tools/pluginapi.py | |
parent | 0d656f44a1252cc8cdc8ec05be33187db3470e5d (diff) | |
parent | 04f295e20db4f8b170d2b0ec21eea850f7aa2bd5 (diff) | |
download | mediagoblin-761e26bb29623f58ebf2496b7fd7e945fad4e6ed.tar.lz mediagoblin-761e26bb29623f58ebf2496b7fd7e945fad4e6ed.tar.xz mediagoblin-761e26bb29623f58ebf2496b7fd7e945fad4e6ed.zip |
Merge branch '637_friendlier_hooks'
Diffstat (limited to 'mediagoblin/tools/pluginapi.py')
-rw-r--r-- | mediagoblin/tools/pluginapi.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/mediagoblin/tools/pluginapi.py b/mediagoblin/tools/pluginapi.py index 784bede9..283350a8 100644 --- a/mediagoblin/tools/pluginapi.py +++ b/mediagoblin/tools/pluginapi.py @@ -272,3 +272,70 @@ def get_hook_templates(hook_name): A list of strings representing template paths. """ return PluginManager().get_template_hooks(hook_name) + + +########################### +# Callable convenience code +########################### + +class CantHandleIt(Exception): + """ + A callable may call this method if they look at the relevant + arguments passed and decide it's not possible for them to handle + things. + """ + pass + +class UnhandledCallable(Exception): + """ + Raise this method if no callables were available to handle the + specified hook. Only used by callable_runone. + """ + pass + + +def callable_runone(hookname, *args, **kwargs): + """ + Run the callable hook HOOKNAME... run until the first response, + then return. + + This function will run stop at the first hook that handles the + result. Hooks raising CantHandleIt will be skipped. + + Unless unhandled_okay is True, this will error out if no hooks + have been registered to handle this function. + """ + callables = PluginManager().get_hook_callables(hookname) + + unhandled_okay = kwargs.pop("unhandled_okay", False) + + for callable in callables: + try: + return callable(*args, **kwargs) + except CantHandleIt: + continue + + if unhandled_okay is False: + raise UnhandledCallable( + "No hooks registered capable of handling '%s'" % hookname) + + +def callable_runall(hookname, *args, **kwargs): + """ + Run all callables for HOOKNAME. + + This method will run *all* hooks that handle this method (skipping + those that raise CantHandleIt), and will return a list of all + results. + """ + callables = PluginManager().get_hook_callables(hookname) + + results = [] + + for callable in callables: + try: + results.append(callable(*args, **kwargs)) + except CantHandleIt: + continue + + return results |