From 99c984049c42231679bb508f4e8f133388cc322f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 9 Jan 2022 14:15:18 +0100 Subject: [PATCH] add dictionary formatter for translation package --- __init__.py | 5 +- build/build.py | 4 ++ build/utils/pytex_file.py | 8 ++- default_formatters/__init__.py | 4 +- default_formatters/dictionary_formatter.py | 72 ++++++++++++++++++++++ 5 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 default_formatters/dictionary_formatter.py diff --git a/__init__.py b/__init__.py index 4649aef..42cfc1e 100644 --- a/__init__.py +++ b/__init__.py @@ -1,6 +1,7 @@ -from PyTeX.default_formatters import ClassFormatter, PackageFormatter +from PyTeX.default_formatters import ClassFormatter, PackageFormatter, DictionaryFormatter __all__ = [ "ClassFormatter", - "PackageFormatter" + "PackageFormatter", + "DictionaryFormatter" ] diff --git a/build/build.py b/build/build.py index 2df2930..9a1467b 100644 --- a/build/build.py +++ b/build/build.py @@ -65,11 +65,15 @@ def build( files.append(file) for file in src_dir.rglob('*.pycls'): files.append(file) + for file in src_dir.rglob('*.pydict'): + files.append(file) else: for file in src_dir.glob('*.pysty'): files.append(file) for file in src_dir.glob('*.pycls'): files.append(file) + for file in src_dir.glob('*.pydict'): + files.append(file) sources_to_build = [] for file in files: diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py index 42a5758..e1a7221 100644 --- a/build/utils/pytex_file.py +++ b/build/utils/pytex_file.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Optional, List from PyTeX.build.git_hook import is_recent, get_latest_commit -from PyTeX import PackageFormatter, ClassFormatter +from PyTeX import PackageFormatter, ClassFormatter, DictionaryFormatter from .pytex_msg import pytex_msg from .build_information import BuildInfo @@ -86,6 +86,12 @@ class TexFileToFormat: class_name=self.src_path.with_suffix('').name, author=self.current_build_info.author, extra_header=self._header) + elif '.pydict' in self.src_path.name: + formatter = DictionaryFormatter( + kind=self.src_path.with_suffix('').name, + author=self.current_build_info.author, + header=self._header + ) else: raise Exception('Programming error. Please contact the developer.') pytex_msg('Writing file {}'.format(formatter.file_name)) diff --git a/default_formatters/__init__.py b/default_formatters/__init__.py index b32b533..21f3150 100644 --- a/default_formatters/__init__.py +++ b/default_formatters/__init__.py @@ -1,7 +1,9 @@ from .class_formatter import ClassFormatter from .package_formatter import PackageFormatter +from .dictionary_formatter import DictionaryFormatter __all__ = [ 'PackageFormatter', - 'ClassFormatter' + 'ClassFormatter', + 'DictionaryFormatter' ] diff --git a/default_formatters/dictionary_formatter.py b/default_formatters/dictionary_formatter.py new file mode 100644 index 0000000..a513916 --- /dev/null +++ b/default_formatters/dictionary_formatter.py @@ -0,0 +1,72 @@ +import datetime +from pathlib import Path +from typing import Dict, Optional, List +from datetime import * +import csv + + +class DictionaryFormatter: + def __init__(self, kind: str, author: str, header: Optional[List[str]]): + self.header = header + self.kind = kind.lower() + self.author = author + author_parts = self.author.lower().replace('ß', 'ss').split(' ') + self.author_acronym = author_parts[0][0] + author_parts[-1] + self.dictname = r'translator-{kind}-dictionary'.format( + kind=self.kind + ) + self.name_lowercase = self.dictname + '-{language}' + self.file_name = self.name_lowercase + '.dict' + self.date = datetime.now().strftime('%Y/%m/%d') + self.year = int(datetime.now().strftime('%Y')) + self.replace_dict: Dict = {} + self.arg_replace_dict: Dict = {} + self.source_file_name = "not specified" + + @property + def filename(self): + return self.file_name + + def format_file(self, input_path: Path, output_dir: Path = None): + self.source_file_name = str(input_path.name) + + if self.header: + lines = '%' * 80 + '\n' \ + + '\n'.join(map(lambda line: '% ' + line, self.header)) \ + + '\n' + '%' * 80 + '\n\n' + else: + lines = [] + if output_dir is None: + output_dir = input_path.parent + output_dir.mkdir(parents=True, exist_ok=True) + + with open(input_path, newline='') as csvfile: + spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') + langs = next(spamreader) + translations = {} + for lang in langs[1:]: + translations[lang] = {} + for line in spamreader: + for n in range(1, len(line)): + translations[langs[n]][line[0]] = line[n] + + for lang in langs[1:]: + lang_lines = lines + lang_lines += r'\ProvidesDictionary{{{dictname}}}{{{lang}}}'.format( + dictname=self.dictname, + lang=lang + ) + lang_lines += '\n' + lang_lines += '\n' + for key in translations[lang].keys(): + if translations[lang][key].strip() != '': + lang_lines += r'\providetranslation{{{key}}}{{{translation}}}'.format( + key=key.strip(), + translation=translations[lang][key].strip() + ) + lang_lines += '\n' + + (output_dir / self.file_name.format(language=lang)).write_text(''.join(lang_lines)) + + def make_default_macros(self): + pass