Simple/Complex templates / parsers

This commit is contained in:
Piotr Dec 2025-11-03 21:56:59 +01:00
parent e3a37419e8
commit 3dc27cc868
Signed by: stawros
GPG key ID: 74B18A3F0F1E99C0
4 changed files with 38 additions and 19 deletions

View file

@ -6,11 +6,16 @@ from injectable import injectable, autowired, Autowired
from app.services import Passwords
class DotTemplate(Template):
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
@ -21,8 +26,12 @@ class Mo:
raw = ''
with open(mo_file, "r") as mo:
raw = mo.read()
tpl = DotTemplate(raw)
rendered = tpl.substitute(self._passwords.get_values(tpl.get_identifiers()))
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)

View file

@ -33,13 +33,27 @@ class Passwords:
output = {}
for k in keys:
key_parts = k.split(".")
path = key_parts[:-2] if len(key_parts) > 2 else None
path = key_parts[:-1] if len(key_parts) > 2 else None
entry_name = key_parts[-2]
field_name = key_parts[-1]
kp_entry = self._kp_org.find_entries(path=path, first=True, title=entry_name)[0]
output[k] = kp_entry[field_name] # TODO: TypeError: 'Entry' object is not subscriptable
kp_entry = self._kp_org.find_entries(path=path, first=True, title=entry_name)
output[k] = self._get_field_value(kp_entry, field_name)
return output
@staticmethod
def _get_field_value(kp_entry, field_name):
if kp_entry is None:
return None
match field_name:
case "username":
return kp_entry.username
case "password":
return kp_entry.password
case "url":
return kp_entry.url
case _:
return kp_entry.get_custom_property(field_name)
def save(self):
# nadpisz plik źródłowy zmianami z lock
self._kp.save()