Skip to content

Commit

Permalink
Merge pull request #674 from TotallyNotRobots/hook-dict
Browse files Browse the repository at this point in the history
Move plugin hooks to typed dict
  • Loading branch information
linuxdaemon authored Mar 15, 2024
2 parents 134a2fe + bd42e49 commit d70f1b9
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 41 deletions.
12 changes: 12 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM mcr.microsoft.com/devcontainers/python:3.11

RUN \
pipx uninstall pydocstyle \
&& pipx uninstall pycodestyle \
&& pipx uninstall mypy \
&& pipx uninstall pylint \
&& pipx uninstall pytest \
&& pipx uninstall flake8 \
&& pipx uninstall black

ENV SHELL /bin/bash
72 changes: 42 additions & 30 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:0-3.9",
"features": {
"ghcr.io/devcontainers-contrib/features/zsh-plugins:0": {
"plugins": "ssh-agent",
"omzPlugins": "https://github.com/zsh-users/zsh-autosuggestions"
}
},
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"dockerFile": "./Dockerfile",
"context": "..",
"features": {
"ghcr.io/devcontainers-contrib/features/zsh-plugins:0": {
"plugins": "ssh-agent",
"omzPlugins": "https://github.com/zsh-users/zsh-autosuggestions"
}
},

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install -Ur requirements-dev.txt",
"postStartCommand": "pip3 install -Ur requirements-dev.txt",
// Configure tool-specific properties.
// "customizations": {},
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install -Ur requirements-dev.txt",
"postCreateCommand": "pip3 install -Ur requirements-dev.txt",
// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "root",
"customizations": {
"vscode": {
"extensions": [
"tamasfe.even-better-toml",
"ms-python.python",
"ms-python.vscode-pylance",
"EditorConfig.EditorConfig"
]
}
}
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root",
"customizations": {
"vscode": {
"extensions": [
"tamasfe.even-better-toml",
"ms-python.python",
"ms-python.vscode-pylance",
"EditorConfig.EditorConfig",
"GitHub.vscode-pull-request-github"
],
"settings": {
"python.pythonPath": "/usr/local/bin/python",
"python.testing.pytestArgs": ["--no-cov"],
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh"
}
}
}
}
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"recommendations": [
"github.vscode-github-actions",
"github.vscode-pull-request-github"
]
}
45 changes: 41 additions & 4 deletions cloudbot/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
from functools import partial
from operator import attrgetter
from pathlib import Path
from typing import Dict, List, MutableMapping, Optional, Tuple, Type, cast
from typing import (
Dict,
List,
MutableMapping,
Optional,
Tuple,
Type,
TypedDict,
cast,
)
from weakref import WeakValueDictionary

import sqlalchemy
Expand All @@ -19,8 +28,18 @@
CommandHook,
ConfigHook,
EventHook,
IrcOutHook,
OnCapAckHook,
OnCapAvaliableHook,
OnConnectHook,
OnStartHook,
OnStopHook,
PeriodicHook,
PermHook,
PostHookHook,
RawHook,
RegexHook,
SieveHook,
hook_name_to_plugin,
)
from cloudbot.util import HOOK_ATTR, LOADED_ATTR, async_util, database
Expand All @@ -29,7 +48,25 @@
logger = logging.getLogger("cloudbot")


def find_hooks(parent, module):
class HookDict(TypedDict):
command: List[CommandHook]
on_connect: List[OnConnectHook]
on_start: List[OnStartHook]
on_stop: List[OnStopHook]
on_cap_available: List[OnCapAvaliableHook]
on_cap_ack: List[OnCapAckHook]
sieve: List[SieveHook]
event: List[EventHook]
regex: List[RegexHook]
periodic: List[PeriodicHook]
irc_raw: List[RawHook]
irc_out: List[IrcOutHook]
post_hook: List[PostHookHook]
config: List[ConfigHook]
perm_check: List[PermHook]


def find_hooks(parent, module) -> HookDict:
hooks = defaultdict(list)
for func in module.__dict__.values():
if hasattr(func, HOOK_ATTR) and not hasattr(func, "_not_" + HOOK_ATTR):
Expand All @@ -44,7 +81,7 @@ def find_hooks(parent, module):
# delete the hook to free memory
delattr(func, HOOK_ATTR)

return hooks
return cast(HookDict, hooks)


def find_tables(code):
Expand Down Expand Up @@ -351,7 +388,7 @@ async def load_plugin(self, path):
self._sort_hooks()

# we don't need this anymore
del plugin.hooks["on_start"]
plugin.hooks["on_start"].clear()

def _sort_hooks(self) -> None:
def _sort_list(hooks):
Expand Down
13 changes: 10 additions & 3 deletions cloudbot/util/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
from typing import Dict, Optional, Union

import requests
from requests import HTTPError, PreparedRequest, RequestException, Response
from requests import Request
from requests import (
HTTPError,
PreparedRequest,
Request,
RequestException,
Response,
)

# Constants
DEFAULT_SHORTENER = "is.gd"
Expand Down Expand Up @@ -163,7 +168,9 @@ def paste(data, ext="txt", service=DEFAULT_PASTEBIN, raise_on_no_paste=False):


class ServiceError(Exception):
def __init__(self, request: Union[Request, PreparedRequest], message: str) -> None:
def __init__(
self, request: Union[Request, PreparedRequest], message: str
) -> None:
super().__init__(message)
self.request = request

Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import importlib
import logging
from typing import List
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -30,13 +31,13 @@ def caplog_bot(caplog):

@pytest.fixture()
def patch_import_module():
with patch("importlib.import_module") as mocked:
with patch.object(importlib, "import_module") as mocked:
yield mocked


@pytest.fixture()
def patch_import_reload():
with patch("importlib.reload") as mocked:
with patch.object(importlib, "reload") as mocked:
yield mocked


Expand Down
3 changes: 2 additions & 1 deletion tests/core_tests/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from cloudbot.util import database
from tests.util.mock_module import MockModule


@pytest.fixture()
def mock_bot(mock_bot_factory, event_loop, tmp_path):
tmp_base = tmp_path / "tmp"
Expand Down Expand Up @@ -221,6 +220,8 @@ def test_plugin_with_objs_full_dict_attr(mock_manager, patch_import_module):
def test_plugin_load_disabled(
mock_manager, patch_import_module, patch_import_reload
):
patch_import_module.reset_mock()
patch_import_reload.reset_mock()
patch_import_module.return_value = MockModule()
mock_manager.bot.config.update(
{
Expand Down
3 changes: 3 additions & 0 deletions tests/plugin_tests/test_chan_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
def test_format_exception_chain():
def _get_data(exc):
yield repr(exc)
if hasattr(exc, 'add_note'):
yield f" add_note = {exc.add_note!r}"

yield f" args = {exc.args!r}"
yield f" with_traceback = {exc.with_traceback!r}"
yield ""
Expand Down

0 comments on commit d70f1b9

Please sign in to comment.