Merge branch 'maintenance' into devel
This commit is contained in:
commit
445d2dc600
2 files changed
+72
-19
No files matched your search
@@ -205,7 +205,7 @@ class PluginManagerPlugin(
|
||||
self.get_plugin_data_folder(), "notices.json"
|
||||
)
|
||||
self._notices_cache_ttl = self._settings.get_int(["notices_ttl"]) * 60
|
||||
self._confirm_disable = self._settings.global_get_boolean(["confirm_disable"])
|
||||
self._confirm_disable = self._settings.get_boolean(["confirm_disable"])
|
||||
|
||||
self._pip_caller = create_pip_caller(
|
||||
command=self._settings.global_get(["server", "commands", "localPipCommand"]),
|
||||
|
||||
@@ -40,6 +40,7 @@ from octoprint.util import (
|
||||
dict_merge,
|
||||
fast_deepcopy,
|
||||
is_hidden_path,
|
||||
time_this,
|
||||
yaml,
|
||||
)
|
||||
|
||||
@@ -238,6 +239,36 @@ class HierarchicalChainMap:
|
||||
|
||||
def __init__(self, *maps):
|
||||
self._chainmap = ChainMap(*map(self._flatten, maps))
|
||||
self._prefixed_keys = {}
|
||||
|
||||
def _has_prefix(self, prefix, current=None):
|
||||
if current is None:
|
||||
current = self._chainmap
|
||||
return any(map(lambda x: x in current, self._cached_prefixed_keys(prefix)))
|
||||
|
||||
def _with_prefix(self, prefix, current=None):
|
||||
if current is None:
|
||||
current = self._chainmap
|
||||
return {k: current[k] for k in self._cached_prefixed_keys(prefix) if k in current}
|
||||
|
||||
def _cached_prefixed_keys(self, prefix):
|
||||
if prefix not in self._prefixed_keys:
|
||||
self._prefixed_keys[prefix] = [
|
||||
k for k in self._chainmap.keys() if k.startswith(prefix)
|
||||
]
|
||||
return self._prefixed_keys[prefix]
|
||||
|
||||
def _invalidate_prefixed_keys(self, prefix):
|
||||
seps = []
|
||||
for i, c in enumerate(prefix):
|
||||
if c == _CHAINMAP_SEP:
|
||||
seps.append(i)
|
||||
|
||||
for sep in reversed(seps):
|
||||
try:
|
||||
del self._prefixed_keys[prefix[: sep + 1]]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def deep_dict(self):
|
||||
return self._unflatten(self._chainmap)
|
||||
@@ -252,8 +283,13 @@ class HierarchicalChainMap:
|
||||
|
||||
key = self._path_to_key(path)
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
return key in current or any(map(lambda x: x.startswith(prefix), current.keys()))
|
||||
return key in current or self._has_prefix(prefix, current)
|
||||
|
||||
@time_this(
|
||||
logtarget="octoprint.settings.timings.HierarchicalChainMap.get_by_path",
|
||||
message="{func}({func_args}) took {timing:.6f}ms",
|
||||
incl_func_args=True,
|
||||
)
|
||||
def get_by_path(self, path, only_local=False, only_defaults=False, merged=False):
|
||||
if only_defaults:
|
||||
current = self._chainmap.parents
|
||||
@@ -265,7 +301,7 @@ class HierarchicalChainMap:
|
||||
key = self._path_to_key(path)
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
|
||||
if key in current and not any(k.startswith(prefix) for k in current.keys()):
|
||||
if key in current and not self._has_prefix(prefix, current):
|
||||
# found it, return
|
||||
return current[key]
|
||||
|
||||
@@ -277,13 +313,11 @@ class HierarchicalChainMap:
|
||||
# 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 any(k.startswith(prefix) for k in layer):
|
||||
if self._has_prefix(prefix, layer):
|
||||
current = layer
|
||||
break
|
||||
|
||||
result = self._unflatten(
|
||||
{k: v for k, v in current.items() if k.startswith(prefix)}, prefix=prefix
|
||||
)
|
||||
result = self._unflatten(self._with_prefix(prefix, current), prefix=prefix)
|
||||
if not result:
|
||||
raise KeyError("Could not find entry for " + str(path))
|
||||
return result
|
||||
@@ -291,16 +325,21 @@ class HierarchicalChainMap:
|
||||
def set_by_path(self, path, value):
|
||||
current = self._chainmap.maps[0] # config only
|
||||
key = self._path_to_key(path)
|
||||
|
||||
# delete any subkeys
|
||||
self._del_prefix(current, key)
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
|
||||
if isinstance(value, dict):
|
||||
current.update(self._flatten(value, key))
|
||||
self._invalidate_prefixed_keys(prefix)
|
||||
|
||||
else:
|
||||
# path might have had subkeys before, clean them up
|
||||
self._del_prefix(current, prefix)
|
||||
|
||||
# make sure to clear anything below the path (e.g. switching from dict
|
||||
# to something else, for whatever reason)
|
||||
self._clean_upward_path(current, path)
|
||||
|
||||
# finally set the new value
|
||||
current[key] = value
|
||||
|
||||
def del_by_path(self, path):
|
||||
@@ -308,15 +347,16 @@ class HierarchicalChainMap:
|
||||
raise ValueError("Invalid path")
|
||||
|
||||
current = self._chainmap.maps[0] # config only
|
||||
delete_key = self._path_to_key(path)
|
||||
key = self._path_to_key(path)
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
deleted = False
|
||||
|
||||
# delete any subkeys
|
||||
deleted = self._del_prefix(current, delete_key)
|
||||
deleted = self._del_prefix(current, prefix)
|
||||
|
||||
# delete the key itself if it's there
|
||||
try:
|
||||
del current[delete_key]
|
||||
del current[key]
|
||||
deleted = True
|
||||
except KeyError:
|
||||
pass
|
||||
@@ -327,13 +367,14 @@ class HierarchicalChainMap:
|
||||
# clean anything that's now empty and above our path
|
||||
self._clean_upward_path(current, path)
|
||||
|
||||
def _del_prefix(self, current, key):
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
|
||||
to_delete = [k for k in current if k.startswith(prefix)]
|
||||
def _del_prefix(self, current, prefix):
|
||||
to_delete = self._with_prefix(prefix, current).keys()
|
||||
for k in to_delete:
|
||||
del current[k]
|
||||
|
||||
if len(to_delete) > 0:
|
||||
self._invalidate_prefixed_keys(prefix)
|
||||
|
||||
return len(to_delete) > 0
|
||||
|
||||
def _clean_upward_path(self, current, path):
|
||||
@@ -345,7 +386,7 @@ class HierarchicalChainMap:
|
||||
|
||||
key = self._path_to_key(working_path)
|
||||
prefix = key + _CHAINMAP_SEP
|
||||
if any(map(lambda k: k.startswith(prefix), current)):
|
||||
if self._has_prefix(prefix, current):
|
||||
# there's at least one subkey here, we're done
|
||||
break
|
||||
|
||||
@@ -396,9 +437,21 @@ class HierarchicalChainMap:
|
||||
return self._unflatten(self._chainmap.maps[-1])
|
||||
|
||||
def insert_map(self, pos, d):
|
||||
self._chainmap.maps.insert(pos, self._flatten(d))
|
||||
flattened = self._flatten(d)
|
||||
for k in flattened:
|
||||
if _CHAINMAP_SEP in k:
|
||||
self._invalidate_prefixed_keys(
|
||||
k.rsplit(_CHAINMAP_SEP, 1)[0] + _CHAINMAP_SEP
|
||||
)
|
||||
self._chainmap.maps.insert(pos, flattened)
|
||||
|
||||
def delete_map(self, pos):
|
||||
flattened = self._chainmap.maps[pos]
|
||||
for k in flattened:
|
||||
if _CHAINMAP_SEP in k:
|
||||
self._invalidate_prefixed_keys(
|
||||
k.rsplit(_CHAINMAP_SEP, 1)[0] + _CHAINMAP_SEP
|
||||
)
|
||||
del self._chainmap.maps[pos]
|
||||
|
||||
@property
|
||||
|
||||
Reference in new issue
Block a user