implemented entity path getter

This commit is contained in:
iLLiCiTiT 2021-01-25 10:12:19 +01:00
parent 89c4c4558e
commit 2e790c117b
3 changed files with 65 additions and 0 deletions

View file

@ -124,6 +124,9 @@ class BaseEntity:
# Log object
self._log = None
# Item path attribute (may be filled or be dynamic)
self._path = None
# These should be set on initialization and not change then
self.valid_value_types = getattr(self, "valid_value_types", NOT_SET)
self.value_on_not_set = getattr(self, "value_on_not_set", NOT_SET)
@ -167,6 +170,20 @@ class BaseEntity:
self.override_state = OverrideState.STUDIO
@property
def path(self):
if self._path is not None:
return self._path
path = self.parent.get_child_path(self)
if not self.is_in_dynamic_item and not self.is_dynamic_item:
self._path = path
return path
@abstractmethod
def get_child_path(self, child_obj):
pass
@abstractmethod
def set_override_state(self, state):
pass

View file

@ -192,6 +192,11 @@ class InputEntity(ItemEntity):
self.has_studio_override = self.had_studio_override
self.has_project_override = self.had_project_override
def get_child_path(self, child_obj):
raise TypeError("{} can't have children".format(
self.__class__.__name__
))
class NumberEntity(InputEntity):
schema_types = ["number"]

View file

@ -147,7 +147,10 @@ class GUIEntity(ItemEntity):
child_overriden = False
current_value = NOT_SET
path = "GUIEntity"
# Abstract methods
get_child_path = None
set_value = None
set_override_state = None
discard_changes = None
@ -267,6 +270,19 @@ class DictImmutableKeysEntity(ItemEntity):
if self.is_dynamic_item:
self.require_key = False
def get_child_path(self, child_obj):
result_key = None
for key, _child_obj in self.non_gui_children.items():
if _child_obj is child_obj:
result_key = key
break
if result_key is None:
# raise ValueError("Didn't found child {}".format(child_obj))
print("NOT FOUND CHILD PATH", self.path)
return "/".join([self.path, result_key])
def set_value(self, value):
for _key, _value in value.items():
self.non_gui_children[_key].set_value(_value)
@ -816,6 +832,18 @@ class ListEntity(ItemEntity):
self.collapsible = self.schema_data.get("collapsible") or True
self.collapsed = self.schema_data.get("collapsed") or False
def get_child_path(self, child_obj):
result_idx = None
for idx, _child_obj in enumerate(self.children):
if _child_obj is child_obj:
result_idx = idx
break
if result_idx is None:
raise ValueError("Didn't found child {}".format(child_obj))
return "/".join([self.path, str(result_idx)])
def set_value(self, value):
pass
@ -986,6 +1014,9 @@ class PathEntity(ItemEntity):
self.child_obj = self.create_schema_object(item_schema, self)
self.valid_value_types = valid_value_types
def get_child_path(self, child_obj):
return self.path
def set_value(self, value):
self.child_obj.set_value(value)
@ -1092,6 +1123,18 @@ class ListStrictEntity(ItemEntity):
# GUI attribute
self.is_horizontal = self.schema_data.get("horizontal", True)
def get_child_path(self, child_obj):
result_idx = None
for idx, _child_obj in enumerate(self.children):
if _child_obj is child_obj:
result_idx = idx
break
if result_idx is None:
raise ValueError("Didn't found child {}".format(child_obj))
return "/".join([self.path, str(result_idx)])
@property
def current_value(self):
return self._current_value