Skip to content

Commit 1140d1c

Browse files
author
Konstantinos Bairaktaris
committed
Get rid of initialized
1 parent 2a43663 commit 1140d1c

5 files changed

Lines changed: 10 additions & 85 deletions

File tree

tests/native/conftest.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/native/core/test_core.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from transifex.common.utils import generate_key
66
from transifex.native.cache import MemoryCache
77
from transifex.native.cds import TRANSIFEX_CDS_HOST
8-
from transifex.native.core import NotInitializedError, TxNative
8+
from transifex.native.core import TxNative
99
from transifex.native.parsing import SourceString
1010
from transifex.native.rendering import (PseudoTranslationPolicy,
1111
SourceStringPolicy, parse_error_policy)
@@ -55,34 +55,6 @@ def _get_tx(self, **kwargs):
5555
mytx.init(['en', 'el'], 'cds_token', **kwargs)
5656
return mytx
5757

58-
def test_uninitialized(self):
59-
mytx = TxNative()
60-
with pytest.raises(NotInitializedError):
61-
mytx.translate('string', 'en')
62-
with pytest.raises(NotInitializedError):
63-
mytx.fetch_translations()
64-
with pytest.raises(NotInitializedError):
65-
mytx.push_source_strings([], False)
66-
67-
def test_default_init(self):
68-
mytx = self._get_tx()
69-
assert mytx.initialized is True
70-
assert mytx._languages == ['en', 'el']
71-
assert isinstance(mytx._missing_policy, SourceStringPolicy)
72-
assert isinstance(mytx._cache, MemoryCache)
73-
assert mytx._cds_handler.token == 'cds_token'
74-
assert mytx._cds_handler.host == TRANSIFEX_CDS_HOST
75-
76-
def test_custom_init(self):
77-
missing_policy = PseudoTranslationPolicy()
78-
mytx = self._get_tx(cds_host='myhost', missing_policy=missing_policy)
79-
assert mytx.initialized is True
80-
assert mytx._languages == ['en', 'el']
81-
assert mytx._missing_policy == missing_policy
82-
assert isinstance(mytx._cache, MemoryCache)
83-
assert mytx._cds_handler.token == 'cds_token'
84-
assert mytx._cds_handler.host == 'myhost'
85-
8658
@patch('transifex.native.core.StringRenderer.render')
8759
def test_translate_source_language_reaches_renderer(self, mock_render):
8860
mytx = self._get_tx()

tests/native/core/test_init.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,11 @@
66
class TestModuleInit(object):
77
"""Test __init__.py of root native module."""
88

9-
def test_init_uses_given_params(self, reset_tx):
9+
def test_init_uses_given_params(self):
1010
native.init(
1111
'mytoken', ['lang1', 'lang2'], cds_host='myhost',
1212
missing_policy=PseudoTranslationPolicy(),
1313
)
14-
assert _tx.initialized is True
15-
assert _tx._cds_handler.token == 'mytoken'
16-
assert _tx._cds_handler.host == 'myhost'
17-
assert _tx._languages == ['lang1', 'lang2']
18-
assert isinstance(_tx._missing_policy, PseudoTranslationPolicy)
19-
20-
def test_initialized_only_once(self, reset_tx):
21-
# Even if native.init() is called multiple times, only the first one matters
22-
native.init(
23-
'mytoken', ['lang1', 'lang2'], cds_host='myhost',
24-
missing_policy=PseudoTranslationPolicy(),
25-
)
26-
native.init('another_token', ['lang3', 'lang4'], 'another_host')
2714
assert _tx._cds_handler.token == 'mytoken'
2815
assert _tx._cds_handler.host == 'myhost'
2916
assert _tx._languages == ['lang1', 'lang2']

transifex/native/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ def init(
1919
:param AbstractErrorPolicy error_policy: an optional policy to use
2020
for defining how to handle translation rendering errors
2121
"""
22-
if not tx.initialized:
23-
tx.init(
24-
languages,
25-
token,
26-
secret=secret,
27-
cds_host=cds_host,
28-
missing_policy=missing_policy,
29-
error_policy=error_policy
30-
)
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+
)
3130

3231

3332
tx = TxNative()

transifex/native/core.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
SourceStringPolicy, StringRenderer)
1111

1212

13-
class NotInitializedError(Exception):
14-
"""Raised when a method of a TxNative instance is called but the class
15-
hasn't been initialized.
16-
17-
Allows for better debugging when developers neglect to call init().
18-
"""
19-
pass
20-
21-
2213
class TxNative(object):
2314
"""The main class of the framework, responsible for orchestrating all
2415
behavior."""
@@ -33,7 +24,6 @@ def __init__(self):
3324
self._languages = []
3425
self._missing_policy = None
3526
self._cds_handler = None
36-
self.initialized = False
3727

3828
def init(
3929
self, languages, token, secret=None, cds_host=None,
@@ -62,7 +52,6 @@ def init(
6252
self._cds_handler = CDSHandler(
6353
self._languages, token, secret=secret, host=cds_host
6454
)
65-
self.initialized = True
6655

6756
def translate(
6857
self, source_string, language_code, is_source=False,
@@ -88,8 +77,6 @@ def translate(
8877
if params is None:
8978
params = {}
9079

91-
self._check_initialization()
92-
9380
translation_template = self.get_translation(source_string,
9481
language_code,
9582
_context,
@@ -148,7 +135,6 @@ def render_translation(self, translation_template, params, source_string,
148135

149136
def fetch_translations(self):
150137
"""Fetch fresh content from the CDS."""
151-
self._check_initialization()
152138
self._cache.update(self._cds_handler.fetch_translations())
153139

154140
def push_source_strings(self, strings, purge=False):
@@ -163,16 +149,5 @@ def push_source_strings(self, strings, purge=False):
163149
response
164150
:rtype: tuple
165151
"""
166-
self._check_initialization()
167152
response = self._cds_handler.push_source_strings(strings, purge)
168153
return response.status_code, json.loads(response.content)
169-
170-
def _check_initialization(self):
171-
"""Raise an exception if the class has not been initialized.
172-
173-
:raise NotInitializedError: if the class hasn't been initialized
174-
"""
175-
if not self.initialized:
176-
raise NotInitializedError(
177-
'TxNative is not initialized, make sure you call init() first.'
178-
)

0 commit comments

Comments
 (0)