diff options
| author | Simeon Simeonov | 2022-04-10 22:21:41 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2022-04-10 22:21:41 +0200 |
| commit | d15ee60ca7ad62004c5e036203f095cc27660f90 (patch) | |
| tree | 0fc478fe2cab5cd670137ad4d4dbd75fd4b68b36 | |
| parent | e36f260c2ebc5e21724e5eedf0a91d2bc66cceec (diff) | |
Introduce a new macro (%p) representing the parent value of a given key
| -rw-r--r-- | README.md | 16 | ||||
| -rwxr-xr-x | src/etoolkit/etoolkit.py | 6 | ||||
| -rw-r--r-- | tests/conftest.py | 15 | ||||
| -rw-r--r-- | tests/test_cli.py | 4 | ||||
| -rw-r--r-- | tests/test_envtoolkit_instance.py | 13 |
5 files changed, 34 insertions, 20 deletions
| @@ -179,7 +179,9 @@ corresponding values. Currently the following macros are supported: | |||
| 179 | 179 | ||
| 180 | - **%h** - the home directory of the user running *etoolkit* (~/) | 180 | - **%h** - the home directory of the user running *etoolkit* (~/) |
| 181 | 181 | ||
| 182 | - **%i** - the names of the instance that is about to be loaded | 182 | - **%i** - the name of the instance that is about to be loaded |
| 183 | |||
| 184 | - **%p** - the parent value (for the same key) | ||
| 183 | 185 | ||
| 184 | - **%u** - the username of the user running *etoolkit* (*getpass.getuser()*) | 186 | - **%u** - the username of the user running *etoolkit* (*getpass.getuser()*) |
| 185 | 187 | ||
| @@ -234,16 +236,16 @@ or the *instances* structure being loaded from a diferent configuration file | |||
| 234 | "general": { | 236 | "general": { |
| 235 | }, | 237 | }, |
| 236 | "instances": { | 238 | "instances": { |
| 237 | "default": { | 239 | "_default": { |
| 238 | "ETOOLKIT_PROMPT": "(%i)" | 240 | "ETOOLKIT_PROMPT": "(%i)", |
| 241 | "PYTHONPATH": "/home/user/%i/python", | ||
| 239 | }, | 242 | }, |
| 240 | "dev": { | 243 | "dev": { |
| 241 | "ETOOLKIT_PARENT": "default", | 244 | "ETOOLKIT_PARENT": "_default", |
| 242 | "PYTHONPATH": ":/home/user/.pythonpath", | 245 | "PYTHONPATH": "%p:/home/user/%i/.pythonpath", |
| 243 | "DB_CONNECTION": "enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT5tS9o+ABvsxogIXpJim16Gz5SVtV8=" | ||
| 244 | }, | 246 | }, |
| 245 | "secret": { | 247 | "secret": { |
| 246 | "ETOOLKIT_PARENT": "default", | 248 | "ETOOLKIT_PARENT": "_default", |
| 247 | "ETOOLKIT_SENSITIVE": ["PASSWORD"], | 249 | "ETOOLKIT_SENSITIVE": ["PASSWORD"], |
| 248 | "GNUPGHOME": "%h/private/.gnupg", | 250 | "GNUPGHOME": "%h/private/.gnupg", |
| 249 | "PASSWORD": "enc-val$1$vIBcoCNiYrsDLtF41uLuSEnppBjhliD0B8jwcBJcj/c=$KwOGe/y1dlxktDaCnJPIVNuaQ4Q7yNo=" | 251 | "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: | |||
| 342 | if not value: | 342 | if not value: |
| 343 | # perhaps unset instead of skipping? | 343 | # perhaps unset instead of skipping? |
| 344 | continue | 344 | continue |
| 345 | if isinstance(value, str) and '%p' in value: | ||
| 346 | macros['%p'] = ( | ||
| 347 | self._parent.get_environ().get(key, '') | ||
| 348 | if self._parent is not None | ||
| 349 | else '' | ||
| 350 | ) | ||
| 345 | if isinstance(value, str) and value.startswith('enc-val$1$'): | 351 | if isinstance(value, str) and value.startswith('enc-val$1$'): |
| 346 | value = self._decrypt_value(value) | 352 | value = self._decrypt_value(value) |
| 347 | if isinstance(value, str) and value.endswith(':'): | 353 | 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(): | |||
| 30 | ) | 30 | ) |
| 31 | }, | 31 | }, |
| 32 | 'instances': { | 32 | 'instances': { |
| 33 | 'default': {'ETOOLKIT_PROMPT': '(%i)'}, | 33 | '_default': { |
| 34 | 'ETOOLKIT_PROMPT': '(%i)', | ||
| 35 | 'PYTHONPATH': '/home/foo/%i/python', | ||
| 36 | }, | ||
| 34 | 'dev': { | 37 | 'dev': { |
| 35 | 'ETOOLKIT_PARENT': 'default', | 38 | 'ETOOLKIT_PARENT': '_default', |
| 36 | 'PYTHONPATH': ':/home/user/.pythonpath', | 39 | 'PYTHONPATH': '%p:/home/user/%i/.pythonpath', |
| 37 | 'DB_CONNECTION': ( | ||
| 38 | 'enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT' | ||
| 39 | '5tS9o+ABvsxogIXpJim16Gz5SVtV8=' | ||
| 40 | ), | ||
| 41 | }, | 40 | }, |
| 42 | 'secret': { | 41 | 'secret': { |
| 43 | 'ETOOLKIT_PARENT': 'default', | 42 | 'ETOOLKIT_PARENT': '_default', |
| 44 | 'ETOOLKIT_SENSITIVE': ['PASSWORD'], | 43 | 'ETOOLKIT_SENSITIVE': ['PASSWORD'], |
| 45 | 'GNUPGHOME': '%h/private/.gnupg', | 44 | 'GNUPGHOME': '%h/private/.gnupg', |
| 46 | 'PASSWORD': ( | 45 | '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): | |||
| 89 | main(['-c', f'{config_file}', '-l']) | 89 | main(['-c', f'{config_file}', '-l']) |
| 90 | assert exit_info.type == SystemExit | 90 | assert exit_info.type == SystemExit |
| 91 | assert exit_info.value.code == 0 | 91 | assert exit_info.value.code == 0 |
| 92 | assert capsys.readouterr().out.strip() == ( | 92 | assert capsys.readouterr().out.strip() == f'dev{os.linesep}secret' |
| 93 | f'default{os.linesep}dev{os.linesep}secret' | ||
| 94 | ) | ||
| 95 | 93 | ||
| 96 | 94 | ||
| 97 | def test_help(capsys): | 95 | 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 @@ | |||
| 1 | # etoolkit | 1 | # etoolkit |
| 2 | # Copyright (C) 2021-22 Simeon Simeonov | 2 | # Copyright (C) 2021-2022 Simeon Simeonov |
| 3 | 3 | ||
| 4 | # This program is free software: you can redistribute it and/or modify | 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by | 5 | # it under the terms of the GNU General Public License as published by |
| @@ -63,4 +63,13 @@ def test_get_environ(config_data): | |||
| 63 | def test_get_full_name(config_data): | 63 | def test_get_full_name(config_data): |
| 64 | """Tests the EtoolkitInstance.get_full_name method""" | 64 | """Tests the EtoolkitInstance.get_full_name method""" |
| 65 | instance = etoolkit.EtoolkitInstance('secret', config_data) | 65 | instance = etoolkit.EtoolkitInstance('secret', config_data) |
| 66 | assert instance.get_full_name('->') == 'default->secret' | 66 | assert instance.get_full_name('->') == '_default->secret' |
| 67 | |||
| 68 | |||
| 69 | def test_parent_vars(config_data): | ||
| 70 | """Tests the EtoolkitInstance.get_full_name method""" | ||
| 71 | instance = etoolkit.EtoolkitInstance('dev', config_data) | ||
| 72 | assert instance.get_full_name('->') == '_default->dev' | ||
| 73 | assert instance.get_environ()['PYTHONPATH'] == ( | ||
| 74 | '/home/foo/_default/python:/home/user/dev/.pythonpath' | ||
| 75 | ) | ||
