Skip to content

Commit a1ca63d

Browse files
committed
refactored route operation decorators
1 parent f3d2f02 commit a1ca63d

File tree

8 files changed

+76
-75
lines changed

8 files changed

+76
-75
lines changed

ellar/common/__init__.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
from .decorators.controller import Controller
66
from .decorators.exception import exception_handler
77
from .decorators.guards import guards
8-
from .decorators.html import Render, template_filter, template_global
8+
from .decorators.html import render, template_filter, template_global
99
from .decorators.middleware import middleware
1010
from .decorators.modules import Module
1111
from .decorators.openapi import openapi
1212
from .decorators.request import on_shutdown, on_startup
1313
from .decorators.serializer import serializer_filter
1414
from .decorators.versioning import version
1515
from .routing import (
16-
Delete,
17-
Get,
18-
Head,
19-
HttpRoute,
20-
Options,
21-
Patch,
22-
Post,
23-
Put,
24-
Trace,
25-
WsRoute,
16+
delete,
17+
get,
18+
head,
19+
http_route,
20+
options,
21+
patch,
22+
post,
23+
put,
24+
trace,
25+
ws_route,
2626
)
2727
from .routing.params import (
2828
Body,
@@ -41,7 +41,7 @@
4141

4242
__all__ = [
4343
"ModuleRouter",
44-
"Render",
44+
"render",
4545
"Module",
4646
"guards",
4747
"Param",
@@ -50,16 +50,16 @@
5050
"Controller",
5151
"openapi",
5252
"version",
53-
"Delete",
54-
"Get",
55-
"Head",
56-
"HttpRoute",
57-
"Options",
58-
"Patch",
59-
"Post",
60-
"Put",
61-
"Trace",
62-
"WsRoute",
53+
"delete",
54+
"get",
55+
"head",
56+
"http_route",
57+
"options",
58+
"patch",
59+
"post",
60+
"put",
61+
"trace",
62+
"ws_route",
6363
"Body",
6464
"Cookie",
6565
"File",

ellar/common/decorators/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .controller import Controller # noqa
33
from .exception import exception_handler # noqa
44
from .guards import guards # noqa
5-
from .html import Render, template_filter, template_global # noqa
5+
from .html import render, template_filter, template_global # noqa
66
from .middleware import middleware # noqa
77
from .modules import Module # noqa
88
from .openapi import openapi # noqa

ellar/common/decorators/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _decorator(cls: t.Type) -> t.Type[ControllerBase]:
118118
_tag = _controller_type.controller_class_name()
119119

120120
if not kwargs.openapi.tag: # type: ignore
121-
kwargs.openapi.tag = _tag # type: ignore
121+
kwargs["openapi"]["tag"] = _tag # type: ignore
122122

123123
if kwargs["path"] is NOT_SET:
124124
kwargs["path"] = f"/{_tag}"

ellar/common/decorators/html.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@ class RenderDecoratorException(Exception):
2424
pass
2525

2626

27-
class Render:
28-
def __init__(self, template_name: t.Optional[str] = NOT_SET) -> None:
29-
if template_name is not NOT_SET:
30-
assert isinstance(
31-
template_name, str
32-
), "Render Operation must invoked eg. @Render()"
33-
self.template_name = None if template_name is NOT_SET else template_name
34-
self.class_base_function_regex = re.compile(
35-
"<\\w+ (\\w+)\\.(\\w+) at \\w+>", re.IGNORECASE
36-
)
37-
38-
def __call__(self, func: t.Union[t.Callable, t.Any]) -> t.Union[t.Callable, t.Any]:
27+
def render(template_name: t.Optional[str] = NOT_SET) -> t.Callable:
28+
if template_name is not NOT_SET:
29+
assert isinstance(
30+
template_name, str
31+
), "Render Operation must invoked eg. @Render()"
32+
template_name = None if template_name is NOT_SET else template_name
33+
class_base_function_regex = re.compile(
34+
"<\\w+ (\\w+)\\.(\\w+) at \\w+>", re.IGNORECASE
35+
)
36+
37+
def _decorator(func: t.Union[t.Callable, t.Any]) -> t.Union[t.Callable, t.Any]:
3938
if not callable(func) or isinstance(func, RouteOperationBase):
4039
warnings.warn_explicit(
4140
UserWarning(
@@ -52,23 +51,25 @@ def __call__(self, func: t.Union[t.Callable, t.Any]) -> t.Union[t.Callable, t.An
5251
endpoint_name = get_name(func)
5352
is_class_base_function = use_mvc = False
5453

55-
if self.class_base_function_regex.match(repr(func)):
54+
if class_base_function_regex.match(repr(func)):
5655
is_class_base_function = True
5756

58-
if not self.template_name and is_class_base_function:
57+
if not template_name and is_class_base_function:
5958
use_mvc = True
6059

61-
if not self.template_name and not is_class_base_function:
60+
if not template_name and not is_class_base_function:
6261
raise RenderDecoratorException(
6362
f"template_name is required for function endpoints. {func}"
6463
)
6564

6665
response = HTMLResponseModel(
67-
template_name=self.template_name or endpoint_name, use_mvc=use_mvc
66+
template_name=template_name or endpoint_name, use_mvc=use_mvc
6867
)
6968
target_decorator = set_meta(RESPONSE_OVERRIDE_KEY, {200: response})
7069
return target_decorator(func)
7170

71+
return _decorator
72+
7273

7374
def _validate_template_function(f: t.Any) -> None:
7475
if asyncio.iscoroutinefunction(f):

ellar/common/routing/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818

1919
_route_definitions = OperationDefinitions()
2020

21-
Get = _route_definitions.get
22-
Post = _route_definitions.post
21+
get = _route_definitions.get
22+
post = _route_definitions.post
2323

24-
Delete = _route_definitions.delete
25-
Patch = _route_definitions.patch
24+
delete = _route_definitions.delete
25+
patch = _route_definitions.patch
2626

27-
Put = _route_definitions.put
28-
Options = _route_definitions.options
27+
put = _route_definitions.put
28+
options = _route_definitions.options
2929

30-
Trace = _route_definitions.trace
31-
Head = _route_definitions.head
30+
trace = _route_definitions.trace
31+
head = _route_definitions.head
3232

33-
HttpRoute = _route_definitions.http_route
34-
WsRoute = _route_definitions.ws_route
33+
http_route = _route_definitions.http_route
34+
ws_route = _route_definitions.ws_route
3535

3636
__all__ = [
3737
"Ctx",
@@ -48,14 +48,14 @@
4848
"Query",
4949
"Param",
5050
"ParamTypes",
51-
"Get",
52-
"Post",
53-
"Delete",
54-
"Patch",
55-
"Put",
56-
"Options",
57-
"Trace",
58-
"Head",
59-
"HttpRoute",
60-
"WsRoute",
51+
"get",
52+
"post",
53+
"delete",
54+
"patch",
55+
"put",
56+
"options",
57+
"trace",
58+
"head",
59+
"http_route",
60+
"ws_route",
6161
]

ellar/compatible/contextmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

33
if sys.version_info >= (3, 7): # pragma: no cover
4-
from contextlib import asynccontextmanager as asynccontextmanager
4+
from contextlib import asynccontextmanager as asynccontextmanager # noqa
55
else:
66
from contextlib2 import asynccontextmanager as asynccontextmanager # noqa

ellar/core/routing/router/module.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,17 @@ def __init__(
191191
)
192192
_route_definitions = self.operation_definition_class(t.cast(list, self.routes))
193193

194-
self.Get = _route_definitions.get
195-
self.Post = _route_definitions.post
194+
self.get = _route_definitions.get
195+
self.post = _route_definitions.post
196196

197-
self.Delete = _route_definitions.delete
198-
self.Patch = _route_definitions.patch
197+
self.delete = _route_definitions.delete
198+
self.patch = _route_definitions.patch
199199

200-
self.Put = _route_definitions.put
201-
self.Options = _route_definitions.options
200+
self.put = _route_definitions.put
201+
self.options = _route_definitions.options
202202

203-
self.Trace = _route_definitions.trace
204-
self.Head = _route_definitions.head
203+
self.trace = _route_definitions.trace
204+
self.head = _route_definitions.head
205205

206-
self.HttpRoute = _route_definitions.http_route
207-
self.WsRoute = _route_definitions.ws_route
206+
self.http_route = _route_definitions.http_route
207+
self.ws_route = _route_definitions.ws_route

ellar/openapi/module.py

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

3-
from ellar.common import Get, Module, Render, guards as guards_decorator
3+
from ellar.common import Module, get, guards as guards_decorator, render
44
from ellar.core.guard import GuardCanActivate
55
from ellar.core.main import App
66
from ellar.core.modules import ModuleBase
@@ -26,7 +26,7 @@ def __init__(
2626
if not openapi_url and document:
2727
self._openapi_url = "/openapi.json"
2828

29-
@Get(self._openapi_url, include_in_schema=False)
29+
@get(self._openapi_url, include_in_schema=False)
3030
@guards_decorator(*self._guards)
3131
def openapi_schema() -> t.Any:
3232
assert document and isinstance(document, OpenAPI), "Invalid Document"
@@ -39,8 +39,8 @@ def _setup_docs(
3939
) -> None:
4040
_path = path.lstrip("/").rstrip("/")
4141

42-
@Get(f"/{_path}", include_in_schema=False)
43-
@Render(template_name)
42+
@get(f"/{_path}", include_in_schema=False)
43+
@render(template_name)
4444
@guards_decorator(*self._guards)
4545
def _doc() -> t.Any:
4646
return template_context

0 commit comments

Comments
 (0)