From bc1af48439c744eb125e0dbe69cd25877ce46fdc Mon Sep 17 00:00:00 2001 From: wulan17 Date: Tue, 10 Sep 2024 14:54:54 +0700 Subject: [PATCH] pyrofork: Ignore excluded plugins Signed-off-by: wulan17 --- pyrogram/client.py | 56 +++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/pyrogram/client.py b/pyrogram/client.py index 82c72df6b..7ce641021 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -865,11 +865,26 @@ def load_plugins(self): root = plugins["root"] include = plugins.get("include", []) exclude = plugins.get("exclude", []) + exclude_plugins = [] + exclude_dirs = [] + if exclude: + for i in exclude: + plugin = root.replace(".", "/") + "/" + i.replace(".", "/") + if Path(plugin).is_dir(): + exclude_dirs.append(plugin + "/") + else: + exclude_plugins.append(plugin + ".py") count = 0 if not include: for path in sorted(Path(root.replace(".", "/")).rglob("*.py")): + if exclude_dirs and any(str(path).startswith(i) for i in exclude_dirs): + log.info(f"[{self.name}] [LOAD] Ignoring excluded plugins {path}") + continue + if exclude_plugins and str(path) in exclude_plugins: + log.info(f"[{self.name}] [LOAD] Ignoring excluded plugins {path}") + continue module_path = '.'.join(path.parent.parts + (path.stem,)) module = import_module(module_path) @@ -889,6 +904,12 @@ def load_plugins(self): else: for path, handlers in include: module_path = root.replace("/",".") + "." + path + if exclude_dirs and any(module_path.startswith(i) for i in exclude_dirs): + log.info(f"[{self.name}] [LOAD] Ignoring excluded plugins {module_path}") + continue + if exclude_plugins and module_path+".py" in exclude_plugins: + log.info(f"[{self.name}] [LOAD] Ignoring excluded plugins {module_path}") + continue warn_non_existent_functions = True try: @@ -921,41 +942,6 @@ def load_plugins(self): log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format( self.name, name, module_path)) - if exclude: - for path, handlers in exclude: - module_path = root.replace("/",".") + "." + path - warn_non_existent_functions = True - - try: - module = import_module(module_path) - except ImportError: - log.warning('[%s] [UNLOAD] Ignoring non-existent module "%s"', self.name, module_path) - continue - - if "__path__" in dir(module): - log.warning('[%s] [UNLOAD] Ignoring namespace "%s"', self.name, module_path) - continue - - if handlers is None: - handlers = vars(module).keys() - warn_non_existent_functions = False - - for name in handlers: - # noinspection PyBroadException - try: - for handler, group in getattr(module, name).handlers: - if isinstance(handler, Handler) and isinstance(group, int): - self.remove_handler(handler, group) - - log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format( - self.name, type(handler).__name__, name, group, module_path)) - - count -= 1 - except Exception: - if warn_non_existent_functions: - log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format( - self.name, name, module_path)) - if count > 0: log.info('[{}] Successfully loaded {} plugin{} from "{}"'.format( self.name, count, "s" if count > 1 else "", root))