Compare commits
7 Commits
a2ae52014b
...
0ebf359647
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ebf359647 | |||
| c0b49c96b7 | |||
| 2d94f8a108 | |||
| 86f0e6d99c | |||
| 15f6e819e5 | |||
| 86295c6492 | |||
| 91a7541581 |
@ -118,6 +118,7 @@ class Config:
|
|||||||
# a default configuration
|
# a default configuration
|
||||||
cache: str = '.'
|
cache: str = '.'
|
||||||
db: str = './db/'
|
db: str = './db/'
|
||||||
|
glossaries: str | None = './glossaries/'
|
||||||
ais: dict[str, AIConfig] = field(default_factory=create_default_ai_configs)
|
ais: dict[str, AIConfig] = field(default_factory=create_default_ai_configs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -135,7 +136,8 @@ class Config:
|
|||||||
return cls(
|
return cls(
|
||||||
cache=str(source['cache']) if 'cache' in source else '.',
|
cache=str(source['cache']) if 'cache' in source else '.',
|
||||||
db=str(source['db']),
|
db=str(source['db']),
|
||||||
ais=ais
|
ais=ais,
|
||||||
|
glossaries=str(source['glossaries']) if 'glossaries' in source else None
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@ -30,9 +30,10 @@ class Glossary:
|
|||||||
"""
|
"""
|
||||||
A glossary consists of the following parameters:
|
A glossary consists of the following parameters:
|
||||||
- Name (freely selectable)
|
- Name (freely selectable)
|
||||||
- Path (full file path)
|
- Path (full file path, suffix is automatically generated)
|
||||||
- Source language
|
- Source language
|
||||||
- Target language
|
- Target language
|
||||||
|
- Description (optional)
|
||||||
- Entries (pairs of source lang and target lang terms)
|
- Entries (pairs of source lang and target lang terms)
|
||||||
- ID (automatically generated / modified, required by DeepL)
|
- ID (automatically generated / modified, required by DeepL)
|
||||||
"""
|
"""
|
||||||
@ -40,8 +41,9 @@ class Glossary:
|
|||||||
name: str
|
name: str
|
||||||
source_lang: str
|
source_lang: str
|
||||||
target_lang: str
|
target_lang: str
|
||||||
entries: dict[str, str] = field(default_factory=lambda: dict())
|
|
||||||
file_path: Path | None = None
|
file_path: Path | None = None
|
||||||
|
desc: str | None = None
|
||||||
|
entries: dict[str, str] = field(default_factory=lambda: dict())
|
||||||
ID: str | None = None
|
ID: str | None = None
|
||||||
file_suffix: ClassVar[str] = '.glo'
|
file_suffix: ClassVar[str] = '.glo'
|
||||||
|
|
||||||
@ -56,15 +58,18 @@ class Glossary:
|
|||||||
raise GlossaryError(f"File type '{file_path.suffix}' is not supported")
|
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)
|
# use BaseLoader so every entry is read as a string
|
||||||
# remove any quotes from the entries that YAML may have added while dumping
|
# - disables automatic conversions
|
||||||
# (e. g. for special keywords like 'yes')
|
# - makes it possible to omit quoting for YAML keywords in entries (e. g. 'yes')
|
||||||
clean_entries = {key.strip('\"\' '): value for key, value in data['Entries'].items()}
|
# - also correctly reads quoted entries
|
||||||
|
data = yaml.load(fd, Loader=yaml.BaseLoader)
|
||||||
|
clean_entries = data['Entries']
|
||||||
return cls(name=data['Name'],
|
return cls(name=data['Name'],
|
||||||
source_lang=data['SourceLang'],
|
source_lang=data['SourceLang'],
|
||||||
target_lang=data['TargetLang'],
|
target_lang=data['TargetLang'],
|
||||||
entries=clean_entries,
|
|
||||||
file_path=file_path,
|
file_path=file_path,
|
||||||
|
desc=data['Description'],
|
||||||
|
entries=clean_entries,
|
||||||
ID=data['ID'] if data['ID'] != 'None' else None)
|
ID=data['ID'] if data['ID'] != 'None' else None)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise GlossaryError(f"'{file_path}' does not contain a valid glossary")
|
raise GlossaryError(f"'{file_path}' does not contain a valid glossary")
|
||||||
@ -86,6 +91,7 @@ class Glossary:
|
|||||||
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)
|
||||||
data = {'Name': self.name,
|
data = {'Name': self.name,
|
||||||
|
'Description': self.desc,
|
||||||
'ID': str(self.ID),
|
'ID': str(self.ID),
|
||||||
'SourceLang': self.source_lang,
|
'SourceLang': self.source_lang,
|
||||||
'TargetLang': self.target_lang,
|
'TargetLang': self.target_lang,
|
||||||
@ -136,3 +142,20 @@ class Glossary:
|
|||||||
self.entries[parts[0]] = parts[1]
|
self.entries[parts[0]] = parts[1]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise GlossaryError(f"Error importing TSV: {e}")
|
raise GlossaryError(f"Error importing TSV: {e}")
|
||||||
|
|
||||||
|
def to_str(self, with_entries: bool = False) -> str:
|
||||||
|
"""
|
||||||
|
Return the current glossary as a string.
|
||||||
|
"""
|
||||||
|
output: list[str] = []
|
||||||
|
output.append(f'{self.name} (ID: {self.ID}):')
|
||||||
|
if self.desc:
|
||||||
|
output.append('- ' + self.desc)
|
||||||
|
output.append(f'- Languages: {self.source_lang} -> {self.target_lang}')
|
||||||
|
if with_entries:
|
||||||
|
output.append('- Entries:')
|
||||||
|
for source, target in self.entries.items():
|
||||||
|
output.append(f' {source} : {target}')
|
||||||
|
else:
|
||||||
|
output.append(f'- Entries: {len(self.entries)}')
|
||||||
|
return '\n'.join(output)
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
# vim: set fileencoding=utf-8 :
|
# vim: set fileencoding=utf-8 :
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import argcomplete
|
import argcomplete
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -156,6 +157,46 @@ def create_parser() -> argparse.ArgumentParser:
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def create_directories(config: Config) -> None: # noqa: 11
|
||||||
|
"""
|
||||||
|
Create the directories in the given configuration if they don't exist.
|
||||||
|
"""
|
||||||
|
def make_dir(path: Path) -> None:
|
||||||
|
try:
|
||||||
|
os.makedirs(path.absolute())
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Creating directory '{path.absolute()}' failed with: {e}")
|
||||||
|
sys.exit(1)
|
||||||
|
# Cache
|
||||||
|
cache_path = Path(config.cache)
|
||||||
|
if not cache_path.exists():
|
||||||
|
answer = input(f"Cache directory '{cache_path}' does not exist. Create it? [y/n]")
|
||||||
|
if answer.lower() in ['y', 'yes']:
|
||||||
|
make_dir(cache_path.absolute())
|
||||||
|
else:
|
||||||
|
print("Can't continue without a valid cache directory!")
|
||||||
|
sys.exit(1)
|
||||||
|
# DB
|
||||||
|
db_path = Path(config.db)
|
||||||
|
if not db_path.exists():
|
||||||
|
answer = input(f"DB directory '{db_path}' does not exist. Create it? [y/n]")
|
||||||
|
if answer.lower() in ['y', 'yes']:
|
||||||
|
make_dir(db_path.absolute())
|
||||||
|
else:
|
||||||
|
print("Can't continue without a valid DB directory!")
|
||||||
|
sys.exit(1)
|
||||||
|
# Glossaries
|
||||||
|
if config.glossaries:
|
||||||
|
glossaries_path = Path(config.glossaries)
|
||||||
|
if not glossaries_path.exists():
|
||||||
|
answer = input(f"Glossaries directory '{glossaries_path}' does not exist. Create it? [y/n]")
|
||||||
|
if answer.lower() in ['y', 'yes']:
|
||||||
|
make_dir(glossaries_path.absolute())
|
||||||
|
else:
|
||||||
|
print("Can't continue without a valid glossaries directory. Create it or remove it from the configuration.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
parser = create_parser()
|
parser = create_parser()
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@ -165,6 +206,7 @@ def main() -> int:
|
|||||||
command.func(command)
|
command.func(command)
|
||||||
else:
|
else:
|
||||||
config = Config.from_file(args.config)
|
config = Config.from_file(args.config)
|
||||||
|
create_directories(config)
|
||||||
command.func(command, config)
|
command.func(command, config)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@ -71,11 +71,13 @@ class TestConfig(unittest.TestCase):
|
|||||||
'frequency_penalty': 0.7,
|
'frequency_penalty': 0.7,
|
||||||
'presence_penalty': 0.2
|
'presence_penalty': 0.2
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
'glossaries': './glossaries/'
|
||||||
}
|
}
|
||||||
config = Config.from_dict(source_dict)
|
config = Config.from_dict(source_dict)
|
||||||
self.assertEqual(config.cache, '.')
|
self.assertEqual(config.cache, '.')
|
||||||
self.assertEqual(config.db, './test_db/')
|
self.assertEqual(config.db, './test_db/')
|
||||||
|
self.assertEqual(config.glossaries, './glossaries/')
|
||||||
self.assertEqual(len(config.ais), 1)
|
self.assertEqual(len(config.ais), 1)
|
||||||
self.assertEqual(config.ais['myopenai'].name, 'openai')
|
self.assertEqual(config.ais['myopenai'].name, 'openai')
|
||||||
self.assertEqual(cast(OpenAIConfig, config.ais['myopenai']).system, 'Custom system')
|
self.assertEqual(cast(OpenAIConfig, config.ais['myopenai']).system, 'Custom system')
|
||||||
@ -105,6 +107,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
'frequency_penalty': 0.7,
|
'frequency_penalty': 0.7,
|
||||||
'presence_penalty': 0.2
|
'presence_penalty': 0.2
|
||||||
}
|
}
|
||||||
|
# omit glossaries, since it's optional
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with open(self.test_file.name, 'w') as f:
|
with open(self.test_file.name, 'w') as f:
|
||||||
@ -113,6 +116,8 @@ class TestConfig(unittest.TestCase):
|
|||||||
self.assertIsInstance(config, Config)
|
self.assertIsInstance(config, Config)
|
||||||
self.assertEqual(config.cache, './test_cache/')
|
self.assertEqual(config.cache, './test_cache/')
|
||||||
self.assertEqual(config.db, './test_db/')
|
self.assertEqual(config.db, './test_db/')
|
||||||
|
# missing 'glossaries' should result in 'None'
|
||||||
|
self.assertEqual(config.glossaries, None)
|
||||||
self.assertEqual(len(config.ais), 1)
|
self.assertEqual(len(config.ais), 1)
|
||||||
self.assertIsInstance(config.ais['default'], AIConfig)
|
self.assertIsInstance(config.ais['default'], AIConfig)
|
||||||
self.assertEqual(cast(OpenAIConfig, config.ais['default']).system, 'Custom system')
|
self.assertEqual(cast(OpenAIConfig, config.ais['default']).system, 'Custom system')
|
||||||
|
|||||||
@ -9,29 +9,73 @@ 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_yaml_unquoted(self) -> None:
|
||||||
# Prepare a temporary YAML file with valid content
|
"""
|
||||||
|
Test glossary creatiom from YAML with unquoted entries.
|
||||||
|
"""
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) 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"
|
||||||
|
"Description: A brief description\n"
|
||||||
"ID: '123'\n"
|
"ID: '123'\n"
|
||||||
"SourceLang: en\n"
|
"SourceLang: en\n"
|
||||||
"TargetLang: es\n"
|
"TargetLang: es\n"
|
||||||
"Entries:\n"
|
"Entries:\n"
|
||||||
" hello: hola\n"
|
" hello: hola\n"
|
||||||
" goodbye: adiós\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)
|
yaml_file_path = Path(yaml_file.name)
|
||||||
|
|
||||||
glossary = Glossary.from_file(yaml_file_path)
|
glossary = Glossary.from_file(yaml_file_path)
|
||||||
self.assertEqual(glossary.name, "Sample")
|
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.source_lang, "en")
|
||||||
self.assertEqual(glossary.target_lang, "es")
|
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
|
yaml_file_path.unlink() # Remove the temporary file
|
||||||
|
|
||||||
def test_to_file_writes_yaml(self) -> None:
|
def test_to_file_writes_yaml(self) -> None:
|
||||||
# Create glossary instance
|
# Create glossary instance
|
||||||
glossary = Glossary(name="Test", source_lang="en", target_lang="fr", entries={"yes": "oui"})
|
glossary = Glossary(name="Test",
|
||||||
|
desc="Test description",
|
||||||
|
ID="666",
|
||||||
|
source_lang="en",
|
||||||
|
target_lang="fr",
|
||||||
|
entries={"yes": "oui"})
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) 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)
|
||||||
@ -41,6 +85,8 @@ class TestGlossary(unittest.TestCase):
|
|||||||
content = file.read()
|
content = file.read()
|
||||||
|
|
||||||
self.assertIn("Name: Test", content)
|
self.assertIn("Name: Test", content)
|
||||||
|
self.assertIn("Description: Test description", content)
|
||||||
|
self.assertIn("ID: '666'", content)
|
||||||
self.assertIn("SourceLang: en", content)
|
self.assertIn("SourceLang: en", content)
|
||||||
self.assertIn("TargetLang: fr", content)
|
self.assertIn("TargetLang: fr", content)
|
||||||
self.assertIn("Entries", content)
|
self.assertIn("Entries", content)
|
||||||
@ -115,3 +161,44 @@ class TestGlossary(unittest.TestCase):
|
|||||||
glossary.to_file(file_path)
|
glossary.to_file(file_path)
|
||||||
assert glossary.file_path is not None
|
assert glossary.file_path is not None
|
||||||
self.assertEqual(glossary.file_path.suffix, glossary_suffix)
|
self.assertEqual(glossary.file_path.suffix, glossary_suffix)
|
||||||
|
|
||||||
|
def test_to_str_with_id(self) -> None:
|
||||||
|
# Create a Glossary instance with an ID
|
||||||
|
glossary_with_id = Glossary(name="TestGlossary", source_lang="en", target_lang="fr",
|
||||||
|
desc="A simple test glossary", ID="1001", entries={"one": "un"})
|
||||||
|
glossary_str = glossary_with_id.to_str()
|
||||||
|
self.assertIn("TestGlossary (ID: 1001):", glossary_str)
|
||||||
|
self.assertIn("- A simple test glossary", glossary_str)
|
||||||
|
self.assertIn("- Languages: en -> fr", glossary_str)
|
||||||
|
self.assertIn("- Entries: 1", glossary_str)
|
||||||
|
|
||||||
|
def test_to_str_with_id_and_entries(self) -> None:
|
||||||
|
# Create a Glossary instance with an ID and include entries
|
||||||
|
glossary_with_entries = Glossary(name="TestGlossaryWithEntries", source_lang="en", target_lang="fr",
|
||||||
|
desc="Another test glossary", ID="2002",
|
||||||
|
entries={"hello": "salut", "goodbye": "au revoir"})
|
||||||
|
glossary_str_with_entries = glossary_with_entries.to_str(with_entries=True)
|
||||||
|
self.assertIn("TestGlossaryWithEntries (ID: 2002):", glossary_str_with_entries)
|
||||||
|
self.assertIn("- Entries:", glossary_str_with_entries)
|
||||||
|
self.assertIn(" hello : salut", glossary_str_with_entries)
|
||||||
|
self.assertIn(" goodbye : au revoir", glossary_str_with_entries)
|
||||||
|
|
||||||
|
def test_to_str_without_id(self) -> None:
|
||||||
|
# Create a Glossary instance without an ID
|
||||||
|
glossary_without_id = Glossary(name="TestGlossaryNoID", source_lang="en", target_lang="fr",
|
||||||
|
desc="A test glossary without an ID", ID=None, entries={"yes": "oui"})
|
||||||
|
glossary_str_no_id = glossary_without_id.to_str()
|
||||||
|
self.assertIn("TestGlossaryNoID (ID: None):", glossary_str_no_id)
|
||||||
|
self.assertIn("- A test glossary without an ID", glossary_str_no_id)
|
||||||
|
self.assertIn("- Languages: en -> fr", glossary_str_no_id)
|
||||||
|
self.assertIn("- Entries: 1", glossary_str_no_id)
|
||||||
|
|
||||||
|
def test_to_str_without_id_and_no_entries(self) -> None:
|
||||||
|
# Create a Glossary instance without an ID and no entries
|
||||||
|
glossary_no_id_no_entries = Glossary(name="EmptyGlossary", source_lang="en", target_lang="fr",
|
||||||
|
desc="An empty test glossary", ID=None, entries={})
|
||||||
|
glossary_str_no_id_no_entries = glossary_no_id_no_entries.to_str()
|
||||||
|
self.assertIn("EmptyGlossary (ID: None):", glossary_str_no_id_no_entries)
|
||||||
|
self.assertIn("- An empty test glossary", glossary_str_no_id_no_entries)
|
||||||
|
self.assertIn("- Languages: en -> fr", glossary_str_no_id_no_entries)
|
||||||
|
self.assertIn("- Entries: 0", glossary_str_no_id_no_entries)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user