Add extension reload helper (#9)

* Add reload_extension helper

* Removes unused message reaction event handling

Eliminates dead code for message reaction events that were not being processed or utilized by the application.

Cleans up the event dispatcher by removing the unused parser method and event mappings for reaction add/remove events.
This commit is contained in:
Slipstream 2025-06-10 15:37:33 -06:00 committed by GitHub
parent 29439137ef
commit 84b4e49c6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 60 additions and 7 deletions

View File

@ -56,8 +56,6 @@ class EventDispatcher:
"CHANNEL_CREATE": self._parse_channel_create,
"PRESENCE_UPDATE": self._parse_presence_update,
"TYPING_START": self._parse_typing_start,
"MESSAGE_REACTION_ADD": self._parse_message_reaction,
"MESSAGE_REACTION_REMOVE": self._parse_message_reaction,
}
def _parse_message_create(self, data: Dict[str, Any]) -> Message:
@ -75,10 +73,6 @@ class EventDispatcher:
self._client._messages.pop(message_id, None)
return data
def _parse_message_reaction(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Returns the raw reaction payload."""
return data
def _parse_interaction_create(self, data: Dict[str, Any]) -> "Interaction":
"""Parses raw INTERACTION_CREATE data into an Interaction object."""
from .interactions import Interaction

View File

@ -5,7 +5,7 @@ import sys
from types import ModuleType
from typing import Dict
__all__ = ["load_extension", "unload_extension"]
__all__ = ["load_extension", "unload_extension", "reload_extension"]
_loaded_extensions: Dict[str, ModuleType] = {}
@ -41,3 +41,14 @@ def unload_extension(name: str) -> None:
module.teardown()
sys.modules.pop(name, None)
def reload_extension(name: str) -> ModuleType:
"""Reload an extension by name.
This is a convenience wrapper around :func:`unload_extension` followed by
:func:`load_extension`.
"""
unload_extension(name)
return load_extension(name)

15
docs/extension_loader.md Normal file
View File

@ -0,0 +1,15 @@
# Extension Loader
The `disagreement.ext.loader` module provides simple helpers to manage optional
extensions. Extensions are regular Python modules that expose a `setup` function
called when the extension is loaded.
```python
from disagreement.ext import loader
```
- `loader.load_extension(name)` Import and initialize an extension.
- `loader.unload_extension(name)` Tear down and remove a previously loaded
extension.
- `loader.reload_extension(name)` Convenience wrapper that unloads then loads
the extension again.

View File

@ -42,3 +42,36 @@ def test_load_extension_twice_raises():
loader.load_extension("repeat_ext")
loader.unload_extension("repeat_ext")
assert called["teardown"] is True
def test_reload_extension(monkeypatch):
called_first = create_dummy_module("reload_ext")
loader.load_extension("reload_ext")
called_second = {"setup": False, "teardown": False}
module_second = types.ModuleType("reload_ext")
def setup_second():
called_second["setup"] = True
def teardown_second():
called_second["teardown"] = True
module_second.setup = setup_second
module_second.teardown = teardown_second
def import_stub(name):
assert name == "reload_ext"
sys.modules[name] = module_second
return module_second
monkeypatch.setattr(loader, "import_module", import_stub)
loader.reload_extension("reload_ext")
assert called_first["teardown"] is True
assert called_second["setup"] is True
assert loader._loaded_extensions["reload_ext"] is module_second
loader.unload_extension("reload_ext")
assert called_second["teardown"] is True