diff options
Diffstat (limited to 'mediagoblin/tools')
-rw-r--r-- | mediagoblin/tools/pluginapi.py | 112 | ||||
-rw-r--r-- | mediagoblin/tools/processing.py | 2 | ||||
-rw-r--r-- | mediagoblin/tools/routing.py | 3 | ||||
-rw-r--r-- | mediagoblin/tools/template.py | 5 | ||||
-rw-r--r-- | mediagoblin/tools/translate.py | 29 |
5 files changed, 98 insertions, 53 deletions
diff --git a/mediagoblin/tools/pluginapi.py b/mediagoblin/tools/pluginapi.py index 283350a8..3f98aa8a 100644 --- a/mediagoblin/tools/pluginapi.py +++ b/mediagoblin/tools/pluginapi.py @@ -274,68 +274,94 @@ def get_hook_templates(hook_name): return PluginManager().get_template_hooks(hook_name) -########################### -# Callable convenience code -########################### +############################# +## Hooks: The Next Generation +############################# -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. +def hook_handle(hook_name, *args, **kwargs): """ - pass + Run through hooks attempting to find one that handle this hook. + All callables called with the same arguments until one handles + things and returns a non-None value. -def callable_runone(hookname, *args, **kwargs): - """ - Run the callable hook HOOKNAME... run until the first response, - then return. + (If you are writing a handler and you don't have a particularly + useful value to return even though you've handled this, returning + True is a good solution.) - This function will run stop at the first hook that handles the - result. Hooks raising CantHandleIt will be skipped. + Note that there is a special keyword argument: + if "default_handler" is passed in as a keyword argument, this will + be used if no handler is found. - Unless unhandled_okay is True, this will error out if no hooks - have been registered to handle this function. + Some examples of using this: + - You need an interface implemented, but only one fit for it + - You need to *do* something, but only one thing needs to do it. """ - callables = PluginManager().get_hook_callables(hookname) + default_handler = kwargs.pop('default_handler', None) + + callables = PluginManager().get_hook_callables(hook_name) - unhandled_okay = kwargs.pop("unhandled_okay", False) + result = None for callable in callables: - try: - return callable(*args, **kwargs) - except CantHandleIt: - continue + result = callable(*args, **kwargs) - if unhandled_okay is False: - raise UnhandledCallable( - "No hooks registered capable of handling '%s'" % hookname) + if result is not None: + break + if result is None and default_handler is not None: + result = default_handler(*args, **kwargs) + + return result -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. +def hook_runall(hook_name, *args, **kwargs): + """ + Run through all callable hooks and pass in arguments. + + All non-None results are accrued in a list and returned from this. + (Other "false-like" values like False and friends are still + accrued, however.) + + Some examples of using this: + - You have an interface call where actually multiple things can + and should implement it + - You need to get a list of things from various plugins that + handle them and do something with them + - You need to *do* something, and actually multiple plugins need + to do it separately """ - callables = PluginManager().get_hook_callables(hookname) + callables = PluginManager().get_hook_callables(hook_name) results = [] for callable in callables: - try: - results.append(callable(*args, **kwargs)) - except CantHandleIt: - continue + result = callable(*args, **kwargs) + + if result is not None: + results.append(result) return results + + +def hook_transform(hook_name, arg): + """ + Run through a bunch of hook callables and transform some input. + + Note that unlike the other hook tools, this one only takes ONE + argument. This argument is passed to each function, which in turn + returns something that becomes the input of the next callable. + + Some examples of using this: + - You have an object, say a form, but you want plugins to each be + able to modify it. + """ + result = arg + + callables = PluginManager().get_hook_callables(hook_name) + + for callable in callables: + result = callable(result) + + return result diff --git a/mediagoblin/tools/processing.py b/mediagoblin/tools/processing.py index cff4cb9d..2abe6452 100644 --- a/mediagoblin/tools/processing.py +++ b/mediagoblin/tools/processing.py @@ -21,8 +21,6 @@ import traceback from urllib2 import urlopen, Request, HTTPError from urllib import urlencode -from mediagoblin.tools.common import TESTS_ENABLED - _log = logging.getLogger(__name__) TESTS_CALLBACKS = {} diff --git a/mediagoblin/tools/routing.py b/mediagoblin/tools/routing.py index 791cd1e6..a15795fe 100644 --- a/mediagoblin/tools/routing.py +++ b/mediagoblin/tools/routing.py @@ -16,6 +16,7 @@ import logging +import six from werkzeug.routing import Map, Rule from mediagoblin.tools.common import import_component @@ -43,7 +44,7 @@ def endpoint_to_controller(rule): _log.debug('endpoint: {0} view_func: {1}'.format(endpoint, view_func)) # import the endpoint, or if it's already a callable, call that - if isinstance(view_func, basestring): + if isinstance(view_func, six.string_types): view_func = import_component(view_func) rule.gmg_controller = view_func diff --git a/mediagoblin/tools/template.py b/mediagoblin/tools/template.py index 78d65654..54aeac92 100644 --- a/mediagoblin/tools/template.py +++ b/mediagoblin/tools/template.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from math import ceil import jinja2 from jinja2.ext import Extension @@ -27,7 +26,7 @@ from mediagoblin import mg_globals from mediagoblin import messages from mediagoblin import _version from mediagoblin.tools import common -from mediagoblin.tools.translate import get_gettext_translation +from mediagoblin.tools.translate import set_thread_locale from mediagoblin.tools.pluginapi import get_hook_templates from mediagoblin.tools.timesince import timesince from mediagoblin.meddleware.csrf import render_csrf_form_token @@ -44,7 +43,7 @@ def get_jinja_env(template_loader, locale): (In the future we may have another system for providing theming; for now this is good enough.) """ - mg_globals.thread_scope.translations = get_gettext_translation(locale) + set_thread_locale(locale) # If we have a jinja environment set up with this locale, just # return that one. diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py index 4acafac7..b20e57d1 100644 --- a/mediagoblin/tools/translate.py +++ b/mediagoblin/tools/translate.py @@ -42,6 +42,22 @@ def set_available_locales(): AVAILABLE_LOCALES = locales +class ReallyLazyProxy(LazyProxy): + """ + Like LazyProxy, except that it doesn't cache the value ;) + """ + @property + def value(self): + return self._func(*self._args, **self._kwargs) + + def __repr__(self): + return "<%s for %s(%r, %r)>" % ( + self.__class__.__name__, + self._func, + self._args, + self._kwargs) + + def locale_to_lower_upper(locale): """ Take a locale, regardless of style, and format it like "en_US" @@ -112,6 +128,11 @@ def get_gettext_translation(locale): return this_gettext +def set_thread_locale(locale): + """Set the current translation for this thread""" + mg_globals.thread_scope.translations = get_gettext_translation(locale) + + def pass_to_ugettext(*args, **kwargs): """ Pass a translation on to the appropriate ugettext method. @@ -122,7 +143,6 @@ def pass_to_ugettext(*args, **kwargs): return mg_globals.thread_scope.translations.ugettext( *args, **kwargs) - def pass_to_ungettext(*args, **kwargs): """ Pass a translation on to the appropriate ungettext method. @@ -133,6 +153,7 @@ def pass_to_ungettext(*args, **kwargs): return mg_globals.thread_scope.translations.ungettext( *args, **kwargs) + def lazy_pass_to_ugettext(*args, **kwargs): """ Lazily pass to ugettext. @@ -144,7 +165,7 @@ def lazy_pass_to_ugettext(*args, **kwargs): you would want to use the lazy version for _. """ - return LazyProxy(pass_to_ugettext, *args, **kwargs) + return ReallyLazyProxy(pass_to_ugettext, *args, **kwargs) def pass_to_ngettext(*args, **kwargs): @@ -166,7 +187,7 @@ def lazy_pass_to_ngettext(*args, **kwargs): level but you need it to not translate until the time that it's used as a string. """ - return LazyProxy(pass_to_ngettext, *args, **kwargs) + return ReallyLazyProxy(pass_to_ngettext, *args, **kwargs) def lazy_pass_to_ungettext(*args, **kwargs): """ @@ -176,7 +197,7 @@ def lazy_pass_to_ungettext(*args, **kwargs): level but you need it to not translate until the time that it's used as a string. """ - return LazyProxy(pass_to_ungettext, *args, **kwargs) + return ReallyLazyProxy(pass_to_ungettext, *args, **kwargs) def fake_ugettext_passthrough(string): |