azils3/bash
0
1# Ultroid - UserBot2# Copyright (C) 2021-2025 TeamUltroid3#4# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >5# PLease read the GNU Affero General Public License in6# <https://github.com/TeamUltroid/pyUltroid/blob/main/LICENSE>.7 8import contextlib9import glob10import os11from importlib import import_module12from logging import Logger13 14from . import LOGS15from .fns.tools import get_all_files16 17 18class Loader:19 def __init__(self, path="plugins", key="Official", logger: Logger = LOGS):20 self.path = path21 self.key = key22 self._logger = logger23 24 def load(25 self,26 log=True,27 func=import_module,28 include=None,29 exclude=None,30 after_load=None,31 load_all=False,32 ):33 _single = os.path.isfile(self.path)34 if include:35 if log:36 self._logger.info("Including: {}".format("• ".join(include)))37 files = glob.glob(f"{self.path}/_*.py")38 for file in include:39 path = f"{self.path}/{file}.py"40 if os.path.exists(path):41 files.append(path)42 elif _single:43 files = [self.path]44 else:45 if load_all:46 files = get_all_files(self.path, ".py")47 else:48 files = glob.glob(f"{self.path}/*.py")49 if exclude:50 for path in exclude:51 if not path.startswith("_"):52 with contextlib.suppress(ValueError):53 files.remove(f"{self.path}/{path}.py")54 if log and not _single:55 self._logger.info(56 f"• Installing {self.key} Plugins || Count : {len(files)} •"57 )58 for plugin in sorted(files):59 if func == import_module:60 plugin = plugin.replace(".py", "").replace("/", ".").replace("\\", ".")61 try:62 modl = func(plugin)63 except ModuleNotFoundError as er:64 modl = None65 self._logger.error(f"{plugin}: '{er.name}' not installed!")66 continue67 except Exception as exc:68 modl = None69 self._logger.error(f"pyUltroid - {self.key} - ERROR - {plugin}")70 self._logger.exception(exc)71 continue72 if _single and log:73 self._logger.info(f"Successfully Loaded {plugin}!")74 if callable(after_load):75 if func == import_module:76 plugin = plugin.split(".")[-1]77 after_load(self, modl, plugin_name=plugin)78 