add dictionary formatter for translation package

This commit is contained in:
Maximilian Keßler 2022-01-09 14:15:18 +01:00
parent 9bf3daa352
commit 99c984049c
5 changed files with 89 additions and 4 deletions

View File

@ -1,6 +1,7 @@
from PyTeX.default_formatters import ClassFormatter, PackageFormatter
from PyTeX.default_formatters import ClassFormatter, PackageFormatter, DictionaryFormatter
__all__ = [
"ClassFormatter",
"PackageFormatter"
"PackageFormatter",
"DictionaryFormatter"
]

View File

@ -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:

View File

@ -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))

View File

@ -1,7 +1,9 @@
from .class_formatter import ClassFormatter
from .package_formatter import PackageFormatter
from .dictionary_formatter import DictionaryFormatter
__all__ = [
'PackageFormatter',
'ClassFormatter'
'ClassFormatter',
'DictionaryFormatter'
]

View File

@ -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