From fd0a40442a9745d77c8252ea72d82dcfe1d70dd0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 11 Jun 2019 18:31:38 +0200 Subject: [PATCH] Fix plugins not getting reloaded properly when restarting a client --- pyrogram/client/client.py | 59 ++++++++++++++++++++----------- pyrogram/client/ext/dispatcher.py | 1 + 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 6dc8ce97..fd5db423 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -1085,29 +1085,34 @@ class Client(Methods, BaseClient): self._proxy["password"] = parser.get("proxy", "password", fallback=None) or None if self.plugins: - self.plugins["enabled"] = bool(self.plugins.get("enabled", True)) - self.plugins["include"] = "\n".join(self.plugins.get("include", [])) or None - self.plugins["exclude"] = "\n".join(self.plugins.get("exclude", [])) or None + self.plugins = { + "enabled": bool(self.plugins.get("enabled", True)), + "root": self.plugins.get("root", None), + "include": self.plugins.get("include", []), + "exclude": self.plugins.get("exclude", []) + } else: try: section = parser["plugins"] self.plugins = { "enabled": section.getboolean("enabled", True), - "root": section.get("root"), - "include": section.get("include") or None, - "exclude": section.get("exclude") or None + "root": section.get("root", None), + "include": section.get("include", []), + "exclude": section.get("exclude", []) } - except KeyError: - self.plugins = {} - if self.plugins: - for option in ["include", "exclude"]: - if self.plugins[option] is not None: - self.plugins[option] = [ - (i.split()[0], i.split()[1:] or None) - for i in self.plugins[option].strip().split("\n") - ] + include = self.plugins["include"] + exclude = self.plugins["exclude"] + + if include: + self.plugins["include"] = include.strip().split("\n") + + if exclude: + self.plugins["exclude"] = exclude.strip().split("\n") + + except KeyError: + self.plugins = None def load_session(self): try: @@ -1142,14 +1147,26 @@ class Client(Methods, BaseClient): self.peers_by_phone[k] = peer def load_plugins(self): - if self.plugins.get("enabled", False): - root = self.plugins["root"] - include = self.plugins["include"] - exclude = self.plugins["exclude"] + if self.plugins: + plugins = self.plugins.copy() + + for option in ["include", "exclude"]: + if plugins[option]: + plugins[option] = [ + (i.split()[0], i.split()[1:] or None) + for i in self.plugins[option] + ] + else: + return + + if plugins.get("enabled", False): + root = plugins["root"] + include = plugins["include"] + exclude = plugins["exclude"] count = 0 - if include is None: + if not include: for path in sorted(Path(root).rglob("*.py")): module_path = '.'.join(path.parent.parts + (path.stem,)) module = import_module(module_path) @@ -1206,7 +1223,7 @@ class Client(Methods, BaseClient): log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format( self.session_name, name, module_path)) - if exclude is not None: + if exclude: for path, handlers in exclude: module_path = root + "." + path warn_non_existent_functions = True diff --git a/pyrogram/client/ext/dispatcher.py b/pyrogram/client/ext/dispatcher.py index 2f1ec2b9..a15cb299 100644 --- a/pyrogram/client/ext/dispatcher.py +++ b/pyrogram/client/ext/dispatcher.py @@ -106,6 +106,7 @@ class Dispatcher: worker.join() self.workers_list.clear() + self.groups.clear() def add_handler(self, handler, group: int): if group not in self.groups: