mirror of
https://github.com/evennia/evennia.git
synced 2026-03-17 05:16:31 +01:00
Updated the Command class with set_key/set_alias methods to allow for dynamic updating of command key/aliases properties while making sure all optimization recaches are hit.
This commit is contained in:
parent
a8332fe431
commit
ea0de32607
1 changed files with 47 additions and 0 deletions
|
|
@ -217,6 +217,53 @@ class Command(object):
|
|||
"""
|
||||
return any(query in keyalias for keyalias in self._keyaliases)
|
||||
|
||||
def _optimize(self):
|
||||
"""
|
||||
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)
|
||||
# optimization for looping over keys+aliases
|
||||
self._keyaliases = tuple(self._matchset)
|
||||
|
||||
def set_key(self, new_key):
|
||||
"""
|
||||
Update key.
|
||||
|
||||
Args:
|
||||
new_key (str): The new key.
|
||||
|
||||
Notes:
|
||||
This is necessary to use to make sure the optimization
|
||||
caches are properly updated as well.
|
||||
|
||||
"""
|
||||
self.key = new_key.lower()
|
||||
self._optimize()
|
||||
|
||||
def set_aliases(self, new_aliases):
|
||||
"""
|
||||
Update aliases.
|
||||
|
||||
Args:
|
||||
new_aliases (list):
|
||||
|
||||
Notes:
|
||||
This is necessary to use to make sure the optimization
|
||||
caches are properly updated as well.
|
||||
|
||||
"""
|
||||
if not is_iter(new_aliases):
|
||||
try:
|
||||
self.aliases = [str(alias).strip().lower()
|
||||
for alias in self.aliases.split(',')]
|
||||
except Exception:
|
||||
self.aliases = []
|
||||
self.aliases = list(set(alias for alias in self.aliases
|
||||
if alias and alias != self.key))
|
||||
self._optimize()
|
||||
|
||||
|
||||
def match(self, cmdname):
|
||||
"""
|
||||
This is called by the system when searching the available commands,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue