From ea0de32607f5eb525034ffbafa2c65c8a2eb6a22 Mon Sep 17 00:00:00 2001 From: Griatch Date: Tue, 7 Jul 2015 16:42:20 +0200 Subject: [PATCH] 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. --- evennia/commands/command.py | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/evennia/commands/command.py b/evennia/commands/command.py index eaf435e5df..bd6b28dad3 100644 --- a/evennia/commands/command.py +++ b/evennia/commands/command.py @@ -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,