From d15ee60ca7ad62004c5e036203f095cc27660f90 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Sun, 10 Apr 2022 22:21:41 +0200 Subject: Introduce a new macro (%p) representing the parent value of a given key --- README.md | 16 +++++++++------- src/etoolkit/etoolkit.py | 6 ++++++ tests/conftest.py | 15 +++++++-------- tests/test_cli.py | 4 +--- tests/test_envtoolkit_instance.py | 13 +++++++++++-- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a4efa6f..ae52d21 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,9 @@ corresponding values. Currently the following macros are supported: - **%h** - the home directory of the user running *etoolkit* (~/) -- **%i** - the names of the instance that is about to be loaded +- **%i** - the name of the instance that is about to be loaded + +- **%p** - the parent value (for the same key) - **%u** - the username of the user running *etoolkit* (*getpass.getuser()*) @@ -234,16 +236,16 @@ or the *instances* structure being loaded from a diferent configuration file "general": { }, "instances": { - "default": { - "ETOOLKIT_PROMPT": "(%i)" + "_default": { + "ETOOLKIT_PROMPT": "(%i)", + "PYTHONPATH": "/home/user/%i/python", }, "dev": { - "ETOOLKIT_PARENT": "default", - "PYTHONPATH": ":/home/user/.pythonpath", - "DB_CONNECTION": "enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT5tS9o+ABvsxogIXpJim16Gz5SVtV8=" + "ETOOLKIT_PARENT": "_default", + "PYTHONPATH": "%p:/home/user/%i/.pythonpath", }, "secret": { - "ETOOLKIT_PARENT": "default", + "ETOOLKIT_PARENT": "_default", "ETOOLKIT_SENSITIVE": ["PASSWORD"], "GNUPGHOME": "%h/private/.gnupg", "PASSWORD": "enc-val$1$vIBcoCNiYrsDLtF41uLuSEnppBjhliD0B8jwcBJcj/c=$KwOGe/y1dlxktDaCnJPIVNuaQ4Q7yNo=" diff --git a/src/etoolkit/etoolkit.py b/src/etoolkit/etoolkit.py index d8221a5..9b95a73 100755 --- a/src/etoolkit/etoolkit.py +++ b/src/etoolkit/etoolkit.py @@ -342,6 +342,12 @@ class EtoolkitInstance: if not value: # perhaps unset instead of skipping? continue + if isinstance(value, str) and '%p' in value: + macros['%p'] = ( + self._parent.get_environ().get(key, '') + if self._parent is not None + else '' + ) if isinstance(value, str) and value.startswith('enc-val$1$'): value = self._decrypt_value(value) if isinstance(value, str) and value.endswith(':'): diff --git a/tests/conftest.py b/tests/conftest.py index 0770adb..5dcc456 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,17 +30,16 @@ def config_data(): ) }, 'instances': { - 'default': {'ETOOLKIT_PROMPT': '(%i)'}, + '_default': { + 'ETOOLKIT_PROMPT': '(%i)', + 'PYTHONPATH': '/home/foo/%i/python', + }, 'dev': { - 'ETOOLKIT_PARENT': 'default', - 'PYTHONPATH': ':/home/user/.pythonpath', - 'DB_CONNECTION': ( - 'enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT' - '5tS9o+ABvsxogIXpJim16Gz5SVtV8=' - ), + 'ETOOLKIT_PARENT': '_default', + 'PYTHONPATH': '%p:/home/user/%i/.pythonpath', }, 'secret': { - 'ETOOLKIT_PARENT': 'default', + 'ETOOLKIT_PARENT': '_default', 'ETOOLKIT_SENSITIVE': ['PASSWORD'], 'GNUPGHOME': '%h/private/.gnupg', 'PASSWORD': ( diff --git a/tests/test_cli.py b/tests/test_cli.py index 884e676..684c2e7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -89,9 +89,7 @@ def test_list(capsys, config_file): main(['-c', f'{config_file}', '-l']) assert exit_info.type == SystemExit assert exit_info.value.code == 0 - assert capsys.readouterr().out.strip() == ( - f'default{os.linesep}dev{os.linesep}secret' - ) + assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret' def test_help(capsys): diff --git a/tests/test_envtoolkit_instance.py b/tests/test_envtoolkit_instance.py index 9061dc8..09baa31 100644 --- a/tests/test_envtoolkit_instance.py +++ b/tests/test_envtoolkit_instance.py @@ -1,5 +1,5 @@ # etoolkit -# Copyright (C) 2021-22 Simeon Simeonov +# Copyright (C) 2021-2022 Simeon Simeonov # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -63,4 +63,13 @@ def test_get_environ(config_data): def test_get_full_name(config_data): """Tests the EtoolkitInstance.get_full_name method""" instance = etoolkit.EtoolkitInstance('secret', config_data) - assert instance.get_full_name('->') == 'default->secret' + assert instance.get_full_name('->') == '_default->secret' + + +def test_parent_vars(config_data): + """Tests the EtoolkitInstance.get_full_name method""" + instance = etoolkit.EtoolkitInstance('dev', config_data) + assert instance.get_full_name('->') == '_default->dev' + assert instance.get_environ()['PYTHONPATH'] == ( + '/home/foo/_default/python:/home/user/dev/.pythonpath' + ) -- cgit v1.3