Make numbered_names use get_display_name; make dbref display separate method
This commit is contained in:
parent
d893cfd46e
commit
cbe3d4c738
14 changed files with 242 additions and 122 deletions
|
|
@ -11,12 +11,11 @@ from datetime import datetime, timedelta
|
|||
|
||||
import mock
|
||||
from django.test import TestCase
|
||||
from parameterized import parameterized
|
||||
from twisted.internet import task
|
||||
|
||||
from evennia.utils import utils
|
||||
from evennia.utils.ansi import ANSIString
|
||||
from evennia.utils.test_resources import BaseEvenniaTest
|
||||
from parameterized import parameterized
|
||||
from twisted.internet import task
|
||||
|
||||
|
||||
class TestIsIter(TestCase):
|
||||
|
|
@ -775,6 +774,54 @@ class TestJustify(TestCase):
|
|||
self.assertIn(ANSI_RED, str(result))
|
||||
|
||||
|
||||
class TestGroupObjectsByKeyAndDesc(TestCase):
|
||||
"""
|
||||
Test the utils.group_objects_by_key_and_desc function.
|
||||
|
||||
"""
|
||||
|
||||
class MockObject:
|
||||
def __init__(self, key, desc):
|
||||
self.key = key
|
||||
self.desc = desc
|
||||
|
||||
def get_display_name(self, looker, **kwargs):
|
||||
return self.key + f" (looker: {looker.key})"
|
||||
|
||||
def get_display_desc(self, looker, **kwargs):
|
||||
return self.desc + f" (looker: {looker.key})"
|
||||
|
||||
def get_numbered_name(self, count, looker, **kwargs):
|
||||
return f"{count} {self.key} (looker: {looker.key})"
|
||||
|
||||
def __repr__(self):
|
||||
return f"MockObject({self.key}, {self.desc})"
|
||||
|
||||
def test_group_by_key_and_desc(self):
|
||||
ma1 = self.MockObject("itemA", "descA")
|
||||
ma2 = self.MockObject("itemA", "descA")
|
||||
ma3 = self.MockObject("itemA", "descA")
|
||||
ma4 = self.MockObject("itemA", "descA")
|
||||
|
||||
mb1 = self.MockObject("itemB", "descB")
|
||||
mb2 = self.MockObject("itemB", "descB")
|
||||
mb3 = self.MockObject("itemB", "descB")
|
||||
|
||||
me = self.MockObject("Looker", "DescLooker")
|
||||
|
||||
result = utils.group_objects_by_key_and_desc([ma1, ma2, ma3, ma4, mb1, mb2, mb3], caller=me)
|
||||
|
||||
self.assertEqual(
|
||||
list(result),
|
||||
[
|
||||
("4 itemA (looker: Looker)", "descA (looker: Looker)", [ma1, ma2, ma3, ma4]),
|
||||
("3 itemB (looker: Looker)", "descB (looker: Looker)", [mb1, mb2, mb3]),
|
||||
],
|
||||
)
|
||||
|
||||
# Create a list of objects
|
||||
|
||||
|
||||
class TestMatchIP(TestCase):
|
||||
"""
|
||||
test utils.match_ip
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ from os.path import join as osjoin
|
|||
from string import punctuation
|
||||
from unicodedata import east_asian_width
|
||||
|
||||
import evennia
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||
|
|
@ -35,14 +36,12 @@ from django.core.validators import validate_email as django_validate_email
|
|||
from django.utils import timezone
|
||||
from django.utils.html import strip_tags
|
||||
from django.utils.translation import gettext as _
|
||||
from evennia.utils import logger
|
||||
from simpleeval import simple_eval
|
||||
from twisted.internet import reactor, threads
|
||||
from twisted.internet.defer import returnValue # noqa - used as import target
|
||||
from twisted.internet.task import deferLater
|
||||
|
||||
import evennia
|
||||
from evennia.utils import logger
|
||||
|
||||
_MULTIMATCH_TEMPLATE = settings.SEARCH_MULTIMATCH_TEMPLATE
|
||||
_EVENNIA_DIR = settings.EVENNIA_DIR
|
||||
_GAME_DIR = settings.GAME_DIR
|
||||
|
|
@ -1767,6 +1766,41 @@ def string_partial_matching(alternatives, inp, ret_index=True):
|
|||
return []
|
||||
|
||||
|
||||
def group_objects_by_key_and_desc(objects, caller=None, **kwargs):
|
||||
"""
|
||||
Groups a list of objects by their key and description. This is used to group
|
||||
visibly identical objects together, for example for inventory listings.
|
||||
|
||||
Args:
|
||||
objects (list): A list of objects to group. These must be DefaultObject.
|
||||
|
||||
caller (Object, optional): The object looking at the objects, used to get the
|
||||
description and key of each object.
|
||||
**kwargs: Passed into each object's `get_display_name/desc` methods.
|
||||
|
||||
Returns:
|
||||
iterable: An iterable of tuples, where each tuple is on the form
|
||||
`(numbered_name, description, [objects])`.
|
||||
|
||||
"""
|
||||
key_descs = defaultdict(list)
|
||||
return_string = kwargs.pop("return_string", True)
|
||||
|
||||
for obj in objects:
|
||||
key_descs[
|
||||
(obj.get_display_name(caller, **kwargs), obj.get_display_desc(caller, **kwargs))
|
||||
].append(obj)
|
||||
|
||||
return (
|
||||
(
|
||||
objs[0].get_numbered_name(len(objs), caller, return_string=return_string, **kwargs),
|
||||
desc,
|
||||
objs,
|
||||
)
|
||||
for (key, desc), objs in sorted(key_descs.items(), key=lambda tup: tup[0][0])
|
||||
)
|
||||
|
||||
|
||||
def format_table(table, extra_space=1):
|
||||
"""
|
||||
Format a 2D array of strings into a multi-column table.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue