Compare commits
2 Commits
85315d9c1c
...
a2ae52014b
| Author | SHA1 | Date | |
|---|---|---|---|
| a2ae52014b | |||
| 6e89e014bf |
@ -7,7 +7,7 @@ import shutil
|
|||||||
import csv
|
import csv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Type, TypeVar
|
from typing import Type, TypeVar, ClassVar
|
||||||
|
|
||||||
GlossaryInst = TypeVar('GlossaryInst', bound='Glossary')
|
GlossaryInst = TypeVar('GlossaryInst', bound='Glossary')
|
||||||
|
|
||||||
@ -43,12 +43,17 @@ class Glossary:
|
|||||||
entries: dict[str, str] = field(default_factory=lambda: dict())
|
entries: dict[str, str] = field(default_factory=lambda: dict())
|
||||||
file_path: Path | None = None
|
file_path: Path | None = None
|
||||||
ID: str | None = None
|
ID: str | None = None
|
||||||
|
file_suffix: ClassVar[str] = '.glo'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_file(cls: Type[GlossaryInst], file_path: Path) -> GlossaryInst:
|
def from_file(cls: Type[GlossaryInst], file_path: Path) -> GlossaryInst:
|
||||||
"""
|
"""
|
||||||
Create a glossary from the given file.
|
Create a glossary from the given file.
|
||||||
"""
|
"""
|
||||||
|
if not file_path.exists():
|
||||||
|
raise GlossaryError(f"Glossary file '{file_path}' does not exist")
|
||||||
|
if file_path.suffix != cls.file_suffix:
|
||||||
|
raise GlossaryError(f"File type '{file_path.suffix}' is not supported")
|
||||||
with open(file_path, "r") as fd:
|
with open(file_path, "r") as fd:
|
||||||
try:
|
try:
|
||||||
data = yaml.load(fd, Loader=yaml.FullLoader)
|
data = yaml.load(fd, Loader=yaml.FullLoader)
|
||||||
@ -72,6 +77,11 @@ class Glossary:
|
|||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
if not self.file_path:
|
if not self.file_path:
|
||||||
raise GlossaryError("Got no valid path to write glossary")
|
raise GlossaryError("Got no valid path to write glossary")
|
||||||
|
# check / add valid suffix
|
||||||
|
if not self.file_path.suffix:
|
||||||
|
self.file_path = self.file_path.with_suffix(self.file_suffix)
|
||||||
|
elif self.file_path.suffix != self.file_suffix:
|
||||||
|
raise GlossaryError(f"File suffix '{self.file_path.suffix}' is not supported")
|
||||||
# write YAML
|
# write YAML
|
||||||
with tempfile.NamedTemporaryFile(dir=self.file_path.parent, prefix=self.file_path.name, mode="w", delete=False) as temp_fd:
|
with tempfile.NamedTemporaryFile(dir=self.file_path.parent, prefix=self.file_path.name, mode="w", delete=False) as temp_fd:
|
||||||
temp_file_path = Path(temp_fd.name)
|
temp_file_path = Path(temp_fd.name)
|
||||||
|
|||||||
@ -1,14 +1,17 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import tempfile
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from chatmastermind.glossary import Glossary
|
from chatmastermind.glossary import Glossary, GlossaryError
|
||||||
|
|
||||||
|
|
||||||
|
glossary_suffix: str = Glossary.file_suffix
|
||||||
|
|
||||||
|
|
||||||
class TestGlossary(unittest.TestCase):
|
class TestGlossary(unittest.TestCase):
|
||||||
|
|
||||||
def test_from_file_valid_yaml(self) -> None:
|
def test_from_file_valid_yaml(self) -> None:
|
||||||
# Prepare a temporary YAML file with valid content
|
# Prepare a temporary YAML file with valid content
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False) as yaml_file:
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file:
|
||||||
yaml_file.write("Name: Sample\n"
|
yaml_file.write("Name: Sample\n"
|
||||||
"ID: '123'\n"
|
"ID: '123'\n"
|
||||||
"SourceLang: en\n"
|
"SourceLang: en\n"
|
||||||
@ -30,7 +33,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
# Create glossary instance
|
# Create glossary instance
|
||||||
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False) as tmp_file:
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file:
|
||||||
file_path = Path(tmp_file.name)
|
file_path = Path(tmp_file.name)
|
||||||
glossary.to_file(file_path)
|
glossary.to_file(file_path)
|
||||||
|
|
||||||
@ -50,7 +53,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
# -> use 'yes' in order to test if the YAML quoting is correctly removed when reading the file
|
# -> use 'yes' in order to test if the YAML quoting is correctly removed when reading the file
|
||||||
glossary_write = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
glossary_write = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False) as tmp_file:
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tmp_file:
|
||||||
file_path = Path(tmp_file.name)
|
file_path = Path(tmp_file.name)
|
||||||
glossary_write.to_file(file_path)
|
glossary_write.to_file(file_path)
|
||||||
|
|
||||||
@ -67,7 +70,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={})
|
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={})
|
||||||
|
|
||||||
# First export to CSV
|
# First export to CSV
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False) as csvfile:
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as csvfile:
|
||||||
csv_file_path = Path(csvfile.name)
|
csv_file_path = Path(csvfile.name)
|
||||||
glossary.entries = {"hello": "salut", "goodbye": "au revoir"}
|
glossary.entries = {"hello": "salut", "goodbye": "au revoir"}
|
||||||
glossary.export_csv(glossary.entries, csv_file_path)
|
glossary.export_csv(glossary.entries, csv_file_path)
|
||||||
@ -81,7 +84,7 @@ class TestGlossary(unittest.TestCase):
|
|||||||
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={})
|
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={})
|
||||||
|
|
||||||
# First export to TSV
|
# First export to TSV
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False) as tsvfile:
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as tsvfile:
|
||||||
tsv_file_path = Path(tsvfile.name)
|
tsv_file_path = Path(tsvfile.name)
|
||||||
glossary.entries = {"hello": "salut", "goodbye": "au revoir"}
|
glossary.entries = {"hello": "salut", "goodbye": "au revoir"}
|
||||||
glossary.export_tsv(glossary.entries, tsv_file_path)
|
glossary.export_tsv(glossary.entries, tsv_file_path)
|
||||||
@ -90,3 +93,25 @@ class TestGlossary(unittest.TestCase):
|
|||||||
glossary.import_tsv(tsv_file_path)
|
glossary.import_tsv(tsv_file_path)
|
||||||
self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"})
|
self.assertEqual(glossary.entries, {"hello": "salut", "goodbye": "au revoir"})
|
||||||
tsv_file_path.unlink() # Remove the temporary file
|
tsv_file_path.unlink() # Remove the temporary file
|
||||||
|
|
||||||
|
def test_to_file_wrong_suffix(self) -> None:
|
||||||
|
"""
|
||||||
|
Test for exception if suffix is wrong.
|
||||||
|
"""
|
||||||
|
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
||||||
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.wrong') as tmp_file:
|
||||||
|
file_path = Path(tmp_file.name)
|
||||||
|
with self.assertRaises(GlossaryError) as err:
|
||||||
|
glossary.to_file(file_path)
|
||||||
|
self.assertEqual(str(err.exception), "File suffix '.wrong' is not supported")
|
||||||
|
|
||||||
|
def test_to_file_auto_suffix(self) -> None:
|
||||||
|
"""
|
||||||
|
Test if suffix is auto-generated if omitted.
|
||||||
|
"""
|
||||||
|
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
||||||
|
with tempfile.NamedTemporaryFile('w', delete=False, suffix='') as tmp_file:
|
||||||
|
file_path = Path(tmp_file.name)
|
||||||
|
glossary.to_file(file_path)
|
||||||
|
assert glossary.file_path is not None
|
||||||
|
self.assertEqual(glossary.file_path.suffix, glossary_suffix)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user