Simple/Complex templates / parsers
This commit is contained in:
parent
e3a37419e8
commit
3dc27cc868
4 changed files with 38 additions and 19 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
value: ${sample.password}
|
||||
nested: ${some.nested.value.password}
|
||||
custom: ${custom.field}
|
||||
value: ${sample}
|
||||
nested: ${some.nested.value}
|
||||
custom: @{custom.field}
|
||||
|
|
|
|||
|
|
@ -10,18 +10,14 @@ from app.services.mo import Mo
|
|||
|
||||
class TestMo(TestCase):
|
||||
def test_process(self):
|
||||
target_path = Path('tests/files/test1/test.yaml')
|
||||
mo = Mo(Passwords())
|
||||
mo.process(Path('tests/files/test1/test.mo.yaml').absolute())
|
||||
self.assertTrue(os.path.exists('tests/files/test1/test.mo'))
|
||||
with open('tests/files/test1/test.yaml', 'r') as f:
|
||||
self.assertTrue(os.path.exists(target_path))
|
||||
with open(target_path, 'r') as f:
|
||||
content = f.read()
|
||||
self.assertFalse(content.__contains__('${'))
|
||||
parsed = yaml.load(content, Loader=yaml.FullLoader)
|
||||
self.assertEqual(parsed['value'], 'some_oass')
|
||||
self.assertEqual(parsed['nested'], 'nested_pass')
|
||||
self.assertEqual(parsed['custom'], 'custom_content')
|
||||
"""
|
||||
value: some_oass
|
||||
nested: nested_pass
|
||||
custom: custom_content
|
||||
"""
|
||||
self.assertEqual('some_pass', parsed['value'])
|
||||
self.assertEqual('nested_pass', parsed['nested'])
|
||||
self.assertEqual('custom_content', parsed['custom'])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue