Skip to content

Commit 63cadc0

Browse files
authored
Merge pull request #167 from python-ellar/async_application_context
Async application context
2 parents ad56a28 + c2284f3 commit 63cadc0

File tree

20 files changed

+287
-184
lines changed

20 files changed

+287
-184
lines changed

ellar/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
""" Ellar - Python ASGI web framework for building fast, efficient, and scalable RESTful APIs and server-side applications. """
22

3-
__version__ = "0.6.2"
3+
__version__ = "0.6.4"

ellar/app/context.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def injector(self) -> EllarInjector:
4848
def config(self) -> Config:
4949
return self._config
5050

51-
def __enter__(self) -> "ApplicationContext":
51+
async def __aenter__(self) -> "ApplicationContext":
5252
app_context = app_context_var.get(empty)
5353
if app_context is empty:
5454
# If app_context exist
@@ -57,16 +57,16 @@ def __enter__(self) -> "ApplicationContext":
5757
# ensure current_config is in sync with running application context.
5858
current_config._wrapped = self.config
5959
app_context = self
60-
app_context_started_events.run()
60+
await app_context_started_events.run()
6161
return app_context # type:ignore[return-value]
6262

63-
def __exit__(
63+
async def __aexit__(
6464
self,
6565
exc_type: t.Optional[t.Any],
6666
exc_value: t.Optional[BaseException],
6767
tb: t.Optional[TracebackType],
6868
) -> None:
69-
app_context_teardown_events.run()
69+
await app_context_teardown_events.run()
7070
app_context_var.set(empty)
7171

7272
current_app._wrapped = empty # type:ignore[attr-defined]

ellar/app/factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from uuid import uuid4
55

6-
from ellar.common import EllarTyper
6+
import click
77
from ellar.common.constants import MODULE_METADATA, MODULE_WATERMARK
88
from ellar.common.models import GuardCanActivate
99
from ellar.core import Config, DynamicModule, LazyModuleImport, ModuleBase, ModuleSetup
@@ -185,7 +185,7 @@ def create_app(
185185
global_guards: t.Optional[
186186
t.List[t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]]
187187
] = None,
188-
commands: t.Sequence[t.Union[t.Callable, "EllarTyper"]] = (),
188+
commands: t.Sequence[t.Union[click.Command, click.Group, t.Any]] = (),
189189
config_module: t.Union[str, t.Dict, None] = None,
190190
) -> App:
191191
from ellar.common import Module

ellar/app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def application_context(self) -> ApplicationContext:
246246
return ApplicationContext.create(app=self)
247247

248248
async def __call__(self, scope: TScope, receive: TReceive, send: TSend) -> None:
249-
with self.application_context() as ctx:
249+
async with self.application_context() as ctx:
250250
scope["app"] = ctx.app
251251
if self.middleware_stack is None:
252252
self.middleware_stack = self.build_middleware_stack()

ellar/common/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import typing as t
22

3-
from .commands import EllarTyper, command
43
from .datastructures import UploadFile
54
from .decorators import (
65
Controller,
@@ -105,7 +104,6 @@
105104
"serialize_object",
106105
"ControllerType",
107106
"GuardCanActivate",
108-
"EllarTyper",
109107
"GlobalGuard",
110108
"Serializer",
111109
"WebSocketException",
@@ -120,7 +118,6 @@
120118
"MethodNotAllowed",
121119
"render_template",
122120
"render_template_string",
123-
"command",
124121
"ModuleRouter",
125122
"render",
126123
"Module",

ellar/common/commands/__init__.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

ellar/common/commands/base.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

ellar/common/commands/decorator.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

ellar/common/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
CONTROLLER_OPERATION_HANDLER_KEY = "CONTROLLER_OPERATION_HANDLER"
5252
CONTROLLER_CLASS_KEY = "CONTROLLER_CLASS_KEY"
5353

54-
CALLABLE_COMMAND_INFO = "__CALLABLE_COMMAND_INFO__"
5554
GROUP_METADATA = "GROUP_METADATA"
5655
SKIP_AUTH = "SKIP_AUTH"
5756

ellar/common/decorators/modules.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from functools import partial
44
from pathlib import Path
55

6+
import click
67
from ellar.common.compatible import AttributeDict
78
from ellar.common.constants import MODULE_METADATA, MODULE_WATERMARK
89
from ellar.common.exceptions import ImproperConfiguration
@@ -13,9 +14,6 @@
1314
from ellar.reflect import reflect
1415
from starlette.routing import Host, Mount
1516

16-
if t.TYPE_CHECKING: # pragma: no cover
17-
from ellar.common.commands import EllarTyper
18-
1917

2018
def _wrapper(
2119
target: t.Type,
@@ -50,7 +48,7 @@ def Module(
5048
base_directory: t.Optional[t.Union[Path, str]] = None,
5149
static_folder: str = "static",
5250
modules: t.Sequence[t.Union[t.Type, t.Any]] = (),
53-
commands: t.Sequence[t.Union[t.Callable, "EllarTyper"]] = (),
51+
commands: t.Sequence[t.Union[click.Command, click.Group, t.Any]] = (),
5452
) -> t.Callable:
5553
"""
5654
========= MODULE DECORATOR ==============

0 commit comments

Comments
 (0)