from pathlib import Path from string import Template from typing import Annotated from injectable import injectable, autowired, Autowired from app.services import Passwords class SimpleValueTemplate(Template): # Pozwala na kropki w nazwach placeholderów, np. ${user.name.first} idpattern = r'[_a-zA-Z][_a-zA-Z0-9.]*' class ComplexValueTemplate(SimpleValueTemplate): delimiter = '%' @injectable class Mo: @autowired def __init__(self, passwords: Annotated[Passwords, Autowired]): self._passwords = passwords def process(self, mo_file: Path): raw = '' with open(mo_file, "r") as mo: raw = mo.read() cmp = ComplexValueTemplate(raw) rendered = cmp.substitute(self._passwords.get_values(cmp.get_identifiers())) smp = SimpleValueTemplate(rendered) ids = [_id + '.password' for _id in smp.get_identifiers()] mappings = {k.replace('.password', ''): v for k, v in self._passwords.get_values(ids).items()} rendered = smp.substitute(mappings) de_mo_ified = str(mo_file).replace(".mo", "") with open(de_mo_ified, "w") as mo: mo.write(rendered)