Resolve merge conflicts

This commit is contained in:
Griatch 2022-08-02 16:19:25 +02:00
commit 19bd7ce0b7
19 changed files with 32231 additions and 105 deletions

View file

@ -61,38 +61,14 @@ def build_matches(raw_string, cmdset, include_prefixes=False):
"""
matches = []
try:
if include_prefixes:
# use the cmdname as-is
l_raw_string = raw_string.lower()
for cmd in cmdset:
matches.extend(
[
create_match(cmdname, raw_string, cmd, cmdname)
for cmdname in [cmd.key] + cmd.aliases
if cmdname
and l_raw_string.startswith(cmdname.lower())
and (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname) :]))
]
)
else:
# strip prefixes set in settings
raw_string = (
raw_string.lstrip(_CMD_IGNORE_PREFIXES) if len(raw_string) > 1 else raw_string
)
l_raw_string = raw_string.lower()
for cmd in cmdset:
for raw_cmdname in [cmd.key] + cmd.aliases:
cmdname = (
raw_cmdname.lstrip(_CMD_IGNORE_PREFIXES)
if len(raw_cmdname) > 1
else raw_cmdname
)
if (
cmdname
and l_raw_string.startswith(cmdname.lower())
and (not cmd.arg_regex or cmd.arg_regex.match(l_raw_string[len(cmdname) :]))
):
matches.append(create_match(cmdname, raw_string, cmd, raw_cmdname))
orig_string = raw_string
if not include_prefixes and len(raw_string) > 1:
raw_string = raw_string.lstrip(_CMD_IGNORE_PREFIXES)
search_string = raw_string.lower()
for cmd in cmdset:
cmdname, raw_cmdname = cmd.match(search_string, include_prefixes=include_prefixes)
if cmdname:
matches.append(create_match(cmdname, raw_string, cmd, raw_cmdname))
except Exception:
log_trace("cmdhandler error. raw_input:%s" % raw_string)
return matches

View file

@ -27,6 +27,7 @@ Set theory.
"""
from weakref import WeakKeyDictionary
from django.utils.translation import gettext as _
from evennia.utils.utils import inherits_from, is_iter
@ -546,10 +547,7 @@ class CmdSet(object, metaclass=_CmdSetMeta):
commands[ic] = cmd # replace
except ValueError:
commands.append(cmd)
self.commands = commands
if not allow_duplicates:
# extra run to make sure to avoid doublets
self.commands = list(set(self.commands))
# add system_command to separate list as well,
# for quick look-up
if cmd.key.startswith("__"):
@ -559,6 +557,11 @@ class CmdSet(object, metaclass=_CmdSetMeta):
except ValueError:
system_commands.append(cmd)
self.commands = commands
if not allow_duplicates:
# extra run to make sure to avoid doublets
self.commands = list(set(self.commands))
def remove(self, cmd):
"""
Remove a command instance from the cmdset.
@ -568,6 +571,15 @@ class CmdSet(object, metaclass=_CmdSetMeta):
or the key of such a command.
"""
if isinstance(cmd, str):
_cmd = next((_cmd for _cmd in self.commands if _cmd.key == cmd), None)
if _cmd is None:
if not cmd.startswith("__"):
# if a syscommand, keep the original string and instantiate on it
return None
else:
cmd = _cmd
cmd = self._instantiate(cmd)
if cmd.key.startswith("__"):
try:
@ -591,6 +603,15 @@ class CmdSet(object, metaclass=_CmdSetMeta):
cmd (Command): The first matching Command in the set.
"""
if isinstance(cmd, str):
_cmd = next((_cmd for _cmd in self.commands if _cmd.key == cmd), None)
if _cmd is None:
if not cmd.startswith("__"):
# if a syscommand, keep the original string and instantiate on it
return None
else:
cmd = _cmd
cmd = self._instantiate(cmd)
for thiscmd in self.commands:
if thiscmd == cmd:

View file

@ -219,6 +219,7 @@ class Command(metaclass=CommandMeta):
"""
if kwargs:
_init_command(self, **kwargs)
self._optimize()
@lazy_property
def lockhandler(self):
@ -295,10 +296,15 @@ class Command(metaclass=CommandMeta):
Optimize the key and aliases for lookups.
"""
# optimization - a set is much faster to match against than a list
self._matchset = set([self.key] + self.aliases)
matches = [self.key.lower()]
matches.extend(x.lower() for x in self.aliases)
self._matchset = set(matches)
# optimization for looping over keys+aliases
self._keyaliases = tuple(self._matchset)
self._noprefix_aliases = {x.lstrip(CMD_IGNORE_PREFIXES): x for x in matches}
def set_key(self, new_key):
"""
Update key.
@ -334,7 +340,7 @@ class Command(metaclass=CommandMeta):
self.aliases = list(set(alias for alias in aliases if alias != self.key))
self._optimize()
def match(self, cmdname):
def match(self, cmdname, include_prefixes=True):
"""
This is called by the system when searching the available commands,
in order to determine if this is the one we wanted. cmdname was
@ -343,11 +349,23 @@ class Command(metaclass=CommandMeta):
Args:
cmdname (str): Always lowercase when reaching this point.
Kwargs:
include_prefixes (bool): If false, will compare against the _noprefix
variants of commandnames.
Returns:
result (bool): Match result.
"""
return cmdname in self._matchset
if include_prefixes:
for cmd_key in self._keyaliases:
if cmdname.startswith(cmd_key) and (not self.arg_regex or self.arg_regex.match(cmdname[len(cmd_key) :])):
return cmd_key, cmd_key
else:
for k, v in self._noprefix_aliases.items():
if cmdname.startswith(k) and (not self.arg_regex or self.arg_regex.match(cmdname[len(k) :])):
return k, v
return None, None
def access(self, srcobj, access_type="cmd", default=False):
"""

View file

@ -1927,7 +1927,7 @@ class CmdSetAttribute(ObjManipCommand):
if self.rhs is None:
# no = means we inspect the attribute(s)
if not attrs:
attrs = [attr.key for attr in obj.attributes.get(category=None, return_obj=True)]
attrs = [attr.key for attr in obj.attributes.get(category=None, return_obj=True, return_list=True)]
for attr in attrs:
if not self.check_attr(obj, attr, category):
continue

View file

@ -874,14 +874,14 @@ class CmdSetHelp(CmdHelp):
if isinstance(match, HelpCategory):
warning = (
f"'{querystr}' matches (or partially matches) the name of "
"help-category '{match.key}'. If you continue, your help entry will "
f"help-category '{match.key}'. If you continue, your help entry will "
"take precedence and the category (or part of its name) *may* not "
"be usable for grouping help entries anymore."
)
elif inherits_from(match, "evennia.commands.command.Command"):
warning = (
f"'{querystr}' matches (or partially matches) the key/alias of "
"Command '{match.key}'. Command-help take precedence over other "
f"Command '{match.key}'. Command-help take precedence over other "
"help entries so your help *may* be impossible to reach for those "
"with access to that command."
)

View file

@ -1199,3 +1199,21 @@ class TestCmdSetNesting(BaseEvenniaTest):
cmd = self.char1.cmdset.cmdset_stack[-1].commands[0]
self.assertEqual(cmd.obj, self.char1)
class TestCmdSet(BaseEvenniaTest):
"""
General tests for cmdsets
"""
def test_cmdset_remove_by_key(self):
test_cmd_set = _CmdSetTest()
test_cmd_set.remove("another command")
self.assertNotIn(_CmdTest2, test_cmd_set.commands)
def test_cmdset_gets_by_key(self):
test_cmd_set = _CmdSetTest()
result = test_cmd_set.get("another command")
self.assertIsInstance(result, _CmdTest2)