Make examine command display strattr Attribute values correctly; Resolve #3590. Clean up some regex syntaxes for py3.13 compatibility

This commit is contained in:
Griatch 2024-08-11 12:09:48 +02:00
parent 61374b10ff
commit 7d553125ba
4 changed files with 36 additions and 25 deletions

View file

@ -3,8 +3,12 @@
## Main branch ## Main branch
[Docs][issue3591]: Fix of NPC reaction tutorial code (Griatch) [Docs][issue3591]: Fix of NPC reaction tutorial code (Griatch)
[Docs][issue3590]: Make `examine` command properly show `strattr` type
Attribute values (Griatch)
[issue3591]: https://github.com/evennia/evennia/issues/3591
[issue3590]: https://github.com/evennia/evennia/issues/3590
[issue3592]: https://github.com/evennia/evennia/issues/3591
## Evennia 4.3.0 ## Evennia 4.3.0

View file

@ -5,11 +5,10 @@ Building and world design commands
import re import re
import typing import typing
import evennia
from django.conf import settings from django.conf import settings
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import Max, Min, Q from django.db.models import Max, Min, Q
import evennia
from evennia import InterruptCommand from evennia import InterruptCommand
from evennia.commands.cmdhandler import generate_cmdset_providers, get_and_merge_cmdsets from evennia.commands.cmdhandler import generate_cmdset_providers, get_and_merge_cmdsets
from evennia.locks.lockhandler import LockException from evennia.locks.lockhandler import LockException
@ -2831,8 +2830,12 @@ class CmdExamine(ObjManipCommand):
_FUNCPARSER = funcparser.FuncParser(settings.FUNCPARSER_OUTGOING_MESSAGES_MODULES) _FUNCPARSER = funcparser.FuncParser(settings.FUNCPARSER_OUTGOING_MESSAGES_MODULES)
key, category, value = attr.db_key, attr.db_category, attr.value key, category, value = attr.db_key, attr.db_category, attr.value
valuetype = ""
if value is None and attr.strvalue is not None:
value = attr.strvalue
valuetype = " |B[strvalue]|n"
typ = self._get_attribute_value_type(value) typ = self._get_attribute_value_type(value)
typ = f" |B[type: {typ}]|n" if typ else "" typ = f" |B[type:{typ}]|n{valuetype}" if typ else f"{valuetype}"
value = utils.to_str(value) value = utils.to_str(value)
value = _FUNCPARSER.parse(ansi_raw(value), escape=True) value = _FUNCPARSER.parse(ansi_raw(value), escape=True)
return ( return (
@ -2846,8 +2849,12 @@ class CmdExamine(ObjManipCommand):
_FUNCPARSER = funcparser.FuncParser(settings.FUNCPARSER_OUTGOING_MESSAGES_MODULES) _FUNCPARSER = funcparser.FuncParser(settings.FUNCPARSER_OUTGOING_MESSAGES_MODULES)
key, category, value = attr.db_key, attr.db_category, attr.value key, category, value = attr.db_key, attr.db_category, attr.value
valuetype = ""
if value is None and attr.strvalue is not None:
value = attr.strvalue
valuetype = " |B[strvalue]|n"
typ = self._get_attribute_value_type(value) typ = self._get_attribute_value_type(value)
typ = f" |B[type: {typ}]|n" if typ else "" typ = f" |B[type: {typ}]|n{valuetype}" if typ else f"{valuetype}"
value = utils.to_str(value) value = utils.to_str(value)
value = _FUNCPARSER.parse(ansi_raw(value), escape=True) value = _FUNCPARSER.parse(ansi_raw(value), escape=True)
value = utils.crop(value) value = utils.crop(value)

View file

@ -14,13 +14,10 @@ main test suite started with
import datetime import datetime
from unittest.mock import MagicMock, Mock, patch from unittest.mock import MagicMock, Mock, patch
import evennia
from anything import Anything from anything import Anything
from django.conf import settings from django.conf import settings
from django.test import override_settings from django.test import override_settings
from parameterized import parameterized
from twisted.internet import task
import evennia
from evennia import ( from evennia import (
DefaultCharacter, DefaultCharacter,
DefaultExit, DefaultExit,
@ -32,14 +29,7 @@ from evennia import (
from evennia.commands import cmdparser from evennia.commands import cmdparser
from evennia.commands.cmdset import CmdSet from evennia.commands.cmdset import CmdSet
from evennia.commands.command import Command, InterruptCommand from evennia.commands.command import Command, InterruptCommand
from evennia.commands.default import ( from evennia.commands.default import account, admin, batchprocess, building, comms, general
account,
admin,
batchprocess,
building,
comms,
general,
)
from evennia.commands.default import help as help_module from evennia.commands.default import help as help_module
from evennia.commands.default import syscommands, system, unloggedin from evennia.commands.default import syscommands, system, unloggedin
from evennia.commands.default.cmdset_character import CharacterCmdSet from evennia.commands.default.cmdset_character import CharacterCmdSet
@ -48,6 +38,8 @@ from evennia.prototypes import prototypes as protlib
from evennia.utils import create, gametime, utils from evennia.utils import create, gametime, utils
from evennia.utils.test_resources import BaseEvenniaCommandTest # noqa from evennia.utils.test_resources import BaseEvenniaCommandTest # noqa
from evennia.utils.test_resources import BaseEvenniaTest, EvenniaCommandTest from evennia.utils.test_resources import BaseEvenniaTest, EvenniaCommandTest
from parameterized import parameterized
from twisted.internet import task
# ------------------------------------------------------------ # ------------------------------------------------------------
# Command testing # Command testing
@ -446,7 +438,7 @@ class TestCmdTasks(BaseEvenniaCommandTest):
args = f"/pause {self.task.get_id()}" args = f"/pause {self.task.get_id()}"
wanted_msg = "Pause task 1 with completion date" wanted_msg = "Pause task 1 with completion date"
cmd_result = self.call(system.CmdTasks(), args, wanted_msg) cmd_result = self.call(system.CmdTasks(), args, wanted_msg)
self.assertRegex(cmd_result, " \(func_test_cmd_tasks\) ") self.assertRegex(cmd_result, r" \(func_test_cmd_tasks\) ")
self.char1.execute_cmd("y") self.char1.execute_cmd("y")
self.assertTrue(self.task.paused) self.assertTrue(self.task.paused)
self.task_handler.clock.advance(self.timedelay + 1) self.task_handler.clock.advance(self.timedelay + 1)
@ -455,7 +447,7 @@ class TestCmdTasks(BaseEvenniaCommandTest):
self.assertTrue(self.task.exists()) self.assertTrue(self.task.exists())
wanted_msg = "Unpause task 1 with completion date" wanted_msg = "Unpause task 1 with completion date"
cmd_result = self.call(system.CmdTasks(), args, wanted_msg) cmd_result = self.call(system.CmdTasks(), args, wanted_msg)
self.assertRegex(cmd_result, " \(func_test_cmd_tasks\) ") self.assertRegex(cmd_result, r" \(func_test_cmd_tasks\) ")
self.char1.execute_cmd("y") self.char1.execute_cmd("y")
# verify task continues after unpause # verify task continues after unpause
self.task_handler.clock.advance(1) self.task_handler.clock.advance(1)
@ -465,7 +457,7 @@ class TestCmdTasks(BaseEvenniaCommandTest):
args = f"/do_task {self.task.get_id()}" args = f"/do_task {self.task.get_id()}"
wanted_msg = "Do_task task 1 with completion date" wanted_msg = "Do_task task 1 with completion date"
cmd_result = self.call(system.CmdTasks(), args, wanted_msg) cmd_result = self.call(system.CmdTasks(), args, wanted_msg)
self.assertRegex(cmd_result, " \(func_test_cmd_tasks\) ") self.assertRegex(cmd_result, r" \(func_test_cmd_tasks\) ")
self.char1.execute_cmd("y") self.char1.execute_cmd("y")
self.assertFalse(self.task.exists()) self.assertFalse(self.task.exists())
@ -473,7 +465,7 @@ class TestCmdTasks(BaseEvenniaCommandTest):
args = f"/remove {self.task.get_id()}" args = f"/remove {self.task.get_id()}"
wanted_msg = "Remove task 1 with completion date" wanted_msg = "Remove task 1 with completion date"
cmd_result = self.call(system.CmdTasks(), args, wanted_msg) cmd_result = self.call(system.CmdTasks(), args, wanted_msg)
self.assertRegex(cmd_result, " \(func_test_cmd_tasks\) ") self.assertRegex(cmd_result, r" \(func_test_cmd_tasks\) ")
self.char1.execute_cmd("y") self.char1.execute_cmd("y")
self.assertFalse(self.task.exists()) self.assertFalse(self.task.exists())
@ -481,7 +473,7 @@ class TestCmdTasks(BaseEvenniaCommandTest):
args = f"/call {self.task.get_id()}" args = f"/call {self.task.get_id()}"
wanted_msg = "Call task 1 with completion date" wanted_msg = "Call task 1 with completion date"
cmd_result = self.call(system.CmdTasks(), args, wanted_msg) cmd_result = self.call(system.CmdTasks(), args, wanted_msg)
self.assertRegex(cmd_result, " \(func_test_cmd_tasks\) ") self.assertRegex(cmd_result, r" \(func_test_cmd_tasks\) ")
self.char1.execute_cmd("y") self.char1.execute_cmd("y")
# make certain the task is still active # make certain the task is still active
self.assertTrue(self.task.active()) self.assertTrue(self.task.active())
@ -493,7 +485,7 @@ class TestCmdTasks(BaseEvenniaCommandTest):
args = f"/cancel {self.task.get_id()}" args = f"/cancel {self.task.get_id()}"
wanted_msg = "Cancel task 1 with completion date" wanted_msg = "Cancel task 1 with completion date"
cmd_result = self.call(system.CmdTasks(), args, wanted_msg) cmd_result = self.call(system.CmdTasks(), args, wanted_msg)
self.assertRegex(cmd_result, " \(func_test_cmd_tasks\) ") self.assertRegex(cmd_result, r" \(func_test_cmd_tasks\) ")
self.char1.execute_cmd("y") self.char1.execute_cmd("y")
self.assertTrue(self.task.exists()) self.assertTrue(self.task.exists())
self.assertFalse(self.task.active()) self.assertFalse(self.task.active())
@ -797,7 +789,7 @@ class TestBuilding(BaseEvenniaCommandTest):
self.call( self.call(
building.CmdExamine(), building.CmdExamine(),
"self/test2", "self/test2",
"Attribute Char/test2 [category=None]:\n\nthis is a \$random() value.", "Attribute Char/test2 [category=None]:\n\nthis is a \\$random() value.",
) )
self.room1.scripts.add(self.script.__class__) self.room1.scripts.add(self.script.__class__)
@ -805,6 +797,13 @@ class TestBuilding(BaseEvenniaCommandTest):
self.account.scripts.add(self.script.__class__) self.account.scripts.add(self.script.__class__)
self.call(building.CmdExamine(), "*TestAccount") self.call(building.CmdExamine(), "*TestAccount")
self.char1.attributes.add("strattr", "testval", strattr=True)
self.call(
building.CmdExamine(),
"self/strattr",
"Attribute Char/strattr [category=None] [strvalue]:\n\ntestval",
)
def test_set_obj_alias(self): def test_set_obj_alias(self):
self.call(building.CmdSetObjAlias(), "Obj =", "Cleared aliases from Obj") self.call(building.CmdSetObjAlias(), "Obj =", "Cleared aliases from Obj")
self.call( self.call(

View file

@ -17,7 +17,6 @@ from copy import copy
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from evennia.locks.lockhandler import LockHandler from evennia.locks.lockhandler import LockHandler
from evennia.utils.dbserialize import from_pickle, to_pickle from evennia.utils.dbserialize import from_pickle, to_pickle
from evennia.utils.idmapper.models import SharedMemoryModel from evennia.utils.idmapper.models import SharedMemoryModel
@ -142,6 +141,8 @@ class InMemoryAttribute(IAttribute):
# Value and locks are special. We must call the wrappers. # Value and locks are special. We must call the wrappers.
if key == "value": if key == "value":
self.value = value self.value = value
elif key == "strvalue":
self.db_strvalue = value
elif key == "lock_storage": elif key == "lock_storage":
self.lock_storage = value self.lock_storage = value
else: else: