Passed
Push — main ( 8be509...207edf )
by
unknown
01:42
created

pincer.middleware.get_middleware()   A

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 9.65
c 0
b 0
f 0
cc 4
nop 0
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# MIT License
3
#
4
# Copyright (c) 2021 Pincer
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining
7
# a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
from glob import glob
26
from importlib import import_module
27
from os import chdir
28
from pathlib import Path
29
from typing import Dict
30
31
from pincer.exceptions import NoExportMethod
32
from pincer.utils import Coro
33
34
35
def get_middleware() -> Dict[str, Coro]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
36
    middleware_list: Dict[str, Coro] = {}
37
    chdir(Path(__file__).parent.resolve())
38
39
    for middleware_path in glob("*.py"):
40
        if middleware_path.startswith("__"):
41
            continue
42
43
        event = middleware_path[:-3]
44
45
        try:
46
            middleware_list[event] = getattr(
47
                import_module(f".{event}", package=__name__),
48
                "export"
49
            )()
50
        except AttributeError:
51
            raise NoExportMethod(
52
                f"Middleware module `{middleware_path}` expected an "
53
                "`export` method but none was found!"
54
            )
55
56
    return middleware_list
57
58
59
middleware: Dict[str, Coro] = get_middleware()
60