From 2d94f8a10838859e9bc0fc51da6b1f66f21c41da Mon Sep 17 00:00:00 2001 From: juk0de Date: Mon, 5 Feb 2024 08:26:27 +0100 Subject: [PATCH] glossary: now supports quoted and unquoted entries (incl. tests) --- chatmastermind/glossary.py | 6 +++++- tests/test_glossary.py | 44 ++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/chatmastermind/glossary.py b/chatmastermind/glossary.py index b412ecf..3737fbb 100644 --- a/chatmastermind/glossary.py +++ b/chatmastermind/glossary.py @@ -58,7 +58,11 @@ class Glossary: raise GlossaryError(f"File type '{file_path.suffix}' is not supported") with open(file_path, "r") as fd: try: - data = yaml.load(fd, Loader=yaml.FullLoader) + # use BaseLoader so every entry is read as a string + # - disables automatic conversions + # - makes it possible to omit quoting for YAML keywords in entries (e. g. 'yes') + # - also correctly reads quoted entries + data = yaml.load(fd, Loader=yaml.BaseLoader) clean_entries = data['Entries'] return cls(name=data['Name'], source_lang=data['SourceLang'], diff --git a/tests/test_glossary.py b/tests/test_glossary.py index c1ac1ed..3991774 100644 --- a/tests/test_glossary.py +++ b/tests/test_glossary.py @@ -9,8 +9,10 @@ glossary_suffix: str = Glossary.file_suffix class TestGlossary(unittest.TestCase): - def test_from_file_valid_yaml(self) -> None: - # Prepare a temporary YAML file with valid content + def test_from_file_yaml_unquoted(self) -> None: + """ + Test glossary creatiom from YAML with unquoted entries. + """ with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file: yaml_file.write("Name: Sample\n" "Description: A brief description\n" @@ -20,7 +22,9 @@ class TestGlossary(unittest.TestCase): "Entries:\n" " hello: hola\n" " goodbye: adiós\n" - " 'yes': sí\n") # 'yes' is a YAML keyword and therefore quoted + # 'yes' is a YAML keyword and would normally be quoted + " yes: sí\n" + " I'm going home: me voy a casa\n") yaml_file_path = Path(yaml_file.name) glossary = Glossary.from_file(yaml_file_path) @@ -29,7 +33,39 @@ class TestGlossary(unittest.TestCase): self.assertEqual(glossary.ID, "123") self.assertEqual(glossary.source_lang, "en") self.assertEqual(glossary.target_lang, "es") - self.assertEqual(glossary.entries, {"hello": "hola", "goodbye": "adiós", "yes": "sí"}) + self.assertEqual(glossary.entries, {"hello": "hola", + "goodbye": "adiós", + "yes": "sí", + "I'm going home": "me voy a casa"}) + yaml_file_path.unlink() # Remove the temporary file + + def test_from_file_yaml_quoted(self) -> None: + """ + Test glossary creatiom from YAML with quoted entries. + """ + with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file: + yaml_file.write("Name: Sample\n" + "Description: A brief description\n" + "ID: '123'\n" + "SourceLang: en\n" + "TargetLang: es\n" + "Entries:\n" + " 'hello': 'hola'\n" + " 'goodbye': 'adiós'\n" + " 'yes': 'sí'\n" + " \"I'm going home\": 'me voy a casa'\n") + yaml_file_path = Path(yaml_file.name) + + glossary = Glossary.from_file(yaml_file_path) + self.assertEqual(glossary.name, "Sample") + self.assertEqual(glossary.desc, "A brief description") + self.assertEqual(glossary.ID, "123") + self.assertEqual(glossary.source_lang, "en") + self.assertEqual(glossary.target_lang, "es") + self.assertEqual(glossary.entries, {"hello": "hola", + "goodbye": "adiós", + "yes": "sí", + "I'm going home": "me voy a casa"}) yaml_file_path.unlink() # Remove the temporary file def test_to_file_writes_yaml(self) -> None: