Skip to content

Commit 65d1848

Browse files
author
Konstantinos Bairaktaris
committed
1 parent b87b054 commit 65d1848

6 files changed

Lines changed: 39 additions & 65 deletions

File tree

tests/native/core/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class TestNative(object):
5252

5353
def _get_tx(self, **kwargs):
5454
mytx = TxNative()
55-
mytx.init(['en', 'el'], 'cds_token', **kwargs)
55+
mytx.setup(languages=['en', 'el'], token='cds_token', **kwargs)
5656
return mytx
5757

5858
@patch('transifex.native.core.StringRenderer.render')

tests/native/core/test_daemon.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class TestFetchingDaemon(object):
1010
@patch('transifex.native.daemon.tx')
1111
def test_daemon_starts(self, patched_tx):
1212
tx = TxNative()
13-
tx.init(['en', 'el'], 'some:token', 'https://some.host')
13+
tx.setup(languages=['en', 'el'],
14+
token='some:token',
15+
cds_host='https://some.host')
1416

1517
# the `interval` we will be using
1618
interval = 1
@@ -41,7 +43,9 @@ def test_daemon_exception(self, patched_logger, patched_tx):
4143
'Something went wrong')
4244

4345
tx = TxNative()
44-
tx.init(['en', 'el'], 'some:token', 'https://some.host')
46+
tx.setup(languages=['en', 'el'],
47+
token='some:token',
48+
cds_host='https://some.host')
4549

4650
daemon = DaemonicThread()
4751

tests/native/core/test_init.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from transifex import native
21
from transifex.native import tx as _tx
32
from transifex.native.rendering import PseudoTranslationPolicy
43

@@ -7,10 +6,10 @@ class TestModuleInit(object):
76
"""Test __init__.py of root native module."""
87

98
def test_init_uses_given_params(self):
10-
native.init(
11-
'mytoken', ['lang1', 'lang2'], cds_host='myhost',
12-
missing_policy=PseudoTranslationPolicy(),
13-
)
9+
_tx.setup(token='mytoken',
10+
languages=['lang1', 'lang2'],
11+
cds_host='myhost',
12+
missing_policy=PseudoTranslationPolicy())
1413
assert _tx._cds_handler.token == 'mytoken'
1514
assert _tx._cds_handler.host == 'myhost'
1615
assert _tx._languages == ['lang1', 'lang2']

transifex/native/__init__.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
11
from transifex.native.core import TxNative
22

33

4-
def init(
5-
token, languages, secret=None,
6-
cds_host=None, missing_policy=None,
7-
error_policy=None
8-
):
9-
"""Initialize the framework.
10-
11-
:param list languages: A list of language codes for the languages
12-
the application is localized into
13-
:param str token: the API token to use for connecting to the CDS
14-
:param str secret: the additional secret required for pushing translations
15-
:param str cds_host: an optional host for the Content Delivery Service,
16-
defaults to the host provided by Transifex
17-
:param AbstractRenderingPolicy missing_policy: an optional policy to use
18-
for returning strings when a translation is missing
19-
:param AbstractErrorPolicy error_policy: an optional policy to use
20-
for defining how to handle translation rendering errors
21-
"""
22-
tx.init(
23-
languages,
24-
token,
25-
secret=secret,
26-
cds_host=cds_host,
27-
missing_policy=missing_policy,
28-
error_policy=error_policy
29-
)
30-
31-
324
tx = TxNative()
5+
t = tx.translate

transifex/native/core.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ class TxNative(object):
1414
"""The main class of the framework, responsible for orchestrating all
1515
behavior."""
1616

17-
def __init__(self):
18-
# The class uses an untypical initialization scheme, defining
19-
# an init() method, instead of initializing inside the constructor
20-
# This is necessary for allowing it to be initialized by its clients
21-
# with proper arguments, while at the same time being very easy
22-
# to import and use a single "global" instance
23-
self._cache = None
17+
def __init__(self, **kwargs):
2418
self._languages = []
25-
self._missing_policy = None
26-
self._cds_handler = None
19+
self._missing_policy = SourceStringPolicy()
20+
self._cds_handler = CDSHandler()
21+
self._cache = MemoryCache()
22+
self._error_policy = SourceStringErrorPolicy()
2723

28-
def init(
29-
self, languages, token, secret=None, cds_host=None,
30-
missing_policy=None, error_policy=None
31-
):
24+
self.setup(**kwargs)
25+
26+
def setup(self, languages=None, token=None, secret=None, cds_host=None,
27+
missing_policy=None, error_policy=None):
3228
"""Create an instance of the core framework class.
3329
3430
Also warms up the cache by fetching the translations from the CDS.
@@ -45,13 +41,17 @@ def init(
4541
:param AbstractErrorPolicy error_policy: an optional policy
4642
to determine how to handle rendering errors
4743
"""
48-
self._languages = languages
49-
self._cache = MemoryCache()
50-
self._missing_policy = missing_policy or SourceStringPolicy()
51-
self._error_policy = error_policy or SourceStringErrorPolicy()
52-
self._cds_handler = CDSHandler(configured_languages=self._languages,
53-
token=token, secret=secret,
54-
host=cds_host)
44+
if languages is not None:
45+
self._languages = languages
46+
if missing_policy is not None:
47+
self._missing_policy = missing_policy
48+
if error_policy is not None:
49+
self._error_policy = error_policy
50+
51+
self._cds_handler.setup(configured_languages=languages,
52+
token=token,
53+
secret=secret,
54+
host=cds_host)
5555

5656
def translate(
5757
self, source_string, language_code, is_source=False,

transifex/native/django/apps.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.apps import AppConfig
66
from django.core.signals import request_finished
77
from django.utils.translation import to_locale
8-
from transifex.native import init, tx
8+
from transifex.native import tx
99
from transifex.native.daemon import daemon
1010
from transifex.native.django import settings as native_settings
1111
from transifex.native.rendering import (parse_error_policy,
@@ -77,14 +77,12 @@ def ready(self):
7777
error_policy = parse_error_policy(
7878
native_settings.TRANSIFEX_ERROR_POLICY
7979
)
80-
init(
81-
native_settings.TRANSIFEX_TOKEN,
82-
languages,
83-
secret=native_settings.TRANSIFEX_SECRET,
84-
cds_host=native_settings.TRANSIFEX_CDS_HOST,
85-
missing_policy=missing_policy,
86-
error_policy=error_policy
87-
)
80+
tx.setup(token=native_settings.TRANSIFEX_TOKEN,
81+
languages=languages,
82+
secret=native_settings.TRANSIFEX_SECRET,
83+
cds_host=native_settings.TRANSIFEX_CDS_HOST,
84+
missing_policy=missing_policy,
85+
error_policy=error_policy)
8886

8987
if fetch_translations:
9088
logger.info(

0 commit comments

Comments
 (0)