adde abstract method to be able know if entity has children by a path key

This commit is contained in:
iLLiCiTiT 2021-12-16 15:46:45 +01:00
parent e0c48ec5c7
commit 16ae291386
8 changed files with 57 additions and 0 deletions

View file

@ -235,6 +235,11 @@ class BaseItemEntity(BaseEntity):
"""Return system settings entity."""
pass
@abstractmethod
def has_child_with_key(self, key):
"""Entity contains key as children."""
pass
def schema_validations(self):
"""Validate schema of entity and it's hierachy.

View file

@ -107,6 +107,9 @@ class DictConditionalEntity(ItemEntity):
for _key, _value in new_value.items():
self.non_gui_children[self.current_enum][_key].set(_value)
def has_child_with_key(self, key):
return key in self.keys()
def _item_initialization(self):
self._default_metadata = NOT_SET
self._studio_override_metadata = NOT_SET

View file

@ -205,6 +205,9 @@ class DictImmutableKeysEntity(ItemEntity):
)
self.show_borders = self.schema_data.get("show_borders", True)
def has_child_with_key(self, key):
return key in self.non_gui_children
def collect_static_entities_by_path(self):
output = {}
if self.is_dynamic_item or self.is_in_dynamic_item:

View file

@ -191,6 +191,9 @@ class DictMutableKeysEntity(EndpointEntity):
child_entity = self.children_by_key[key]
self.set_child_label(child_entity, label)
def has_child_with_key(self, key):
return key in self.children_by_key
def _item_initialization(self):
self._default_metadata = {}
self._studio_override_metadata = {}

View file

@ -118,6 +118,9 @@ class InputEntity(EndpointEntity):
return self.value == other.value
return self.value == other
def has_child_with_key(self, key):
return False
def get_child_path(self, child_obj):
raise TypeError("{} can't have children".format(
self.__class__.__name__

View file

@ -1,3 +1,7 @@
import re
import six
from .lib import (
NOT_SET,
STRING_TYPE,
@ -48,6 +52,9 @@ class PathEntity(ItemEntity):
raise AttributeError(self.attribute_error_msg.format("items"))
return self.child_obj.items()
def has_child_with_key(self, key):
return self.child_obj.has_child_with_key(key)
def _item_initialization(self):
if self.group_item is None and not self.is_group:
self.is_group = True
@ -197,6 +204,7 @@ class PathEntity(ItemEntity):
class ListStrictEntity(ItemEntity):
schema_types = ["list-strict"]
_key_regex = re.compile(r"[0-9]+")
def __getitem__(self, idx):
if not isinstance(idx, int):
@ -216,6 +224,19 @@ class ListStrictEntity(ItemEntity):
return self.children[idx]
return default
def has_child_with_key(self, key):
if (
key
and isinstance(key, six.string_types)
and self._key_regex.match(key)
):
key = int(key)
if not isinstance(key, int):
return False
return 0 <= key < len(self.children)
def _item_initialization(self):
self.valid_value_types = (list, )
self.require_key = True

View file

@ -1,4 +1,6 @@
import copy
import six
import re
from . import (
BaseEntity,
EndpointEntity
@ -21,6 +23,7 @@ class ListEntity(EndpointEntity):
"collapsible": True,
"collapsed": False
}
_key_regex = re.compile(r"[0-9]+")
def __iter__(self):
for item in self.children:
@ -144,6 +147,19 @@ class ListEntity(EndpointEntity):
)
self.on_change()
def has_child_with_key(self, key):
if (
key
and isinstance(key, six.string_types)
and self._key_regex.match(key)
):
key = int(key)
if not isinstance(key, int):
return False
return 0 <= key < len(self.children)
def _convert_to_valid_type(self, value):
if isinstance(value, (set, tuple)):
return list(value)

View file

@ -127,6 +127,9 @@ class RootEntity(BaseItemEntity):
for _key, _value in new_value.items():
self.non_gui_children[_key].set(_value)
def has_child_with_key(self, key):
return key in self.non_gui_children
def keys(self):
return self.non_gui_children.keys()