glossary: added description and removed useless input stripping

This commit is contained in:
juk0de 2024-02-04 15:18:15 +01:00
parent ff1e405991
commit b4ef2e43ca

View File

@ -30,9 +30,10 @@ class Glossary:
"""
A glossary consists of the following parameters:
- Name (freely selectable)
- Path (full file path)
- Path (full file path, suffix is automatically generated)
- Source language
- Target language
- Description (optional)
- Entries (pairs of source lang and target lang terms)
- ID (automatically generated / modified, required by DeepL)
"""
@ -40,8 +41,9 @@ class Glossary:
name: str
source_lang: str
target_lang: str
entries: dict[str, str] = field(default_factory=lambda: dict())
file_path: Path | None = None
desc: str | None = None
entries: dict[str, str] = field(default_factory=lambda: dict())
ID: str | None = None
file_suffix: ClassVar[str] = '.glo'
@ -57,14 +59,13 @@ class Glossary:
with open(file_path, "r") as fd:
try:
data = yaml.load(fd, Loader=yaml.FullLoader)
# remove any quotes from the entries that YAML may have added while dumping
# (e. g. for special keywords like 'yes')
clean_entries = {key.strip('\"\' '): value for key, value in data['Entries'].items()}
clean_entries = data['Entries']
return cls(name=data['Name'],
source_lang=data['SourceLang'],
target_lang=data['TargetLang'],
entries=clean_entries,
file_path=file_path,
desc=data['Description'],
entries=clean_entries,
ID=data['ID'] if data['ID'] != 'None' else None)
except Exception:
raise GlossaryError(f"'{file_path}' does not contain a valid glossary")
@ -86,6 +87,7 @@ class Glossary:
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)
data = {'Name': self.name,
'Description': str(self.desc),
'ID': str(self.ID),
'SourceLang': self.source_lang,
'TargetLang': self.target_lang,