Fix merge conflict

This commit is contained in:
Griatch 2022-10-13 16:44:33 +02:00
commit 788de8af47
3 changed files with 56 additions and 3 deletions

View file

@ -538,13 +538,14 @@ class TypedObjectManager(idmapper.manager.SharedMemoryManager):
def typeclass_search(self, typeclass, include_children=False, include_parents=False):
"""
Searches through all objects returning those which has a certain typeclass.
Searches through all objects returning those which are of the
specified typeclass.
Args:
typeclass (str or class): A typeclass class or a python path to a typeclass.
include_children (bool, optional): Return objects with
given typeclass *and* all children inheriting from this
typeclass. Mutuall exclusive to `include_parents`.
typeclass. Mutually exclusive to `include_parents`.
include_parents (bool, optional): Return objects with
given typeclass *and* all parents to this typeclass.
Mutually exclusive to `include_children`.

View file

@ -41,6 +41,7 @@ __all__ = (
"search_script_tag",
"search_account_tag",
"search_channel_tag",
"search_typeclass",
)
@ -362,3 +363,35 @@ def search_channel_tag(key=None, category=None, tagtype=None, **kwargs):
# search for tag objects (not the objects they are attached to
search_tag_object = ObjectDB.objects.get_tag
# Locate Objects by Typeclass
# search_objects_by_typeclass(typeclass="", include_children=False, include_parents=False) (also search_typeclass works)
# This returns the objects of the given typeclass
def search_objects_by_typeclass(typeclass, include_children=False, include_parents=False):
"""
Searches through all objects returning those of a certain typeclass.
Args:
typeclass (str or class): A typeclass class or a python path to a typeclass.
include_children (bool, optional): Return objects with
given typeclass *and* all children inheriting from this
typeclass. Mutuall exclusive to `include_parents`.
include_parents (bool, optional): Return objects with
given typeclass *and* all parents to this typeclass.
Mutually exclusive to `include_children`.
Returns:
objects (list): The objects found with the given typeclasses.
"""
return ObjectDB.objects.typeclass_search(
typeclass=typeclass,
include_children=include_children,
include_parents=include_parents,
)
search_typeclass = search_objects_by_typeclass

View file

@ -1,6 +1,13 @@
from evennia.scripts.scripts import DefaultScript
from evennia.utils.test_resources import EvenniaTest
from evennia.utils.search import search_script_attribute, search_script_tag, search_script
from evennia import DefaultObject, DefaultRoom
from evennia.objects.models import ObjectDB
from evennia.utils.search import (
search_script_attribute,
search_script_tag,
search_script,
search_typeclass,
)
class TestSearch(EvenniaTest):
@ -61,3 +68,15 @@ class TestSearch(EvenniaTest):
script, errors = DefaultScript.create("a-script")
found = search_script("wrong_key")
self.assertEqual(len(found), 0, errors)
def test_search_typeclass(self):
"""Check that an object can be found by typeclass"""
obj = DefaultObject.create("test_obj")
found = search_typeclass("evennia.objects.objects.DefaultObject")
self.assertEqual(len(found), 1)
def test_search_wrong_typeclass(self):
"""Check that an object cannot be found by wrong typeclass"""
obj = DefaultObject.create("test_obj_2")
found = search_typeclass("not.a.typeclass")
self.assertEqual(len(found), 0)