Skip to content

Commit

Permalink
feat: set keys in InsightsClientConfig
Browse files Browse the repository at this point in the history
Add the possibility to set configuration keys in InsightsClientConfig.

Note that only known configuration keys will be actually stored in the
configuration; unknown names will set instance attributes as usual.

Signed-off-by: Pino Toscano <[email protected]>
  • Loading branch information
ptoscano committed Feb 9, 2024
1 parent 1980b77 commit 54ce5f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pytest_client_tools/insights_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def __getattr__(self, name):
except configparser.NoOptionError:
raise KeyError(name)

def __setattr__(self, name, value):
if name in (
self._KEYS_BOOL | self._KEYS_INT | self._KEYS_FLOAT | self._KEYS_STRING
):
self._config.set("insights-client", name, str(value))
return
super().__setattr__(name, value)


class InsightsClient:
def __init__(self):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_insights_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ def test_config_existing_keys(tmp_path):
assert isinstance(conf.http_timeout, float)


def test_config_set_keys(tmp_path):
conf_file = tmp_path / "file.conf"
conf_file.write_text(
"""[insights-client]
auto_config=True
authmethod=CERT
cmd_timeout=120
http_timeout=120
"""
)
conf = InsightsClientConfig(conf_file)
# existing key
conf.cmd_timeout = 60
assert conf.cmd_timeout == 60
# missing but known key
with pytest.raises(KeyError):
assert conf.loglevel == "DEBUG"
conf.loglevel = "DEBUG"
assert conf.loglevel == "DEBUG"
# unknown key; setting will set a class attribute, not a config value
conf.unknown = "see"
assert conf.unknown == "see"


def test_config_reload(tmp_path):
conf_file = tmp_path / "file.conf"
conf_file.write_text(
Expand Down

0 comments on commit 54ce5f8

Please sign in to comment.