| Total Complexity | 5 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # Copyright Pincer 2021-Present |
||
|
|
|||
| 2 | # Full MIT License can be found in `LICENSE` at the project root. |
||
| 3 | from __future__ import annotations |
||
| 4 | |||
| 5 | import logging |
||
| 6 | from glob import glob |
||
| 7 | from importlib import import_module |
||
| 8 | from pathlib import Path |
||
| 9 | from typing import TYPE_CHECKING |
||
| 10 | |||
| 11 | from ..utils.directory import chdir |
||
| 12 | |||
| 13 | if TYPE_CHECKING: |
||
| 14 | from typing import Dict |
||
| 15 | from ..utils.types import Coro |
||
| 16 | |||
| 17 | |||
| 18 | _log = logging.getLogger(__package__) |
||
| 19 | |||
| 20 | |||
| 21 | def get_middleware() -> Dict[str, Coro]: |
||
| 22 | middleware_list: Dict[str, Coro] = {} |
||
| 23 | |||
| 24 | with chdir(Path(__file__).parent.resolve()): |
||
| 25 | |||
| 26 | for middleware_path in glob("*.py"): |
||
| 27 | if middleware_path.startswith("__"): |
||
| 28 | continue |
||
| 29 | |||
| 30 | event = middleware_path[:-3] |
||
| 31 | |||
| 32 | try: |
||
| 33 | middleware_list[event] = getattr( |
||
| 34 | import_module(f".{event}", package=__name__), "export" |
||
| 35 | )() |
||
| 36 | except AttributeError: |
||
| 37 | _log.warning( |
||
| 38 | f"Middleware {middleware_path} expected an `export` method." |
||
| 39 | ) |
||
| 40 | |||
| 41 | continue |
||
| 42 | |||
| 43 | # raise NoExportMethod( |
||
| 44 | # f"Middleware module `{middleware_path}` expected an " |
||
| 45 | # "`export` method but none was found!" |
||
| 46 | # ) |
||
| 47 | |||
| 48 | return middleware_list |
||
| 49 | |||
| 50 | |||
| 51 | middleware: Dict[str, Coro] = get_middleware() |
||
| 52 |