From c121d0064bb7a7bd1a289ae29ceb2dee314c2d2f Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Mon, 5 Jan 2009 01:20:14 -0700 Subject: New Param: Added Param.get_label() method for a way to retrieve translated message at request time --- ipalib/parameter.py | 10 ++++++++ tests/test_ipalib/test_error2.py | 2 +- tests/test_ipalib/test_parameter.py | 49 +++++++++++++++++++++++++++++++++++-- tests/test_ipalib/test_request.py | 8 +++--- tests/util.py | 4 +-- 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/ipalib/parameter.py b/ipalib/parameter.py index 55a9bc6df..93a9a6932 100644 --- a/ipalib/parameter.py +++ b/ipalib/parameter.py @@ -23,6 +23,7 @@ Parameter system for command plugins. from types import NoneType from util import make_repr +from request import ugettext from plugable import ReadOnly, lock, check_name from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR @@ -199,6 +200,7 @@ class Param(ReadOnly): kwargs = ( ('cli_name', str, None), + ('label', callable, None), ('doc', str, ''), ('required', bool, True), ('multivalue', bool, False), @@ -301,6 +303,14 @@ class Param(ReadOnly): **self.__kw ) + def get_label(self): + """ + Return translated label using `request.ugettext`. + """ + if self.label is None: + return self.cli_name.decode('UTF-8') + return self.label(ugettext) + def normalize(self, value): """ Normalize ``value`` using normalizer callback. diff --git a/tests/test_ipalib/test_error2.py b/tests/test_ipalib/test_error2.py index a5b1e7c58..33f571ebe 100644 --- a/tests/test_ipalib/test_error2.py +++ b/tests/test_ipalib/test_error2.py @@ -23,7 +23,7 @@ Test the `ipalib.error2` module. import re import inspect -from tests.util import assert_equal, raises, DummyUGettext +from tests.util import assert_equal, raises, dummy_ugettext from ipalib import errors2 from ipalib.constants import TYPE_ERROR diff --git a/tests/test_ipalib/test_parameter.py b/tests/test_ipalib/test_parameter.py index ef248b702..63a1f4ae2 100644 --- a/tests/test_ipalib/test_parameter.py +++ b/tests/test_ipalib/test_parameter.py @@ -17,14 +17,14 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - """ Test the `ipalib.parameter` module. """ from tests.util import raises, ClassChecker, read_only +from tests.util import dummy_ugettext, assert_equal from tests.data import binary_bytes, utf8_bytes, unicode_str -from ipalib import parameter +from ipalib import parameter, request from ipalib.constants import TYPE_ERROR, CALLABLE_ERROR, NULLS @@ -137,6 +137,7 @@ class test_Param(ClassChecker): # Test default kwarg values: assert o.cli_name is name + assert o.label is None assert o.doc == '' assert o.required is True assert o.multivalue is False @@ -200,6 +201,50 @@ class test_Param(ClassChecker): o = self.cls('name', multivalue=True) assert repr(o) == "Param('name', multivalue=True)" + def test_get_label(self): + """ + Test the `ipalib.parameter.get_label` method. + """ + context = request.context + cli_name = 'the_cli_name' + message = 'The Label' + label = lambda _: _(message) + o = self.cls('name', cli_name=cli_name, label=label) + assert o.label is label + + ## Scenario 1: label=callable (a lambda form) + + # Test with no context.ugettext: + assert not hasattr(context, 'ugettext') + assert_equal(o.get_label(), u'The Label') + + # Test with dummy context.ugettext: + assert not hasattr(context, 'ugettext') + dummy = dummy_ugettext() + context.ugettext = dummy + assert o.get_label() is dummy.translation + assert dummy.message is message + del context.ugettext + + ## Scenario 2: label=None + o = self.cls('name', cli_name=cli_name) + assert o.label is None + + # Test with no context.ugettext: + assert not hasattr(context, 'ugettext') + assert_equal(o.get_label(), u'the_cli_name') + + # Test with dummy context.ugettext: + assert not hasattr(context, 'ugettext') + dummy = dummy_ugettext() + context.ugettext = dummy + assert_equal(o.get_label(), u'the_cli_name') + assert not hasattr(dummy, 'message') + + # Cleanup + del context.ugettext + assert not hasattr(context, 'ugettext') + def test_convert(self): """ Test the `ipalib.parameter.Param.convert` method. diff --git a/tests/test_ipalib/test_request.py b/tests/test_ipalib/test_request.py index 819f27554..f26c270a7 100644 --- a/tests/test_ipalib/test_request.py +++ b/tests/test_ipalib/test_request.py @@ -24,7 +24,7 @@ Test the `ipalib.request` module. import threading import locale from tests.util import raises, assert_equal -from tests.util import TempDir, DummyUGettext, DummyUNGettext +from tests.util import TempDir, dummy_ugettext, dummy_ungettext from ipalib.constants import OVERRIDE_ERROR from ipalib import request @@ -43,7 +43,7 @@ def test_ugettext(): # Test with dummy context.ugettext: assert not hasattr(context, 'ugettext') - dummy = DummyUGettext() + dummy = dummy_ugettext() context.ugettext = dummy assert f(message) is dummy.translation assert dummy.message is message @@ -69,7 +69,7 @@ def test_ungettext(): # Test singular with dummy context.ungettext assert not hasattr(context, 'ungettext') - dummy = DummyUNGettext() + dummy = dummy_ungettext() context.ungettext = dummy assert f(singular, plural, 1) is dummy.translation_singular assert dummy.singular is singular @@ -80,7 +80,7 @@ def test_ungettext(): # Test plural with dummy context.ungettext assert not hasattr(context, 'ungettext') - dummy = DummyUNGettext() + dummy = dummy_ungettext() context.ungettext = dummy assert f(singular, plural, 2) is dummy.translation_plural assert dummy.singular is singular diff --git a/tests/util.py b/tests/util.py index b0961f453..9ed10016d 100644 --- a/tests/util.py +++ b/tests/util.py @@ -287,7 +287,7 @@ class PluginTester(object): return (o, api, home) -class DummyUGettext(object): +class dummy_ugettext(object): __called = False def __init__(self): @@ -301,7 +301,7 @@ class DummyUGettext(object): return self.translation -class DummyUNGettext(object): +class dummy_ungettext(object): __called = False def __init__(self): -- cgit