Merge pull request #2686 from Thrag-Zan/component-contrib-tag

component-contrib:add component name as tag on host
This commit is contained in:
Griatch 2022-03-16 23:15:31 +01:00 committed by GitHub
commit fdff5f6cd2
2 changed files with 48 additions and 0 deletions

View file

@ -64,6 +64,7 @@ class ComponentHandler:
"""
self._set_component(component)
self.db_names.append(component.name)
self.host.tags.add(component.name, category="components")
component.at_added(self.host)
def add_default(self, name):
@ -84,6 +85,7 @@ class ComponentHandler:
new_component = component.default_create(self.host)
self._set_component(new_component)
self.db_names.append(name)
self.host.tags.add(name, category="components")
new_component.at_added(self.host)
def remove(self, component):
@ -100,6 +102,7 @@ class ComponentHandler:
if component_name in self._loaded_components:
component.at_removed(self.host)
self.db_names.remove(component_name)
self.host.tags.remove(component_name, category="components")
del self._loaded_components[component_name]
else:
message = f"Cannot remove {component_name} from {self.host.name} as it is not registered."
@ -122,6 +125,7 @@ class ComponentHandler:
instance.at_removed(self.host)
self.db_names.remove(name)
self.host.tags.remove(name, category="components")
del self._loaded_components[name]
def get(self, name):
@ -216,6 +220,14 @@ class ComponentHolderMixin(object):
self.db.component_names = component_names
def basetype_posthook_setup(self):
"""
Method that add component related tags that were set using ComponentProperty.
"""
super().basetype_posthook_setup()
for component_name in self.db.component_names:
self.tags.add(component_name, category="components")
@property
def components(self) -> ComponentHandler:
"""

View file

@ -107,3 +107,39 @@ class TestComponents(EvenniaTest):
def test_returns_none_with_regular_get_when_no_attribute(self):
assert self.char1.cmp.does_not_exist is None
def test_host_has_class_component_tags(self):
assert self.char1.tags.has(key="test_a", category="components")
assert self.char1.tags.has(key="test_b", category="components")
assert not self.char1.tags.has(key="test_c", category="components")
def test_host_has_added_component_tags(self):
rct = RuntimeComponentTestC.create(self.char1)
self.char1.components.add(rct)
test_c = self.char1.components.get('test_c')
assert self.char1.tags.has(key="test_c", category="components")
def test_host_has_added_default_component_tags(self):
self.char1.components.add_default("test_c")
test_c = self.char1.components.get("test_c")
assert self.char1.tags.has(key="test_c", category="components")
def test_host_remove_component_tags(self):
rct = RuntimeComponentTestC.create(self.char1)
handler = self.char1.components
handler.add(rct)
assert self.char1.tags.has(key="test_c", category="components")
handler.remove(rct)
assert not self.char1.tags.has(key="test_c", category="components")
def test_host_remove_by_name_component_tags(self):
rct = RuntimeComponentTestC.create(self.char1)
handler = self.char1.components
handler.add(rct)
assert self.char1.tags.has(key="test_c", category="components")
handler.remove_by_name("test_c")
assert not self.char1.tags.has(key="test_c", category="components")