Skip to content

Commit 1929df8

Browse files
author
Konstantinos Bairaktaris
committed
Fix argument conflicts because of **params
The problem is that we have a chain of function calls like the following: ```python first("foo", language_code="bar") def first(string, **params): second(string=string, "fr", **params) def second(string, language_code, **params): # ... ``` This way, we invoke `second` by providing the `language_code` argument twice and python complains. We fix this by converting params to dict argument, where possible.
1 parent a2ad1c9 commit 1929df8

6 files changed

Lines changed: 45 additions & 35 deletions

File tree

tests/native/core/test_core.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from transifex.native.core import NotInitializedError, TxNative
99
from transifex.native.parsing import SourceString
1010
from transifex.native.rendering import (PseudoTranslationPolicy,
11-
SourceStringErrorPolicy,
1211
SourceStringPolicy, parse_error_policy)
1312

1413

@@ -94,6 +93,7 @@ def test_translate_source_language_reaches_renderer(self, mock_render):
9493
language_code='en',
9594
escape=True,
9695
missing_policy=mytx._missing_policy,
96+
params={},
9797
)
9898

9999
@patch('transifex.native.core.MemoryCache.get')
@@ -111,6 +111,7 @@ def test_translate_target_language_missing_reaches_renderer(self, mock_render,
111111
language_code='en',
112112
escape=True,
113113
missing_policy=mytx._missing_policy,
114+
params={},
114115
)
115116

116117
def test_translate_target_language_missing_reaches_missing_policy(self):
@@ -127,7 +128,7 @@ def test_translate_error_reaches_error_policy(self, mock_renderer):
127128
mytx.translate('My String', 'en', is_source=False)
128129
error_policy.get.assert_called_once_with(
129130
source_string='My String', translation=None, language_code='en',
130-
escape=True
131+
escape=True, params={},
131132
)
132133

133134
def test_translate_error_reaches_source_string_error_policy(

tests/native/core/test_rendering.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_simple_render_escaped(self):
4242
'en',
4343
escape=True,
4444
missing_policy=SourceStringPolicy(),
45-
cnt=2,
45+
params={'cnt': 2},
4646
)
4747
assert translation == (
4848
u'<script type="text/javascript">alert(1)</script>'
@@ -55,7 +55,7 @@ def test_simple_render_unescaped(self):
5555
'en',
5656
escape=False,
5757
missing_policy=SourceStringPolicy(),
58-
cnt=2,
58+
params={'cnt': 2},
5959
)
6060
assert translation == JS_SCRIPT
6161

@@ -66,7 +66,7 @@ def test_simple_render_with_translation(self):
6666
'en',
6767
escape=True,
6868
missing_policy=SourceStringPolicy(),
69-
cnt=2,
69+
params={'cnt': 2},
7070
)
7171
assert translation == u'2 τραπέζια'
7272

@@ -77,7 +77,7 @@ def test_simple_render_with_missing_translation(self):
7777
'en',
7878
escape=True,
7979
missing_policy=SourceStringPolicy(),
80-
cnt=2,
80+
params={'cnt': 2},
8181
)
8282
# Should fall back to source
8383
assert translation == u'2 tables'
@@ -88,7 +88,7 @@ def test_simple_render_with_missing_translation(self):
8888
'en',
8989
escape=True,
9090
missing_policy=PseudoTranslationPolicy(),
91-
cnt=2,
91+
params={'cnt': 2},
9292
)
9393
# Should use the proper missing policy
9494
assert translation == u'2 ťàƀĺêš'
@@ -100,7 +100,7 @@ def test_simple_render_escaped_with_missing_translation(self):
100100
'en',
101101
escape=True,
102102
missing_policy=SourceStringPolicy(),
103-
cnt=2,
103+
params={'cnt': 2},
104104
)
105105
assert translation == (
106106
u'<script type="text/javascript">alert(1)</script>'
@@ -113,7 +113,7 @@ def test_simple_render_unescaped_with_missing_translation(self):
113113
'en',
114114
escape=False,
115115
missing_policy=SourceStringPolicy(),
116-
cnt=2,
116+
params={'cnt': 2},
117117
)
118118
assert translation == JS_SCRIPT
119119

@@ -131,15 +131,15 @@ def test_complex_message_format(self):
131131
assert translation == u'Jane invites Joe and 9 other people to her party.'
132132

133133
def _complex(self, **params):
134+
params = dict(params)
135+
params.update({'host': "Jane", 'guest': "Joe"})
134136
return StringRenderer.render(
135137
COMPLEX_STRINGS,
136138
None,
137139
'en',
138140
escape=True,
139141
missing_policy=SourceStringPolicy(),
140-
host='Jane',
141-
guest='Joe',
142-
**params
142+
params=params,
143143
)
144144

145145
@patch('transifex.native.rendering.html_escape')

transifex/common/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ def pluralized(one, other, cnt_value):
8484
language_code='en',
8585
escape=False,
8686
missing_policy=None,
87-
cnt=cnt_value,
87+
params={'cnt': cnt_value},
8888
)

