Skip to content

feat: add load_extensions method #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions interactions/client/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -2002,6 +2004,35 @@ def load_extension(
module = importlib.import_module(module_name, package)
self.__load_module(module, module_name, **load_kwargs)

def load_extensions(
self,
*packages: str,
recursive: bool = False,
) -> None:
"""
Load multiple extensions at once.

Removes the need of manually looping through the package
and loading the extensions.

Args:
*packages: The package(s) where the extensions are located.
recursive: Whether to load extensions from the subdirectories within the package.
"""
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)]

for ext in extensions:
self.load_extension(ext)

def unload_extension(
self, name: str, package: str | None = None, force: bool = False, **unload_kwargs: Any
) -> None:
Expand Down