4
4
import datetime
5
5
import inspect
6
6
import logging
7
- from collections .abc import Awaitable , Callable
7
+ from collections .abc import Awaitable , Callable , Coroutine
8
8
from dataclasses import dataclass , field
9
9
from enum import StrEnum
10
10
from typing import Any , Concatenate , ParamSpec , TypeVar
13
13
from apscheduler .triggers .base import BaseTrigger
14
14
15
15
from domovoy .applications import AppBase , AppConfigBase , EmptyAppConfig
16
+ from domovoy .core .configuration import get_main_config
16
17
from domovoy .core .context import (
17
18
context_callback_id ,
18
19
context_logger ,
19
20
inside_log_callback ,
20
21
)
21
22
from domovoy .core .errors import (
22
- DomovoyException ,
23
- DomovoyLogOnlyOnDebugWhenUncaughtException ,
24
- DomovoyUnknownPluginException ,
23
+ DomovoyError ,
24
+ DomovoyLogOnlyOnDebugWhenUncaughtError ,
25
+ DomovoyUnknownPluginError ,
25
26
)
26
27
from domovoy .core .logging import get_logger
27
28
from domovoy .core .utils import get_callback_name , set_callback_true_information
@@ -86,7 +87,7 @@ class AppWrapper:
86
87
class_name : str
87
88
status : AppStatus
88
89
logger : logging .LoggerAdapter [Any ]
89
- app : AppBase [Any ] = EmptyAppBase ()
90
+ app : AppBase [Any ] = field ( default_factory = lambda : EmptyAppBase () )
90
91
scheduler_callbacks : dict [str , SchedulerCallbackRegistration ] = field (
91
92
default_factory = dict ,
92
93
)
@@ -97,8 +98,9 @@ def get_pluginx(self, plugin_type: type[T], name: str | None = None) -> T:
97
98
plugin = self .get_plugin (plugin_type , name )
98
99
99
100
if plugin is None :
100
- raise DomovoyUnknownPluginException (
101
- f"Unknown plugin of type: { plugin_type } " ,
101
+ msg = f"Unknown plugin of type: { plugin_type } "
102
+ raise DomovoyUnknownPluginError (
103
+ msg ,
102
104
)
103
105
104
106
return plugin
@@ -112,22 +114,22 @@ def get_plugin(self, plugin_type: type[T], name: str | None = None) -> T | None:
112
114
if name is not None :
113
115
if name not in plugins :
114
116
return None
115
- else :
116
- return plugins [name ] # type: ignore
117
+
118
+ return plugins [name ] # type: ignore[generaltypeissues]
117
119
118
120
total_plugins = len (plugins )
119
121
120
122
if total_plugins == 0 :
121
- raise DomovoyException (
122
- f"There are no plugins registered for type { plugin_type .__name__ } " ,
123
- )
124
- elif total_plugins >= 2 :
125
- raise DomovoyException (
123
+ msg = f"There are no plugins registered for type { plugin_type .__name__ } "
124
+ raise DomovoyError (msg )
125
+ if total_plugins >= 2 :
126
+ msg = (
126
127
f"There are multiple plugins registered for type { plugin_type .__name__ } ."
127
- + " You need to include the name of the plugin instance you require" ,
128
+ " You need to include the name of the plugin instance you require" ,
128
129
)
129
- else :
130
- return next (iter (plugins .values ())) # type: ignore
130
+ raise DomovoyError (msg )
131
+
132
+ return next (iter (plugins .values ())) # type: ignore[generaltypeissues]
131
133
132
134
def register_plugin (self , plugin : AppPlugin , name : str ) -> None :
133
135
plugin_type = type (plugin )
@@ -152,26 +154,26 @@ def prepare_all_plugins(self) -> None:
152
154
p .post_prepare ()
153
155
154
156
def handle_exception_and_logging (
155
- self , true_callback : Callable ,
157
+ self ,
158
+ true_callback : Callable ,
156
159
) -> Callable [
157
- [Callable [Concatenate [TStrOrInt , P ], Awaitable [ None ]]],
158
- Callable [Concatenate [TStrOrInt , P ], Awaitable [ None ]],
160
+ [Callable [Concatenate [TStrOrInt , P ], Coroutine [ Any , Any , None ]]],
161
+ Callable [Concatenate [TStrOrInt , P ], Coroutine [ Any , Any , None ]],
159
162
]:
160
163
def inner_handle_exception_and_logging (
161
- func : Callable [Concatenate [TStrOrInt , P ], Awaitable [ None ]],
162
- ) -> Callable [Concatenate [TStrOrInt , P ], Awaitable [ None ]]:
164
+ func : Callable [Concatenate [TStrOrInt , P ], Coroutine [ Any , Any , None ]],
165
+ ) -> Callable [Concatenate [TStrOrInt , P ], Coroutine [ Any , Any , None ]]:
163
166
async def wrapper (
164
- callback_id : TStrOrInt , * args : P .args , ** kwargs : P .kwargs ,
167
+ callback_id : TStrOrInt ,
168
+ * args : P .args ,
169
+ ** kwargs : P .kwargs ,
165
170
) -> None :
166
- if inside_log_callback .get ():
167
- logger = _logcore
168
- else :
169
- logger = self .logger
171
+ logger = _logcore if inside_log_callback .get () else self .logger
170
172
171
173
if self .status != AppStatus .RUNNING :
172
174
_logcore .warning (
173
175
"Tried to call {function_name} on app `{app_name}` when app's status was `{status}`."
174
- + " -- args: {pargs} -- kwargs: {pkwargs}" ,
176
+ " -- args: {pargs} -- kwargs: {pkwargs}" ,
175
177
app_name = self .app_name ,
176
178
status = self .status ,
177
179
function_name = func .__name__ ,
@@ -191,9 +193,9 @@ async def wrapper(
191
193
)
192
194
193
195
try :
194
- self .scheduler_callbacks
195
196
await asyncio .create_task (
196
- func (callback_id , * args , ** kwargs ), name = get_callback_name (true_callback ), # type: ignore
197
+ func (callback_id , * args , ** kwargs ),
198
+ name = get_callback_name (true_callback ),
197
199
)
198
200
199
201
except asyncio .exceptions .CancelledError as e :
@@ -203,7 +205,7 @@ async def wrapper(
203
205
app_name = self .app_name ,
204
206
)
205
207
206
- except DomovoyLogOnlyOnDebugWhenUncaughtException as e :
208
+ except DomovoyLogOnlyOnDebugWhenUncaughtError as e :
207
209
logger .debug (
208
210
"Debug Log only Uncaught Exception" ,
209
211
e ,
@@ -225,31 +227,37 @@ async def wrapper(
225
227
return inner_handle_exception_and_logging
226
228
227
229
def __get_callback_registration (
228
- self , callback_id : str | int ,
230
+ self ,
231
+ callback_id : str | int ,
229
232
) -> CallbackRegistration | None :
230
233
if isinstance (callback_id , int ):
231
234
return None
232
235
233
236
if callback_id .startswith ("event-" ):
234
237
return self .event_callbacks .get (callback_id , None )
235
- elif callback_id .startswith ("scheduler-" ):
238
+
239
+ if callback_id .startswith ("scheduler-" ):
236
240
return self .scheduler_callbacks .get (callback_id , None )
237
- elif callback_id .startswith ("ephemeral_callback" ):
238
- return None
239
- else :
240
- self .logger .error (
241
- "Tried to load invalid callback_id `{callback_id}` from callback registrations" ,
242
- callback_id = callback_id ,
243
- )
241
+
242
+ if callback_id .startswith ("ephemeral_callback" ):
244
243
return None
245
244
245
+ self .logger .error (
246
+ "Tried to load invalid callback_id `{callback_id}` from callback registrations" ,
247
+ callback_id = callback_id ,
248
+ )
249
+ return None
250
+
246
251
TReturn = TypeVar ("TReturn" )
247
252
248
253
def instrument_app_callback (
249
- self , callback : Callable [P , None | Awaitable [None ]],
254
+ self ,
255
+ callback : Callable [P , None | Awaitable [None ]],
250
256
) -> Callable [Concatenate [str | int , P ], Awaitable [None ]]:
251
257
async def instrumented_call (
252
- callback_id : str | int , * args : P .args , ** kwargs : P .kwargs ,
258
+ callback_id : str | int ,
259
+ * args : P .args ,
260
+ ** kwargs : P .kwargs ,
253
261
) -> None :
254
262
try :
255
263
self .__callback_called (callback_id )
@@ -268,10 +276,10 @@ def __callback_called(self, callback_id: str | int) -> None:
268
276
269
277
if registration :
270
278
registration .times_called += 1
271
- registration .last_call_datetime = datetime .datetime .now ()
279
+ registration .last_call_datetime = datetime .datetime .now (tz = get_main_config (). get_timezone () )
272
280
273
281
def __callback_failed (self , callback_id : str | int ) -> None :
274
282
registration = self .__get_callback_registration (callback_id )
275
283
276
284
if registration :
277
- registration .last_error_datetime = datetime .datetime .now ()
285
+ registration .last_error_datetime = datetime .datetime .now (tz = get_main_config (). get_timezone () )
0 commit comments