From a4eb891f6930075ddf1a26398b1ccd6a8d74c2bf Mon Sep 17 00:00:00 2001 From: Wendy Wang Date: Thu, 13 Oct 2022 14:52:25 +0200 Subject: [PATCH] Adding search_typeclass to search utils --- evennia/typeclasses/managers.py | 7 +++---- evennia/utils/search.py | 33 ++++++++++++++++++++++++++++++ evennia/utils/tests/test_search.py | 21 ++++++++++++++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/evennia/typeclasses/managers.py b/evennia/typeclasses/managers.py index e5d1130f12..7333e76a7f 100644 --- a/evennia/typeclasses/managers.py +++ b/evennia/typeclasses/managers.py @@ -537,15 +537,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. If location is set, limit search to objects - in that location. + 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`. diff --git a/evennia/utils/search.py b/evennia/utils/search.py index 18929057f1..6b33618c75 100644 --- a/evennia/utils/search.py +++ b/evennia/utils/search.py @@ -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 diff --git a/evennia/utils/tests/test_search.py b/evennia/utils/tests/test_search.py index a5355bea22..7bb7b4b97b 100644 --- a/evennia/utils/tests/test_search.py +++ b/evennia/utils/tests/test_search.py @@ -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)