From 8bd3706994843b31900975825e19aec35641e92d Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Wed, 23 Mar 2022 21:39:43 +0000 Subject: Add BotBase that will act as a base for all our bots This commit also modifies the extensions util, since it's now directly used by bot-core. Co-authored-by: Mark <1515135+MarkKoz@users.noreply.github.com> Co-authored-by: Hassan Abouelela --- botcore/utils/__init__.py | 3 +-- botcore/utils/_extensions.py | 49 +++++++++++++++++++++++++++++++++++++++++ botcore/utils/extensions.py | 52 -------------------------------------------- 3 files changed, 50 insertions(+), 54 deletions(-) create mode 100644 botcore/utils/_extensions.py delete mode 100644 botcore/utils/extensions.py (limited to 'botcore/utils') diff --git a/botcore/utils/__init__.py b/botcore/utils/__init__.py index fe906075..7e6ea788 100644 --- a/botcore/utils/__init__.py +++ b/botcore/utils/__init__.py @@ -1,6 +1,6 @@ """Useful utilities and tools for Discord bot development.""" -from botcore.utils import _monkey_patches, caching, channel, extensions, logging, members, regex, scheduling +from botcore.utils import _monkey_patches, caching, channel, logging, members, regex, scheduling def apply_monkey_patches() -> None: @@ -23,7 +23,6 @@ __all__ = [ apply_monkey_patches, caching, channel, - extensions, logging, members, regex, diff --git a/botcore/utils/_extensions.py b/botcore/utils/_extensions.py new file mode 100644 index 00000000..6848fae6 --- /dev/null +++ b/botcore/utils/_extensions.py @@ -0,0 +1,49 @@ +"""Utilities for loading Discord extensions.""" + +import importlib +import inspect +import pkgutil +import types +from typing import NoReturn + + +def unqualify(name: str) -> str: + """ + Return an unqualified name given a qualified module/package ``name``. + + Args: + name: The module name to unqualify. + + Returns: + The unqualified module name. + """ + return name.rsplit(".", maxsplit=1)[-1] + + +def walk_extensions(module: types.ModuleType) -> frozenset[str]: + """ + Yield extension names from the given module. + + Args: + module (types.ModuleType): The module to look for extensions in. + + Returns: + An iterator object, that returns a string that can be passed directly to + :obj:`discord.ext.commands.Bot.load_extension` on call to next(). + """ + + def on_error(name: str) -> NoReturn: + raise ImportError(name=name) # pragma: no cover + + for module_info in pkgutil.walk_packages(module.__path__, f"{module.__name__}.", onerror=on_error): + if unqualify(module_info.name).startswith("_"): + # Ignore module/package names starting with an underscore. + continue + + if module_info.ispkg: + imported = importlib.import_module(module_info.name) + if not inspect.isfunction(getattr(imported, "setup", None)): + # If it lacks a setup function, it's not an extension. + continue + + yield module_info.name diff --git a/botcore/utils/extensions.py b/botcore/utils/extensions.py deleted file mode 100644 index 841120c9..00000000 --- a/botcore/utils/extensions.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Utilities for loading Discord extensions.""" - -import importlib -import inspect -import pkgutil -import types -from typing import NoReturn - - -def unqualify(name: str) -> str: - """ - Return an unqualified name given a qualified module/package ``name``. - - Args: - name: The module name to unqualify. - - Returns: - The unqualified module name. - """ - return name.rsplit(".", maxsplit=1)[-1] - - -def walk_extensions(module: types.ModuleType) -> frozenset[str]: - """ - Yield extension names from the given module. - - Args: - module (types.ModuleType): The module to look for extensions in. - - Returns: - A set of strings that can be passed directly to :obj:`discord.ext.commands.Bot.load_extension`. - """ - - def on_error(name: str) -> NoReturn: - raise ImportError(name=name) # pragma: no cover - - modules = set() - - for module_info in pkgutil.walk_packages(module.__path__, f"{module.__name__}.", onerror=on_error): - if unqualify(module_info.name).startswith("_"): - # Ignore module/package names starting with an underscore. - continue - - if module_info.ispkg: - imported = importlib.import_module(module_info.name) - if not inspect.isfunction(getattr(imported, "setup", None)): - # If it lacks a setup function, it's not an extension. - continue - - modules.add(module_info.name) - - return frozenset(modules) -- cgit v1.2.3