|
| 1 | +import inspect |
1 | 2 | import types |
2 | | -from inspect import Parameter |
3 | | -from typing import Callable, Union |
| 3 | +from dataclasses import dataclass |
| 4 | +from types import MappingProxyType |
| 5 | +from typing import TYPE_CHECKING, Any, Callable, Optional, Type, Union |
4 | 6 |
|
5 | | -from .. import data |
| 7 | +import pydantic |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +from .. import data, errors |
6 | 11 | from ..http.endpoints import Endpoint |
7 | 12 |
|
| 13 | +if TYPE_CHECKING: |
| 14 | + from ..app import Application |
| 15 | + from ..client import Client |
| 16 | + |
| 17 | + |
| 18 | +@dataclass(frozen=False) |
| 19 | +class ListenerConfig: |
| 20 | + force_raise: bool = False |
| 21 | + |
8 | 22 |
|
9 | 23 | class Listener: |
10 | | - __slots__ = ('endpoint', 'callback', 'callback_args') |
| 24 | + __slots__ = ( |
| 25 | + '_app', |
| 26 | + '_client', |
| 27 | + '_endpoint', |
| 28 | + '_callback', |
| 29 | + '_callback_params', |
| 30 | + 'config', |
| 31 | + ) |
11 | 32 |
|
12 | 33 | def __init__( |
13 | 34 | self, |
14 | 35 | endpoint: Endpoint, |
15 | 36 | callback: Callable, |
16 | | - callback_args: types.MappingProxyType[str, Parameter], |
| 37 | + config: ListenerConfig = ListenerConfig(), |
| 38 | + app: Optional['Application'] = None, |
| 39 | + client: Optional['Client'] = None, |
17 | 40 | ): |
18 | | - self.endpoint = endpoint |
19 | | - self.callback = callback |
20 | | - self.callback_args = callback_args |
| 41 | + self._app = app |
| 42 | + self._client = client |
| 43 | + self._endpoint = endpoint |
| 44 | + self._callback = callback |
| 45 | + self._callback_params = inspect.signature(callback).parameters |
| 46 | + self.config = config |
| 47 | + |
| 48 | + @property |
| 49 | + def app(self) -> 'Application': |
| 50 | + return self._app |
| 51 | + |
| 52 | + @property |
| 53 | + def endpoint(self) -> Endpoint: |
| 54 | + return self._endpoint |
| 55 | + |
| 56 | + @property |
| 57 | + def callback(self) -> Callable: |
| 58 | + return self._callback |
| 59 | + |
| 60 | + @property |
| 61 | + def callback_params(self) -> MappingProxyType[str, inspect.Parameter]: |
| 62 | + return self._callback_params |
21 | 63 |
|
22 | 64 | def __repr__(self): |
23 | 65 | return f'{self.__class__.__name__}(endpoint={self.endpoint})' |
24 | 66 |
|
25 | 67 |
|
| 68 | +class ListenerManager: |
| 69 | + def __init__(self): |
| 70 | + """ |
| 71 | + The __init__ method is called when the class is instantiated. |
| 72 | + It sets up the instance variables that will be used by other methods |
| 73 | + in the class. |
| 74 | +
|
| 75 | +
|
| 76 | + :param self: Refer to the class instance |
| 77 | + :return: A dictionary of the capture listeners and request listeners |
| 78 | + """ |
| 79 | + self.listeners: dict[str, Callable] = {} |
| 80 | + |
| 81 | + def get_listener(self, endpoint: Endpoint) -> Listener | None: |
| 82 | + """ |
| 83 | + The get_listener method is used to get the capture listener |
| 84 | + for a given endpoint. |
| 85 | +
|
| 86 | + :param self: Refer to the class instance |
| 87 | + :param endpoint: Endpoint: Get the capture listener from the endpoint |
| 88 | + name |
| 89 | + :return: The capture listener for the given endpoint |
| 90 | + """ |
| 91 | + return self.listeners.get(endpoint.name) |
| 92 | + |
| 93 | + def include_listener(self, listener: Listener) -> Listener: |
| 94 | + """ |
| 95 | + The include_listener method adds a listener to the |
| 96 | + capture_listeners dictionary. |
| 97 | + The key is the name of an endpoint, and the value is a callable |
| 98 | + function that will be called when |
| 99 | + the endpoint's data has been captured. |
| 100 | +
|
| 101 | + :param listener: the listener that will be included |
| 102 | + :param self: Refer to the class instance |
| 103 | + listen to |
| 104 | + request is made to the endpoint |
| 105 | + :return: None |
| 106 | + :raises InvalidListener: Raised if the endpoint is already registered |
| 107 | + """ |
| 108 | + if self.get_listener(listener.endpoint): |
| 109 | + raise errors.InvalidListener( |
| 110 | + message='Already exists an capture_listener for ' |
| 111 | + f'{listener.endpoint}', |
| 112 | + listener=listener.callback, |
| 113 | + ) |
| 114 | + self.listeners.update({listener.endpoint.name: listener}) |
| 115 | + return listener |
| 116 | + |
| 117 | + def remove_listener(self, endpoint: Endpoint) -> Callable: |
| 118 | + """ |
| 119 | + The remove_listener method removes a capture listener from |
| 120 | + the list of listeners. |
| 121 | +
|
| 122 | + :param self: Refer to the class instance |
| 123 | + :param endpoint: Endpoint: Identify the endpoint to remove |
| 124 | + :return: The capture_listener that was removed from the dictionary |
| 125 | + """ |
| 126 | + if self.get_listener(endpoint): |
| 127 | + return self.listeners.pop(endpoint.name) |
| 128 | + |
| 129 | + def clear_listeners(self) -> None: |
| 130 | + """ |
| 131 | + The clear_listeners function clears the capture_listeners list. |
| 132 | +
|
| 133 | + :param self: Refer to the class instance |
| 134 | + :return: None |
| 135 | + """ |
| 136 | + self.listeners = None |
| 137 | + |
| 138 | + @classmethod |
| 139 | + def cast_to_pydantic_model( |
| 140 | + cls, model: Type[BaseModel], values: dict[Any, Any] |
| 141 | + ): |
| 142 | + result: BaseModel | None | dict = values |
| 143 | + if isinstance(model, types.UnionType): |
| 144 | + for ty in model.__args__: |
| 145 | + if ty is None: |
| 146 | + continue |
| 147 | + elif not issubclass(ty, BaseModel): |
| 148 | + continue |
| 149 | + try: |
| 150 | + a = ty(**values) |
| 151 | + return a |
| 152 | + except pydantic.ValidationError: |
| 153 | + continue |
| 154 | + return None |
| 155 | + if issubclass(model, BaseModel): |
| 156 | + try: |
| 157 | + result = model(**values) |
| 158 | + except pydantic.ValidationError as e: |
| 159 | + print(e) |
| 160 | + result = None |
| 161 | + return result |
| 162 | + |
| 163 | + # async def notify( |
| 164 | + # self, |
| 165 | + # endpoint: Endpoint, |
| 166 | + # extra: Any = None, |
| 167 | + # **kwargs, |
| 168 | + # ) -> Any | Exception: |
| 169 | + # """ |
| 170 | + # The notify method is called when a capture event occurs. |
| 171 | + # |
| 172 | + # :param self: Refer to the class instance |
| 173 | + # :param endpoint: Endpoint: Get the endpoint that is being called |
| 174 | + # :param extra: |
| 175 | + # :return: The result of the call function |
| 176 | + # """ |
| 177 | + # pass |
| 178 | + |
| 179 | + |
26 | 180 | ListenerDataTypes = Union[ |
27 | 181 | data.AppData, |
28 | 182 | data.StatusData, |
|
0 commit comments