Skip to content

Commit

Permalink
refactor: Rename middleware to extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
pmdevita committed Nov 10, 2023
1 parent 1c9cf89 commit 6ad19be
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions atsume/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from atsume.db.manager import database
from atsume.component.manager import manager as component_manager
from atsume.component import Component, ComponentConfig
from atsume.middleware.loader import attach_middleware, load_module_setting
from atsume.extensions.loader import attach_extensions, load_module_setting
from atsume.utils import module_to_path


Expand Down Expand Up @@ -92,7 +92,7 @@ def create_bot(
1. :py:func:`initialize_atsume`
2. :py:func:`initialize_discord`
3. :py:func:`atsume.middleware.loader.attach_middleware`
3. :py:func:`atsume.extensions.loader.attach_extensions`
4. :py:func:`load_components`
:param bot_module: The module path for the Atsume project to start.
Expand All @@ -101,7 +101,7 @@ def create_bot(
"""
initialize_atsume(bot_module)
bot, client = initialize_discord()
attach_middleware(client)
attach_extensions(client)
load_components(client)
return bot

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def hook_aiohttp(c: alluka.Injected[tanjun.abc.Client]) -> None:
"""
Create an `aiohttp.ClientSession` instance for an Atsume bot to use.
"""

@c.with_client_callback(tanjun.ClientCallbackNames.STARTING)
async def on_starting(client: alluka.Injected[tanjun.abc.Client]) -> None:
client.set_type_dependency(aiohttp.ClientSession, aiohttp.ClientSession())
Expand Down
20 changes: 10 additions & 10 deletions atsume/middleware/loader.py → atsume/extensions/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

from atsume.settings import settings

# Middleware that Atsume always needs to load
ATSUME_MIDDLEWARE = ["atsume.db.manager.hook_database"]
# Extensions that Atsume always needs to load
ATSUME_EXTENSIONS = ["atsume.db.manager.hook_database"]


class MiddlewareCallable:
class ExtensionCallable:
def __call__(self, client: tanjun.Client) -> None:
...


def attach_middleware(client: tanjun.Client) -> None:
def attach_extensions(client: tanjun.Client) -> None:
"""
Load the middleware modules from the Atsume project settings and hook them
Load the extension modules from the Atsume project settings and hook them
on to the given `tanjun.Client`.
"""
middleware = set()
middleware.update(settings.MIDDLEWARE)
middleware.update(ATSUME_MIDDLEWARE)
for module_path in middleware:
func = load_module_setting(module_path, MiddlewareCallable)
extensions = set()
extensions.update(settings.EXTENSIONS)
extensions.update(ATSUME_EXTENSIONS)
for module_path in extensions:
func = load_module_setting(module_path, ExtensionCallable)
func(client)


Expand Down
2 changes: 1 addition & 1 deletion atsume/settings/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

COMPONENT_GUILD_PERMISSIONS = {}

MIDDLEWARE = []
EXTENSIONS = []

HIKARI_LOGGING = False

Expand Down
8 changes: 4 additions & 4 deletions atsume/settings/type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
INTENTS = hikari.
MIDDLEWARE = [
"atsume.middleware.aiohttp"
EXTENSIONS = [
"atsume.extensions.aiohttp"
]
"""
Expand Down Expand Up @@ -83,8 +83,8 @@
DATABASE_URL: str
"""A database URL for Ormar to use to connect to the database."""

MIDDLEWARE: list[str]
"""A list of middleware module paths to load."""
EXTENSIONS: list[str]
"""A list of extension module paths to load."""

DISABLE_UVLOOP: bool
"""Prevent running with uvloop even if installed. (default: False)"""
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

COMPONENTS = []

MIDDLEWARE = []
EXTENSIONS = []

HIKARI_LOGGING = False

Expand Down

0 comments on commit 6ad19be

Please sign in to comment.