mutable dictionary validate keys on change and tries to fix them on load

This commit is contained in:
iLLiCiTiT 2021-03-25 13:29:56 +01:00
parent 01d0660253
commit 48a300cfc3
2 changed files with 26 additions and 1 deletions

View file

@ -58,6 +58,7 @@ from .exceptions import (
DefaultsNotDefined,
StudioDefaultsNotDefined,
InvalidValueType,
InvalidKeySymbols,
SchemaMissingFileInfo,
SchemeGroupHierarchyBug,
SchemaDuplicatedKeys,
@ -114,6 +115,7 @@ __all__ = (
"DefaultsNotDefined",
"StudioDefaultsNotDefined",
"InvalidValueType",
"InvalidKeySymbols",
"SchemaMissingFileInfo",
"SchemeGroupHierarchyBug",
"SchemaDuplicatedKeys",

View file

@ -1,3 +1,4 @@
import re
import copy
from .lib import (
@ -7,6 +8,7 @@ from .lib import (
from . import EndpointEntity
from .exceptions import (
DefaultsNotDefined,
InvalidKeySymbols,
StudioDefaultsNotDefined,
RequiredKeyModified,
EntitySchemaError
@ -14,7 +16,9 @@ from .exceptions import (
from pype.settings.constants import (
METADATA_KEYS,
M_DYNAMIC_KEY_LABEL,
M_ENVIRONMENT_KEY
M_ENVIRONMENT_KEY,
KEY_REGEX,
KEY_ALLOWED_SYMBOLS
)
@ -92,6 +96,9 @@ class DictMutableKeysEntity(EndpointEntity):
# TODO Check for value type if is Settings entity?
child_obj = self.children_by_key.get(key)
if not child_obj:
if not KEY_REGEX.match(key):
raise InvalidKeySymbols(self.path, key)
child_obj = self.add_key(key)
child_obj.set(value)
@ -102,6 +109,10 @@ class DictMutableKeysEntity(EndpointEntity):
if new_key == old_key:
return
if not KEY_REGEX.match(new_key):
raise InvalidKeySymbols(self.path, new_key)
self.children_by_key[new_key] = self.children_by_key.pop(old_key)
self._on_key_label_change()
@ -116,6 +127,9 @@ class DictMutableKeysEntity(EndpointEntity):
if key in self.children_by_key:
self.pop(key)
if not KEY_REGEX.match(key):
raise InvalidKeySymbols(self.path, key)
if self.value_is_env_group:
item_schema = copy.deepcopy(self.item_schema)
item_schema["env_group_key"] = key
@ -325,6 +339,15 @@ class DictMutableKeysEntity(EndpointEntity):
children_label_by_id = {}
metadata_labels = metadata.get(M_DYNAMIC_KEY_LABEL) or {}
for _key, _value in new_value.items():
if not KEY_REGEX.match(_key):
# Replace invalid characters with underscore
# - this is safety to not break already existing settings
_key = re.sub(
r"[^{}]+".format(KEY_ALLOWED_SYMBOLS),
"_",
_key
)
child_entity = self._add_key(_key)
child_entity.update_default_value(_value)
if using_project_overrides: