From 5b5001ce6fc827e165a1ed654a34ca0e096ff334 Mon Sep 17 00:00:00 2001 From: Maksims K Date: Sat, 12 Aug 2023 20:54:39 +0100 Subject: [PATCH 1/5] feat: add load_extensions method --- interactions/client/client.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/interactions/client/client.py b/interactions/client/client.py index f6a530abd..f698fd612 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -1,9 +1,11 @@ import asyncio import contextlib import functools +import glob import importlib.util import inspect import logging +import os import re import sys import time @@ -2001,6 +2003,21 @@ def load_extension( module = importlib.import_module(module_name, package) self.__load_module(module, module_name, **load_kwargs) + + def load_extensions( + self, + package: str, + recursive: bool = False, + ) -> None: + # If recursive then include subdirectories ('**') + # otherwise just the package specified by the user. + pattern = os.path.join(package, "**" if recursive else "", "*.py") + + # Find all files matching the pattern, and convert slashes to dots. + extensions = [f.replace(os.path.sep, ".").replace(".py", "") for f in glob.glob(pattern, recursive=True)] + + for ext in extensions: + self.load_extension(ext) def unload_extension( self, name: str, package: str | None = None, force: bool = False, **unload_kwargs: Any From 230397f3cae6a5926b75c8b7d97987bbd3c0e3b5 Mon Sep 17 00:00:00 2001 From: Maksims K Date: Sat, 12 Aug 2023 20:55:36 +0100 Subject: [PATCH 2/5] chore: add documentation --- interactions/client/client.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/interactions/client/client.py b/interactions/client/client.py index f698fd612..26434e53e 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -2009,6 +2009,16 @@ def load_extensions( package: str, recursive: bool = False, ) -> None: + """ + Load multiple extensions at once. + + Removes the need of manually looping through the package + and loading the extensions. + + Args: + package: The package the extensions are in. + recursive: Whether to load extensions from the subdirectories within the package. + """ # If recursive then include subdirectories ('**') # otherwise just the package specified by the user. pattern = os.path.join(package, "**" if recursive else "", "*.py") From 13cf72ebc9dee318f7cb09dd43ede0cfbd046295 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 19:57:47 +0000 Subject: [PATCH 3/5] ci: correct from checks. --- interactions/client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions/client/client.py b/interactions/client/client.py index 26434e53e..f30e13f9a 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -2003,7 +2003,7 @@ def load_extension( module = importlib.import_module(module_name, package) self.__load_module(module, module_name, **load_kwargs) - + def load_extensions( self, package: str, From 9acd783808caa3e0bb392eeae22e9ac9ad439e73 Mon Sep 17 00:00:00 2001 From: Maksims K Date: Sat, 12 Aug 2023 22:11:00 +0100 Subject: [PATCH 4/5] refactor: change package parameter to *packages --- interactions/client/client.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/interactions/client/client.py b/interactions/client/client.py index 26434e53e..091219ad3 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -2003,10 +2003,10 @@ def load_extension( module = importlib.import_module(module_name, package) self.__load_module(module, module_name, **load_kwargs) - + def load_extensions( self, - package: str, + *packages: str, recursive: bool = False, ) -> None: """ @@ -2016,18 +2016,23 @@ def load_extensions( and loading the extensions. Args: - package: The package the extensions are in. + *packages: The package(s) where the extensions are located. recursive: Whether to load extensions from the subdirectories within the package. """ - # If recursive then include subdirectories ('**') - # otherwise just the package specified by the user. - pattern = os.path.join(package, "**" if recursive else "", "*.py") + if not packages: + raise ValueError("You must specify at least one package.") + + for package in packages: + # If recursive then include subdirectories ('**') + # otherwise just the package specified by the user. + pattern = os.path.join(package, "**" if recursive else "", "*.py") - # Find all files matching the pattern, and convert slashes to dots. - extensions = [f.replace(os.path.sep, ".").replace(".py", "") for f in glob.glob(pattern, recursive=True)] + # Find all files matching the pattern, and convert slashes to dots. + extensions = [f.replace(os.path.sep, ".").replace(".py", "") for f in glob.glob(pattern, recursive=True)] - for ext in extensions: - self.load_extension(ext) + for ext in extensions: + print(ext) + self.load_extension(ext) def unload_extension( self, name: str, package: str | None = None, force: bool = False, **unload_kwargs: Any From 4b1ca44cdb1f59366b7330263301bc4dfbfb1094 Mon Sep 17 00:00:00 2001 From: Maksims K Date: Sun, 13 Aug 2023 03:24:16 +0100 Subject: [PATCH 5/5] refactor: remove stray print statement --- interactions/client/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/interactions/client/client.py b/interactions/client/client.py index 091219ad3..cd18afd54 100644 --- a/interactions/client/client.py +++ b/interactions/client/client.py @@ -2031,7 +2031,6 @@ def load_extensions( extensions = [f.replace(os.path.sep, ".").replace(".py", "") for f in glob.glob(pattern, recursive=True)] for ext in extensions: - print(ext) self.load_extension(ext) def unload_extension(