From e03da40867dd8a38fe76e1b62d7ea0404cb6f7f8 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Fri, 9 Jun 2023 14:56:51 -0400 Subject: [PATCH] Add a config get/set API (from @guilledk) ? --- piker/config.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/piker/config.py b/piker/config.py index 12085261..a0d403d5 100644 --- a/piker/config.py +++ b/piker/config.py @@ -467,3 +467,51 @@ def load_accounts( accounts['paper'] = None return accounts + + +# XXX: Recursive getting & setting + +def get_value(_dict, _section): + subs = _section.split('.') + if len(subs) > 1: + return get_value( + _dict[subs[0]], + '.'.join(subs[1:]), + ) + + else: + return _dict[_section] + + +def set_value(_dict, _section, val): + subs = _section.split('.') + if len(subs) > 1: + if subs[0] not in _dict: + _dict[subs[0]] = {} + + return set_value( + _dict[subs[0]], + '.'.join(subs[1:]), + val + ) + + else: + _dict[_section] = val + + +def del_value(_dict, _section): + subs = _section.split('.') + if len(subs) > 1: + if subs[0] not in _dict: + return + + return del_value( + _dict[subs[0]], + '.'.join(subs[1:]) + ) + + else: + if _section not in _dict: + return + + del _dict[_section]