transifex/native/core.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def translate(
8484
:return: the rendered string
8585
:rtype: unicode
8686
"""
87+
88+
if params is None:
89+
params = {}
90+
8791
self._check_initialization()
8892

8993
translation_template = self.get_translation(source_string,
@@ -92,7 +96,7 @@ def translate(
9296
is_source)
9397

9498
return self.render_translation(translation_template,
95-
params or {},
99+
params,
96100
source_string,
97101
language_code,
98102
escape)
@@ -132,14 +136,14 @@ def render_translation(self, translation_template, params, source_string,
132136
language_code=language_code,
133137
escape=escape,
134138
missing_policy=self._missing_policy,
135-
**params
139+
params=params,
136140
)
137141
except Exception:
138142
return self._error_policy.get(
139143
source_string=source_string,
140144
translation=translation_template,
141145
language_code=language_code,
142-
escape=escape, **params
146+
escape=escape, params=params,
143147
)
144148

145149
def fetch_translations(self):

transifex/native/django/utils/__init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@
44
from transifex.native import tx
55

66

7-
def translate(string, _context=None, escape=True, **params):
7+
def translate(_string, _context=None, _escape=True, **params):
88
"""Translate the given source string to the current language.
99
1010
A convenience wrapper that uses the current language of a Django app.
1111
1212
If there are any placeholders to replace, they need to be passed as kwargs.
1313
14-
:param unicode string: the source string to get the translation for
14+
:param unicode _string: the source string to get the translation for
1515
:param unicode _context: an optional context that gives more information
1616
about the source string
17-
:param bool escape: if True, the returned string will be HTML-escaped,
17+
:param bool _escape: if True, the returned string will be HTML-escaped,
1818
otherwise it won't
1919
:return: the final translation in the current language
2020
:rtype: unicode
2121
"""
2222
is_source = get_language() == settings.LANGUAGE_CODE
2323
locale = to_locale(get_language()) # e.g. from en-us to en_US
2424
return tx.translate(
25-
string,
25+
_string,
2626
locale,
2727
_context=_context,
2828
is_source=is_source,
29-
escape=escape,
29+
escape=_escape,
3030
params=params,
3131
)
3232

3333

34-
def lazy_translate(string, _context=None, escape=True, **params):
34+
def lazy_translate(_string, _context=None, _escape=True, **params):
3535
"""Lazily translate the given source string to the current language.
3636
3737
Delays the evaluation of translating the given string until necessary.
@@ -41,21 +41,21 @@ def lazy_translate(string, _context=None, escape=True, **params):
4141
4242
See translate() for more details.
4343
44-
:param unicode string: the source string to get the translation for
44+
:param unicode _string: the source string to get the translation for
4545
:param unicode _context: an optional context that gives more information
4646
about the source string
47-
:param bool escape: if True, the returned string will be HTML-escaped,
47+
:param bool _escape: if True, the returned string will be HTML-escaped,
4848
otherwise it won't
4949
:return: an object that when evaluated as a string will return
5050
the final translation in the current language
5151
:rtype: LazyString
5252
"""
5353
return LazyString(
54-
translate, string, _context=_context, escape=escape, **params
54+
translate, _string, _context=_context, escape=_escape, **params
5555
)
5656

5757

58-
def utranslate(string, _context=None, **params):
58+
def utranslate(_string, _context=None, **params):
5959
"""Translate the given source string to the current language, without HTML
6060
escaping.
6161
@@ -66,10 +66,10 @@ def utranslate(string, _context=None, **params):
6666
6767
If there are any placeholders to replace, they need to be passed as kwargs.
6868
69-
:param unicode string: the source string to get the translation for
69+
:param unicode _string: the source string to get the translation for
7070
:param unicode _context: an optional context that gives more information
7171
about the source string
7272
:return: the final translation in the current language
7373
:rtype: unicode
7474
"""
75-
return translate(string, _context, escape=False, **params)
75+
return translate(_string, _context, escape=False, **params)

transifex/native/rendering.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class StringRenderer(object):
3737
@classmethod
3838
def render(
3939
cls, source_string, string_to_render, language_code, escape,
40-
missing_policy, **params
40+
missing_policy, params=None,
4141
):
4242
"""Render the given ICU string.
4343
@@ -61,6 +61,10 @@ def render(
6161
:return: the final rendered string
6262
:rtype: unicode
6363
"""
64+
65+
if params is None:
66+
params = {}
67+
6468
try:
6569
if not string_to_render and not missing_policy:
6670
raise Exception(
@@ -294,10 +298,8 @@ class AbstractErrorPolicy(object):
294298
Error policies define what happens when rendering faces an error.
295299
They are useful to protect the user from pages failing to load."""
296300

297-
def get(
298-
self, source_string, translation, language_code,
299-
escape, **params
300-
):
301+
def get(self, source_string, translation, language_code, escape,
302+
params=None):
301303
raise NotImplementedError()
302304

303305

@@ -311,7 +313,7 @@ def __init__(self, default_text='ERROR'):
311313

312314
def get(
313315
self, source_string, translation, language_code,
314-
escape, **params
316+
escape, params=None,
315317
):
316318
"""Try to render the source string. If something goes wrong,
317319
render a custom text provided by the user.
@@ -322,14 +324,17 @@ def get(
322324
:param bool escape: Whether to escape or not
323325
"""
324326

327+
if params is None:
328+
params = {}
329+
325330
try:
326331
return StringRenderer.render(
327332
source_string=source_string,
328333
string_to_render=source_string,
329334
language_code=language_code,
330335
escape=escape,
331336
missing_policy=None,
332-
**params
337+
params=params,
333338
)
334339
except Exception as e:
335340
logger.error(

0 commit comments

Comments
 (0)