Skip to content

Commit 1075f39

Browse files
Address linting and formatting issues from Ruff (#1)
1 parent 69bb9f9 commit 1075f39

38 files changed

+930
-754
lines changed

.vscode/settings.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
2-
"python.analysis.typeCheckingMode": "basic",
2+
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
33
"python.terminal.activateEnvironment": true,
4-
54
"[python]": {
65
"editor.insertSpaces": true,
76
"editor.tabSize": 4,
@@ -10,5 +9,5 @@
109
"editor.codeActionsOnSave": {
1110
"source.organizeImports": true
1211
},
13-
}
12+
},
1413
}

Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ COPY domovoy ./domovoy
1010

1111
ENV PYTHONPATH /usr/src/app:${PYTHONPATH}
1212

13+
WORKDIR /config
14+
1315
CMD [ "python", "/usr/src/app/domovoy/cli.py", "--config", "/config/config.yml" ]

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Domovoy
22

3+
## How to run
4+
5+
```bash
6+
poetry install
7+
python domovoy/cli.py --config config.yml
8+
```
9+
310
Missing Docs
411

512
## Basic App Template with Config
@@ -31,6 +38,7 @@ class AppName(AppBase[EmptyAppConfig]):
3138
```
3239

3340
## How to register an app
41+
3442
```python
3543
from domovoy.applications.registration import register_app
3644

