2022-01-09 14:15:18 +01:00
|
|
|
import datetime
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Dict, Optional, List
|
|
|
|
from datetime import *
|
|
|
|
import csv
|
|
|
|
|
2022-01-12 22:30:10 +01:00
|
|
|
from PyTeX.formatter import Formatter
|
2022-01-13 20:49:26 +01:00
|
|
|
from PyTeX.utils import ensure_file_integrity
|
2022-01-09 14:15:18 +01:00
|
|
|
|
2022-01-12 22:30:10 +01:00
|
|
|
|
|
|
|
class DictionaryFormatter(Formatter):
|
2022-01-09 14:15:18 +01:00
|
|
|
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"
|
2022-01-12 22:30:10 +01:00
|
|
|
super().__init__()
|
2022-01-09 14:15:18 +01:00
|
|
|
|
2022-01-12 22:30:10 +01:00
|
|
|
def expected_file_name(self):
|
2022-01-09 14:15:18 +01:00
|
|
|
return self.file_name
|
|
|
|
|
2022-01-13 20:04:18 +01:00
|
|
|
def format_file(
|
|
|
|
self,
|
|
|
|
input_path: Path,
|
|
|
|
output_dir: Path = None,
|
|
|
|
relative_name: Optional[str] = None,
|
|
|
|
last_build_info: Optional[List[Dict]] = None
|
|
|
|
) -> List[str]:
|
2022-01-09 14:15:18 +01:00
|
|
|
self.source_file_name = str(input_path.name)
|
2022-01-13 20:04:18 +01:00
|
|
|
written_files = []
|
2022-01-09 14:15:18 +01:00
|
|
|
|
|
|
|
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'
|
2022-01-13 20:49:26 +01:00
|
|
|
ensure_file_integrity(
|
|
|
|
output_dir / self.file_name.format(language=lang),
|
|
|
|
str(Path(relative_name).parent / self.file_name.format(language=lang)),
|
|
|
|
last_build_info
|
|
|
|
)
|
2022-01-09 14:15:18 +01:00
|
|
|
(output_dir / self.file_name.format(language=lang)).write_text(''.join(lang_lines))
|
2022-01-12 22:30:10 +01:00
|
|
|
written_files.append(self.file_name.format(language=lang))
|
|
|
|
return written_files
|
2022-01-09 14:15:18 +01:00
|
|
|
|
|
|
|
def make_default_macros(self):
|
|
|
|
pass
|