From 57208906be5e4d30986cb2f9d44d3e2915b64c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Gonz=C3=A1lez=20Alonso?= Date: Fri, 9 Feb 2024 14:00:07 +0100 Subject: [PATCH] feat: add POEditor option to ignore empty definitions (#380) --- CHANGELOG.rst | 2 + toolium/test/utils/test_dataset_map_param.py | 49 ++++++++++++++++++++ toolium/utils/dataset.py | 6 ++- toolium/utils/poeditor.py | 1 + 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a6cf137a..42178c42 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,8 @@ v3.1.4 *Release date: In development* +- Add `ignore_empty` optional parameter to POEditor configuration to ignore empty translations + v3.1.3 ------ diff --git a/toolium/test/utils/test_dataset_map_param.py b/toolium/test/utils/test_dataset_map_param.py index 817f516d..6e9e3d7f 100644 --- a/toolium/test/utils/test_dataset_map_param.py +++ b/toolium/test/utils/test_dataset_map_param.py @@ -212,6 +212,22 @@ def test_a_poe_param_single_result(): assert result == expected +def test_a_poe_param_with_empty_definition_single_result(): + """ + Verification of a POE mapped parameter with empty definition + """ + dataset.poeditor_terms = [ + { + "term": "Poniendo mute", + "definition": "", + "reference": "home:home.tv.mute", + } + ] + result = map_param('[POE:home.tv.mute]') + expected = "" + assert result == expected + + def test_a_poe_param_no_result_assertion(): """ Verification of a POE mapped parameter without result @@ -228,6 +244,39 @@ def test_a_poe_param_no_result_assertion(): assert "No translations found in POEditor for reference home.tv.off" in str(excinfo.value) +def test_a_poe_param_with_no_definition_no_result_assertion_(): + """ + Verification of a POE mapped parameter without definition and without result + """ + dataset.poeditor_terms = [ + { + "term": "Poniendo mute", + "definition": None, + "reference": "home:home.tv.mute", + } + ] + with pytest.raises(Exception) as excinfo: + map_param('[POE:home.tv.mute]') + assert "No translations found in POEditor for reference home.tv.mute" in str(excinfo.value) + + +def test_a_poe_param_with_empty_definition_no_result_assertion(): + """ + Verification of a POE mapped parameter with empty definition and without result (configured ignore_empty) + """ + dataset.project_config = {'poeditor': {'key_field': 'reference', 'search_type': 'contains', 'ignore_empty': True}} + dataset.poeditor_terms = [ + { + "term": "Poniendo mute", + "definition": "", + "reference": "home:home.tv.mute", + } + ] + with pytest.raises(Exception) as excinfo: + map_param('[POE:home.tv.mute]') + assert "No translations found in POEditor for reference home.tv.mute" in str(excinfo.value) + + def test_a_poe_param_prefix_with_no_definition(): """ Verification of a POE mapped parameter with a single result for a reference diff --git a/toolium/utils/dataset.py b/toolium/utils/dataset.py index 757e63a5..0fc75c37 100644 --- a/toolium/utils/dataset.py +++ b/toolium/utils/dataset.py @@ -690,6 +690,8 @@ def get_translation_by_poeditor_reference(reference, poeditor_terms): poeditor_config = project_config['poeditor'] if project_config and 'poeditor' in project_config else {} key = poeditor_config['key_field'] if 'key_field' in poeditor_config else 'reference' search_type = poeditor_config['search_type'] if 'search_type' in poeditor_config else 'contains' + ignore_empty = poeditor_config['ignore_empty'] if 'ignore_empty' in poeditor_config else False + ignored_definitions = [None, ''] if ignore_empty else [None] # Get POEditor prefixes and add no prefix option poeditor_prefixes = poeditor_config['prefixes'] if 'prefixes' in poeditor_config else [] poeditor_prefixes.append('') @@ -702,10 +704,10 @@ def get_translation_by_poeditor_reference(reference, poeditor_terms): complete_reference = '%s%s' % (prefix, reference) if search_type == 'exact': translation = [term['definition'] for term in poeditor_terms - if complete_reference == term[key] and term['definition'] is not None] + if complete_reference == term[key] and term['definition'] not in ignored_definitions] else: translation = [term['definition'] for term in poeditor_terms - if complete_reference in term[key] and term['definition'] is not None] + if complete_reference in term[key] and term['definition'] not in ignored_definitions] if len(translation) > 0: break assert len(translation) > 0, 'No translations found in POEditor for reference %s' % reference diff --git a/toolium/utils/poeditor.py b/toolium/utils/poeditor.py index 328d0265..b34bb039 100644 --- a/toolium/utils/poeditor.py +++ b/toolium/utils/poeditor.py @@ -46,6 +46,7 @@ "prefixes": [], "key_field": "reference", "search_type": "contains", + "ignore_empty": False, "file_path": "output/poeditor_terms.json", "mode": "online" }