aboutsummaryrefslogtreecommitdiffstats
path: root/bot
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-28 15:41:26 +0100
committerGravatar kwzrd <[email protected]>2020-03-28 15:46:49 +0100
commitb82d104130f6859bee620505b3ac04c3c0680600 (patch)
treef16bd86e898885aed90c3a0bb618b08802b70aa9 /bot
parentDeseasonify: make `get_package_names` an iterator (diff)
Deseasonify: make `get_extensions` an iterator
Simplify internal structure & adjust var naming.
Diffstat (limited to 'bot')
-rw-r--r--bot/__main__.py4
-rw-r--r--bot/exts/__init__.py23
2 files changed, 15 insertions, 12 deletions
diff --git a/bot/__main__.py b/bot/__main__.py
index 9a981c5c..8b6ee782 100644
--- a/bot/__main__.py
+++ b/bot/__main__.py
@@ -2,14 +2,14 @@ import logging
from bot.bot import bot
from bot.constants import Client, STAFF_ROLES, WHITELISTED_CHANNELS
-from bot.exts import get_extensions
+from bot.exts import walk_extensions
from bot.utils.decorators import in_channel_check
log = logging.getLogger(__name__)
bot.add_check(in_channel_check(*WHITELISTED_CHANNELS, bypass_roles=STAFF_ROLES))
-for ext in get_extensions():
+for ext in walk_extensions():
bot.load_extension(ext)
bot.run(Client.token)
diff --git a/bot/exts/__init__.py b/bot/exts/__init__.py
index 4f19c167..25deb9af 100644
--- a/bot/exts/__init__.py
+++ b/bot/exts/__init__.py
@@ -1,9 +1,9 @@
import logging
import pkgutil
from pathlib import Path
-from typing import Iterator, List
+from typing import Iterator
-__all__ = ("get_package_names", "get_extensions")
+__all__ = ("get_package_names", "walk_extensions")
log = logging.getLogger(__name__)
@@ -15,18 +15,21 @@ def get_package_names() -> Iterator[str]:
yield package.name
-def get_extensions() -> List[str]:
+def walk_extensions() -> Iterator[str]:
"""
- Give a list of dot-separated paths to all extensions.
+ Iterate dot-separated paths to all extensions.
The strings are formatted in a way such that the bot's `load_extension`
method can take them. Use this to load all available extensions.
+
+ This intentionally doesn't make use of pkgutil's `walk_packages`, as we only
+ want to build paths to extensions - not recursively all modules. For some
+ extensions, the `setup` function is in the package's __init__ file, while
+ modules nested under the package are only helpers. Constructing the paths
+ ourselves serves our purpose better.
"""
base_path = Path(__path__[0])
- extensions = []
-
- for season in get_package_names():
- for module in pkgutil.iter_modules([base_path.joinpath(season)]):
- extensions.append(f"bot.exts.{season}.{module.name}")
- return extensions
+ for package in get_package_names():
+ for extension in pkgutil.iter_modules([base_path.joinpath(package)]):
+ yield f"bot.exts.{package}.{extension.name}"