diff options
| author | 2020-11-30 15:18:31 +0100 | |
|---|---|---|
| committer | 2020-11-30 15:18:31 +0100 | |
| commit | 04d9cf9583c9e54bb4a45f867e02df9da1bbc357 (patch) | |
| tree | 3910b05cdcfa86253b941a1d1c3ff810f35d827e /bot/utils/extensions.py | |
| parent | Set precision to hours (diff) | |
| parent | Merge pull request #532 from python-discord/sebastiaan/ci/add-core-dev-approv... (diff) | |
Merge branch 'master' into master
Diffstat (limited to 'bot/utils/extensions.py')
| -rw-r--r-- | bot/utils/extensions.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/bot/utils/extensions.py b/bot/utils/extensions.py new file mode 100644 index 00000000..50350ea8 --- /dev/null +++ b/bot/utils/extensions.py @@ -0,0 +1,34 @@ +import importlib +import inspect +import pkgutil +from typing import Iterator, NoReturn + +from bot import exts + + +def unqualify(name: str) -> str: + """Return an unqualified name given a qualified module/package `name`.""" + return name.rsplit(".", maxsplit=1)[-1] + + +def walk_extensions() -> Iterator[str]: + """Yield extension names from the bot.exts subpackage.""" + + def on_error(name: str) -> NoReturn: + raise ImportError(name=name) # pragma: no cover + + for module in pkgutil.walk_packages(exts.__path__, f"{exts.__name__}.", onerror=on_error): + if unqualify(module.name).startswith("_"): + # Ignore module/package names starting with an underscore. + continue + + if module.ispkg: + imported = importlib.import_module(module.name) + if not inspect.isfunction(getattr(imported, "setup", None)): + # If it lacks a setup function, it's not an extension. + continue + + yield module.name + + +EXTENSIONS = frozenset(walk_extensions()) |