From b926f1e59d1f1d5ee3377127244e3dee62ab6eb9 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 18 Jan 2019 21:25:53 +0200 Subject: [PATCH] Fix active command handlers for multi-instance plugins --- maubot/handlers/command.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/maubot/handlers/command.py b/maubot/handlers/command.py index 9d4fcfa..202dd77 100644 --- a/maubot/handlers/command.py +++ b/maubot/handlers/command.py @@ -55,8 +55,27 @@ class CommandHandler: self.__mb_event_handler__: bool = True self.__mb_event_type__: EventType = EventType.ROOM_MESSAGE self.__mb_msgtypes__: List[MessageType] = (MessageType.TEXT,) + self.__instance_vars: Dict[str, CommandHandler] = {} self.__class_instance: Any = None + def __copy__(self) -> 'CommandHandler': + new_ch = type(self)(self.__mb_func__) + keys = ["parent", "subcommands", "arguments", "help", "get_name", "is_command_match", + "require_subcommand", "arg_fallthrough", "event_handler", "event_type", "msgtypes"] + for key in keys: + key = f"__mb_${key}__" + setattr(new_ch, key, getattr(self, key)) + return new_ch + + def __get__(self, instance, instancetype): + try: + return self.__instance_vars[instance] + except KeyError: + copy = self.__copy__() + copy.__class_instance = instance + self.__instance_vars[instance] = copy + return copy + @staticmethod def __command_match_unset(self, val: str) -> str: raise NotImplementedError("Hmm") @@ -117,10 +136,6 @@ class CommandHandler: return False, remaining_val return True, remaining_val - def __get__(self, instance, instancetype): - self.__class_instance = instance - return self - @property def __mb_full_help__(self) -> str: usage = self.__mb_usage_without_subcommands__ + "\n\n"