@@ -45,6 +53,7 @@ register_app(
4553
```
4654

4755
## Register multiple instances of the same AppType
56+
4857
```python
4958
from domovoy.applications.registration import register_app_multiple
5059

@@ -68,4 +77,3 @@ register_app_multiple(
6877
configs=apps_to_register,
6978
)
7079
```
71-

domovoy/applications/registration.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def register_app(
5252
logging_config_name: str | None = None,
5353
ignore: bool = False,
5454
) -> None:
55-
"""Registers an app in the current running instance of the Domovy App Engine.
55+
"""Register an app in the current running instance of the Domovy App Engine.
5656
5757
Args:
5858
----
@@ -72,7 +72,7 @@ def register_app(
7272
if mod is None:
7373
_logcore.error(
7474
"Error when trying to retrieve the module which called register_app for app {app_name}."
75-
+ " App wasn't registered",
75+
" App wasn't registered",
7676
app_name=app_name,
7777
)
7878
return
@@ -82,7 +82,7 @@ def register_app(
8282
if not app_file_path.endswith(f"{get_main_config().app_suffix}.py"):
8383
get_logger(mod.__name__).error(
8484
"Attempting to register an app from a regular python file {path}."
85-
+ " Apps should be registered only in files that end in {suffix}.py",
85+
" Apps should be registered only in files that end in {suffix}.py",
8686
path=app_file_path,
8787
suffix=get_main_config().app_suffix,
8888
)
@@ -106,8 +106,7 @@ def register_app_multiple(
106106
logging_config_name: str | None = None,
107107
ignore: Literal[True, False] = False,
108108
) -> None:
109-
"""Registers a group of apps in the current running instance of the Domovy App Engine, with each one using its own
110-
config.
109+
"""Register a group of apps in the current running instance of the Domovy App Engine, each one using its own config.
111110
112111
Args:
113112
----
@@ -126,7 +125,7 @@ def register_app_multiple(
126125
if mod is None:
127126
_logcore.error(
128127
"Error when trying to retrieve the module which called register_app for apps {app_names}."
129-
+ " Apps weren't registered",
128+
" Apps weren't registered",
130129
app_name=configs.keys(),
131130
)
132131
return
@@ -136,7 +135,7 @@ def register_app_multiple(
136135
if not app_file_path.endswith(f"{get_main_config().app_suffix}.py"):
137136
get_logger(mod.__name__).error(
138137
"Attempting to register apps from a regular python file {path}."
139-
+ " Apps should be registered only in files that end in {suffix}.py",
138+
" Apps should be registered only in files that end in {suffix}.py",
140139
path=app_file_path,
141140
suffix=get_main_config().app_suffix,
142141
)

domovoy/applications/types.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@ class Interval:
1111
milliseconds: float = 0
1212

1313
def is_valid(self) -> bool:
14-
"""Checks if an interval has a non-zero duration.
14+
"""Check if an interval has a non-zero duration.
1515
1616
Returns
1717
-------
1818
bool: True if at least one of the fields of the interval is non-zero.
1919
"""
20-
return (
21-
self.days != 0
22-
or self.hours != 0
23-
or self.minutes != 0
24-
or self.seconds != 0
25-
or self.milliseconds != 0
26-
)
20+
return self.days != 0 or self.hours != 0 or self.minutes != 0 or self.seconds != 0 or self.milliseconds != 0
2721

2822
def to_timedelta(self) -> datetime.timedelta:
29-
"""Converts the interval to a datetime.timedelta.
23+
"""Convert the interval to a datetime.timedelta.
3024
3125
Returns
3226
-------
@@ -41,14 +35,10 @@ def to_timedelta(self) -> datetime.timedelta:
4135
)
4236

4337
def total_seconds(self) -> float:
44-
"""The total duration of the interval in fractional seconds.
38+
"""Get the total duration of the interval in fractional seconds.
4539
4640
Returns
4741
-------
4842
float: The total duration of the interval.
4943
"""
50-
return (
51-
((self.days * 24 + self.hours) * 60 + self.minutes) * 60
52-
+ self.seconds
53-
+ (self.milliseconds / 1000)
54-
)
44+
return ((self.days * 24 + self.hours) * 60 + self.minutes) * 60 + self.seconds + (self.milliseconds / 1000)

domovoy/core/app_infra.py

+52-44
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import datetime
55
import inspect
66
import logging
7-
from collections.abc import Awaitable, Callable
7+
from collections.abc import Awaitable, Callable, Coroutine
88
from dataclasses import dataclass, field
99
from enum import StrEnum
1010
from typing import Any, Concatenate, ParamSpec, TypeVar
@@ -13,15 +13,16 @@
1313
from apscheduler.triggers.base import BaseTrigger
1414

1515
from domovoy.applications import AppBase, AppConfigBase, EmptyAppConfig
16+
from domovoy.core.configuration import get_main_config
1617
from domovoy.core.context import (
1718
context_callback_id,
1819
context_logger,
1920
inside_log_callback,
2021
)
2122
from domovoy.core.errors import (
22-
DomovoyException,
23-
DomovoyLogOnlyOnDebugWhenUncaughtException,
24-
DomovoyUnknownPluginException,
23+
DomovoyError,
24+
DomovoyLogOnlyOnDebugWhenUncaughtError,
25+
DomovoyUnknownPluginError,
2526
)
2627
from domovoy.core.logging import get_logger
2728
from domovoy.core.utils import get_callback_name, set_callback_true_information
@@ -86,7 +87,7 @@ class AppWrapper:
8687
class_name: str
8788
status: AppStatus
8889
logger: logging.LoggerAdapter[Any]
89-
app: AppBase[Any] = EmptyAppBase()
90+
app: AppBase[Any] = field(default_factory=lambda: EmptyAppBase())
9091
scheduler_callbacks: dict[str, SchedulerCallbackRegistration] = field(
9192
default_factory=dict,
9293
)
@@ -97,8 +98,9 @@ def get_pluginx(self, plugin_type: type[T], name: str | None = None) -> T:
9798
plugin = self.get_plugin(plugin_type, name)
9899

99100
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,
102104
)
103105

104106
return plugin
@@ -112,22 +114,22 @@ def get_plugin(self, plugin_type: type[T], name: str | None = None) -> T | None:
112114
if name is not None:
113115
if name not in plugins:
114116
return None
115-
else:
116-
return plugins[name] # type: ignore
117+
118+
return plugins[name] # type: ignore[generaltypeissues]
117119

118120
total_plugins = len(plugins)
119121

120122
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 = (
126127
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",
128129
)
129-
else:
130-
return next(iter(plugins.values())) # type: ignore
130+
raise DomovoyError(msg)
131+
132+
return next(iter(plugins.values())) # type: ignore[generaltypeissues]
131133

132134
def register_plugin(self, plugin: AppPlugin, name: str) -> None:
133135
plugin_type = type(plugin)
@@ -152,26 +154,26 @@ def prepare_all_plugins(self) -> None:
152154
p.post_prepare()
153155

154156
def handle_exception_and_logging(
155-
self, true_callback: Callable,
157+
self,
158+
true_callback: Callable,
156159
) -> 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]],
159162
]:
160163
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]]:
163166
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,
165170
) -> 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
170172

171173
if self.status != AppStatus.RUNNING:
172174
_logcore.warning(
173175
"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}",
175177
app_name=self.app_name,
176178
status=self.status,
177179
function_name=func.__name__,
@@ -191,9 +193,9 @@ async def wrapper(
191193
)
192194

193195
try:
194-
self.scheduler_callbacks
195196
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),
197199
)
198200

199201
except asyncio.exceptions.CancelledError as e:
@@ -203,7 +205,7 @@ async def wrapper(
203205
app_name=self.app_name,
204206
)
205207

206-
except DomovoyLogOnlyOnDebugWhenUncaughtException as e:
208+
except DomovoyLogOnlyOnDebugWhenUncaughtError as e:
207209
logger.debug(
208210
"Debug Log only Uncaught Exception",
209211
e,
@@ -225,31 +227,37 @@ async def wrapper(
225227
return inner_handle_exception_and_logging
226228

227229
def __get_callback_registration(
228-
self, callback_id: str | int,
230+
self,
231+
callback_id: str | int,
229232
) -> CallbackRegistration | None:
230233
if isinstance(callback_id, int):
231234
return None
232235

233236
if callback_id.startswith("event-"):
234237
return self.event_callbacks.get(callback_id, None)
235-
elif callback_id.startswith("scheduler-"):
238+
239+
if callback_id.startswith("scheduler-"):
236240
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"):
244243
return None
245244

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+
246251
TReturn = TypeVar("TReturn")
247252

248253
def instrument_app_callback(
249-
self, callback: Callable[P, None | Awaitable[None]],
254+
self,
255+
callback: Callable[P, None | Awaitable[None]],
250256
) -> Callable[Concatenate[str | int, P], Awaitable[None]]:
251257
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,
253261
) -> None:
254262
try:
255263
self.__callback_called(callback_id)
@@ -268,10 +276,10 @@ def __callback_called(self, callback_id: str | int) -> None:
268276

269277
if registration:
270278
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())
272280

273281
def __callback_failed(self, callback_id: str | int) -> None:
274282
registration = self.__get_callback_registration(callback_id)
275283

276284
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

Comments
 (0)