From 07a3305104da33a9666f09e8516769776b4b17f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 18 Feb 2022 11:18:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Do=20the=20merged=3DFalse?= =?UTF-8?q?=20handling=20only=20after=20a=20miss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That's only interesting for dicts anyhow, so let the chainmap itself take care of fetching the top-most individual values and only spend more cycles on picking the right layer for unmerged access if we can't find the value. --- src/octoprint/settings.py | 46 +++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index d441fa67a..90f10042d 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -630,36 +630,30 @@ class HierarchicalChainMap: key = self._path_to_key(path) - # we do something a bit odd here: if merged is not true, we don't include the - # full contents of the key. Instead, we only include the contents of the key on - # the first level where we find the value. - # - # TODO 2.0.0 remove this weird & historic behaviour and make 'merged' the default - if not merged and not only_local: - # first check if we can find the exact key & if so use that layer + if key in current: + # found it, return + return current[key] + + # if we arrived here we might be trying to grab a dict, look for children + + key = key + _CHAINMAP_SEP + + # TODO 2.0.0 remove this & make 'merged' the default + if not merged and hasattr(current, "maps"): + # we do something a bit odd here: if merged is not true, we don't include the + # full contents of the key. Instead, we only include the contents of the key + # on the first level where we find the value. for layer in current.maps: - if key in layer: + if any(k.startswith(key) for k in layer): current = layer break - else: - # next, check if we can find the key as a prefix for a subtree & if so - # use that layer - for layer in current.maps: - if any(k.startswith(key) for k in layer): - current = layer - break - if key not in current: - # we might be trying to grab a dict, look for children - key = key + _CHAINMAP_SEP - result = self._unflatten( - {k: v for k, v in current.items() if k.startswith(key)}, prefix=key - ) - if not result: - raise KeyError("Could not find entry for " + str(path)) - return result - - return current[key] + result = self._unflatten( + {k: v for k, v in current.items() if k.startswith(key)}, prefix=key + ) + if not result: + raise KeyError("Could not find entry for " + str(path)) + return result def set_by_path(self, path, value): current = self._chainmap