From 51981a128c7ed05e51938a3f358c0970dcc33a6f Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 21 Apr 2010 10:35:07 +0000 Subject: Moved i18n work to a branch and revert to pre-i18n-feature state via svn merge -r9054:9030 svn+ssh://repoze@svn.repoze.org/svn/repoze.bfg/trunk --- repoze/bfg/chameleon_text.py | 8 +- repoze/bfg/chameleon_zpt.py | 8 +- repoze/bfg/configuration.py | 35 ++--- repoze/bfg/i18n.py | 202 ----------------------------- repoze/bfg/interfaces.py | 16 --- repoze/bfg/tests/test_chameleon_text.py | 20 --- repoze/bfg/tests/test_chameleon_zpt.py | 20 --- repoze/bfg/tests/test_configuration.py | 21 --- repoze/bfg/tests/test_i18n.py | 219 -------------------------------- 9 files changed, 10 insertions(+), 539 deletions(-) delete mode 100644 repoze/bfg/i18n.py delete mode 100644 repoze/bfg/tests/test_i18n.py (limited to 'repoze') diff --git a/repoze/bfg/chameleon_text.py b/repoze/bfg/chameleon_text.py index deb1cc43d..f1dc8e3aa 100644 --- a/repoze/bfg/chameleon_text.py +++ b/repoze/bfg/chameleon_text.py @@ -22,7 +22,6 @@ except ImportError: # pragma: no cover from repoze.bfg.interfaces import IResponseFactory from repoze.bfg.interfaces import ITemplateRenderer -from repoze.bfg.interfaces import IChameleonTranslate from repoze.bfg.decorator import reify from repoze.bfg.renderers import template_renderer_factory @@ -51,12 +50,7 @@ class TextTemplateRenderer(object): def template(self): settings = get_settings() auto_reload = settings and settings['reload_templates'] - reg = get_current_registry() - translate = None - if reg is not None: - translate = reg.queryUtility(IChameleonTranslate) - return TextTemplateFile(self.path, auto_reload=auto_reload, - translate=translate) + return TextTemplateFile(self.path, auto_reload=auto_reload) def implementation(self): return self.template diff --git a/repoze/bfg/chameleon_zpt.py b/repoze/bfg/chameleon_zpt.py index 687a11305..f597ebd5f 100644 --- a/repoze/bfg/chameleon_zpt.py +++ b/repoze/bfg/chameleon_zpt.py @@ -13,7 +13,6 @@ except ImportError: # pragma: no cover def __init__(self, *arg, **kw): raise ImportError, exc, tb -from repoze.bfg.interfaces import IChameleonTranslate from repoze.bfg.interfaces import IResponseFactory from repoze.bfg.interfaces import ITemplateRenderer @@ -34,12 +33,7 @@ class ZPTTemplateRenderer(object): def template(self): settings = get_settings() auto_reload = settings and settings['reload_templates'] - reg = get_current_registry() - translate = None - if reg is not None: - translate = reg.queryUtility(IChameleonTranslate) - return PageTemplateFile(self.path, auto_reload=auto_reload, - translate=translate) + return PageTemplateFile(self.path, auto_reload=auto_reload) def implementation(self): return self.template diff --git a/repoze/bfg/configuration.py b/repoze/bfg/configuration.py index a98b3e7d6..4673479da 100644 --- a/repoze/bfg/configuration.py +++ b/repoze/bfg/configuration.py @@ -17,7 +17,6 @@ from zope.interface import implements from repoze.bfg.interfaces import IAuthenticationPolicy from repoze.bfg.interfaces import IAuthorizationPolicy -from repoze.bfg.interfaces import IChameleonTranslate from repoze.bfg.interfaces import IDebugLogger from repoze.bfg.interfaces import IDefaultRootFactory from repoze.bfg.interfaces import IExceptionViewClassifier @@ -32,7 +31,6 @@ from repoze.bfg.interfaces import IRoutesMapper from repoze.bfg.interfaces import ISecuredView from repoze.bfg.interfaces import ISettings from repoze.bfg.interfaces import ITemplateRenderer -from repoze.bfg.interfaces import ITranslatorFactory from repoze.bfg.interfaces import ITraverser from repoze.bfg.interfaces import IView from repoze.bfg.interfaces import IViewClassifier @@ -47,7 +45,6 @@ from repoze.bfg.events import WSGIApplicationCreatedEvent from repoze.bfg.exceptions import Forbidden from repoze.bfg.exceptions import NotFound from repoze.bfg.exceptions import ConfigurationError -from repoze.bfg.i18n import ChameleonTranslate from repoze.bfg.log import make_stream_logger from repoze.bfg.path import caller_package from repoze.bfg.registry import Registry @@ -138,23 +135,14 @@ class Configurator(object): logs to stderr will be used. If it is passed, it should be an instance of the :class:`logging.Logger` (PEP 282) standard library class. The debug logger is used by :mod:`repoze.bfg` itself to - log warnings and authorization debugging information. - - If ``translator_factory`` is passed, it should be a - :term:`translator factory` object. A translator factory is an - object which accepts a request and which returns a translation - function. The translation function accepts a :term:`translation - string` and returns a Unicode object representing the translated - string. The default for ``translator_factory`` is ``None``, - meaning that no translation is performed during template - rendering. """ + log warnings and authorization debugging information. """ manager = manager # for testing injection venusian = venusian # for testing injection def __init__(self, registry=None, package=None, settings=None, root_factory=None, authentication_policy=None, authorization_policy=None, renderers=DEFAULT_RENDERERS, - debug_logger=None, translator_factory=None): + debug_logger=None): self.package = package or caller_package() self.registry = registry if registry is None: @@ -166,8 +154,7 @@ class Configurator(object): authentication_policy=authentication_policy, authorization_policy=authorization_policy, renderers=renderers, - debug_logger=debug_logger, - translator_factory=translator_factory) + debug_logger=debug_logger) def _set_settings(self, mapping): settings = Settings(mapping or {}) @@ -283,8 +270,7 @@ class Configurator(object): def setup_registry(self, settings=None, root_factory=None, authentication_policy=None, authorization_policy=None, - renderers=DEFAULT_RENDERERS, debug_logger=None, - translator_factory=None): + renderers=DEFAULT_RENDERERS, debug_logger=None): """ When you pass a non-``None`` ``registry`` argument to the :term:`Configurator` constructor, no initial 'setup' is performed against the registry. This is because the registry @@ -318,8 +304,6 @@ class Configurator(object): self.add_renderer(name, renderer) self.set_notfound_view(default_notfound_view) self.set_forbidden_view(default_forbidden_view) - if translator_factory is not None: - self.set_translator_factory(translator_factory) # getSiteManager is a unit testing dep injection def hook_zca(self, getSiteManager=None): @@ -1241,6 +1225,10 @@ class Configurator(object): found via context-finding or ``None`` if no context could be found. The exception causing the registered view to be called is however still available as ``request.exception``. + .. warning:: This method has been deprecated in + :mod:`repoze.bfg` 1.3. See + :ref:`changing_the_forbidden_view` to see how a not found + view should be registered in :mod:`repoze.bfg` 1.3+. The ``view`` argument should be a :term:`view callable`. @@ -1311,13 +1299,6 @@ class Configurator(object): return self.add_view(bwcompat_view, context=NotFound, wrapper=wrapper, _info=_info) - def set_translator_factory(self, factory): - """ Set ``factory`` up as the current application - :term:`translator factory` (for internationalization)""" - self.registry.registerUtility(factory, ITranslatorFactory) - ctranslate = ChameleonTranslate(factory) - self.registry.registerUtility(ctranslate, IChameleonTranslate) - def add_static_view(self, name, path, cache_max_age=3600, _info=u''): """ Add a view used to render static resources to the current configuration state. diff --git a/repoze/bfg/i18n.py b/repoze/bfg/i18n.py deleted file mode 100644 index adbdb1db6..000000000 --- a/repoze/bfg/i18n.py +++ /dev/null @@ -1,202 +0,0 @@ -############################################################################## -# -# Copyright (c) 2004 Zope Corporation and Contributors. -# All Rights Reserved. -# -# This software is subject to the provisions of the Zope Public License, -# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. -# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED -# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS -# FOR A PARTICULAR PURPOSE. -# -############################################################################## - -import re - -from zope.interface import implements -from zope.interface import classProvides - -from zope.i18nmessageid import Message - -from repoze.bfg.interfaces import ITranslator -from repoze.bfg.interfaces import ITranslatorFactory -from repoze.bfg.interfaces import IChameleonTranslate - -from repoze.bfg.threadlocal import get_current_registry -from repoze.bfg.threadlocal import get_current_request - -class TranslationString(Message): - """ The constructor for a :term:`translation string`. This - constructor accepts one required argument named ``text``. - ``text`` must be the default text of the translation string, - optionally including replacement markers such as ``${foo}``. - - Optional keyword arguments to the TranslationString constructor - include ``msgid``, ``mapping`` and ``domain``. - - ``mapping``, if supplied, must be a dictionarylike object which - represents the replacement values for any replacement markers - found within the ``text`` value of this - - ``msgid`` represents an explicit :term:`message identifier` for - this translation string. Usually, the ``text`` of a translation - string serves as its message identifier. However, using this - option you can pass an explicit message identifier, usually a - simple string. This is useful when the ``text`` of a translation - string is too complicated or too long to be used as a translation - key. If ``msgid`` is ``None`` (the default), the ``msgid`` value - used by this translation string will be assumed to be the value of - ``text``. - - ``domain`` represents the :term:`translation domain`. By default, - the translation domain is ``None``, indicating that this - translation string is associated with no translation domain. - - After a translation string is constructed, its ``text`` value is - available as the ``default`` attribute of the object, the - ``msgid`` is available as the ``msgid`` attribute of the object, - the ``domain`` is available as the ``domain`` attribute, and the - ``mapping`` is available as the ``mapping`` attribute. - """ - def __new__(cls, text, mapping=None, msgid=None, domain=None): - if msgid is None: - msgid = text - return Message.__new__(cls, msgid, domain=domain, default=text, - mapping=mapping) - -class TranslationStringFactory(object): - """ Create a factory which will generate translation strings - without requiring that each call to the factory be passed a - ``domain`` value. The ``domain`` value passed to this class' - constructor will be used as the ``domain`` values of - :class:`repoze.bfg.i18n.TranslationString` objects generated by - the ``__call__`` of this class. The ``text``, ``mapping``, and - ``msgid`` values provided to ``__call__`` have the meaning as - described by the constructor of the - :class:`repoze.bfg.i18n.TranslationString`""" - def __init__(self, domain): - self.domain = domain - - def __call__(self, text, mapping=None, msgid=None): - return TranslationString(text, mapping=mapping, msgid=msgid, - domain=self.domain) - -bfg_tstr = TranslationStringFactory('bfg') -bfg_tstr.__doc__ = """\ - A :class:`repoze.bfg.i18n.TranslationStringFactory` instance with - a default ``domain`` value of ``bfg``. This object may be called - with the values ``text``, ``mapping``, and ``msgid`` as per the - documentation of the - :class:`repoze.bfg.i18n.TranslationStringFactory` class.""" - -def get_translator(request, translator_factory=None): - """ Return a :term:`translator` for the given request based on the - :term:`translator factory` registered for the current application - and the :term:`request` passed in as the request object. If no - translator factory was sent to the - :class:`repoze.bfg.configuration.Configurator` constructor at - application startup, this function will return a very simple - default 'interpolation only' translator. - - Note that the translation factory will only be constructed once - per request instance. - """ - - translator = getattr(request, '_bfg_translator', None) - - if translator is None: - - if translator_factory is None: - try: - reg = request.registry - except AttributeError: - reg = get_current_registry() - translator_factory = reg.queryUtility( - ITranslatorFactory, - default=InterpolationOnlyTranslator) - - translator = translator_factory(request) - - try: - request._bfg_translator = translator - except AttributeError: # pragma: no cover - pass # it's only a cache - - return translator - -class InterpolationOnlyTranslator(object): - - """ A class implementing the :term:`translator factory` interface - as its constructor and the :term:`translator` interface as its - ``__call__`` method. Useful as a minimal translator factory, this - class only does basic interpolation of mapping values; it does not - actually do any language translations and ignores all - :term:`translation domain` information. To use it explicitly:: - - from repoze.bfg.configuration import Configurator - from repoze.bfg.i18n import InterpolationOnlyTranslator - config = Configurator(translator_factory=InterpolationOnlyTranslator) - - An instance of this class is returned by - :func:`repoze.bfg.i18n.get_translator` if no explicit translator - factory is registered. - """ - classProvides(ITranslatorFactory) - implements(ITranslator) - def __init__(self, request): - self.request = request - - def __call__(self, message): - mapping = getattr(message, 'mapping', None) - return interpolate(message, mapping) - -class ChameleonTranslate(object): - """ Registered as a Chameleon translate function 'under the hood' - to allow our ITranslator and ITranslatorFactory to drive template - translation.""" - implements(IChameleonTranslate) - def __init__(self, translator_factory): - self.translator_factory = translator_factory - - def __call__(self, text, domain=None, mapping=None, context=None, - target_language=None, default=None): - if text is None: - return None - if default is None: - default = text - if mapping is None: - mapping = {} - if not hasattr(text, 'mapping'): - text = TranslationString(default, mapping=mapping, msgid=text, - domain=domain) - translator = self.make_translator(target_language) - return translator(text) - - def make_translator(self, target_language): - translator = None - request = get_current_request() - if request is not None: - translator = get_translator(request, self.translator_factory) - if translator is None: - translator = InterpolationOnlyTranslator(request) - return translator - -NAME_RE = r"[a-zA-Z][-a-zA-Z0-9_]*" - -_interp_regex = re.compile(r'(?