glossary: added description and removed useless input stripping

This commit is contained in:
juk0de 2024-02-04 15:18:15 +01:00
parent a2ae52014b
commit 91a7541581

View File

@ -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'
@ -57,14 +59,13 @@ class Glossary:
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)
# remove any quotes from the entries that YAML may have added while dumping clean_entries = data['Entries']
# (e. g. for special keywords like 'yes')
clean_entries = {key.strip('\"\' '): value for key, value in data['Entries'].items()}
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 +87,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,