Skip to content

Commit 9a7782d

Browse files
committed
code refactor
1 parent f12a294 commit 9a7782d

File tree

11 files changed

+37
-44
lines changed

11 files changed

+37
-44
lines changed

ellar/core/conf/app_settings_models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from .mixins import (
1515
ConfigDefaultTypesMixin,
16-
TBaseCacheBackend,
1716
TEventHandler,
1817
TExceptionHandler,
1918
TMiddleware,
@@ -121,7 +120,7 @@ class Config:
121120
TEMPLATE_GLOBAL_FILTERS: t.Dict[str, t.Callable[..., t.Any]] = {}
122121

123122
LOGGING: t.Optional[t.Dict[str, t.Any]] = None
124-
CACHES: t.Dict[str, TBaseCacheBackend] = {}
123+
CACHES: t.Dict[str, t.Any] = {}
125124

126125
@validator("MIDDLEWARE", pre=True)
127126
def pre_middleware_validate(cls, value: t.Any) -> t.Any:

ellar/core/conf/mixins.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from starlette.responses import JSONResponse
55
from starlette.types import ASGIApp
66

7-
from ellar.cache.model import BaseCacheBackend
87
from ellar.constants import LOG_LEVELS as log_levels
98
from ellar.core.events import EventHandler
109
from ellar.core.exceptions.interfaces import IExceptionHandler
@@ -20,25 +19,9 @@
2019
"TMiddleware",
2120
"TEventHandler",
2221
"TExceptionHandler",
23-
"TBaseCacheBackend",
2422
]
2523

2624

27-
class TBaseCacheBackend:
28-
@classmethod
29-
def __get_validators__(
30-
cls: t.Type["TBaseCacheBackend"],
31-
) -> t.Iterable[t.Callable[..., t.Any]]:
32-
yield cls.validate
33-
34-
@classmethod
35-
def validate(cls: t.Type["TBaseCacheBackend"], v: t.Any) -> t.Any:
36-
if isinstance(v, BaseCacheBackend):
37-
return v
38-
39-
raise ValueError(f"Expected BaseCacheBackend, received: {type(v)}")
40-
41-
4225
class TExceptionHandler:
4326
@classmethod
4427
def __get_validators__(
@@ -182,4 +165,4 @@ class ConfigDefaultTypesMixin:
182165
ALLOWED_HOSTS: t.List[str]
183166
REDIRECT_HOST: bool
184167

185-
CACHES: t.Dict[str, TBaseCacheBackend]
168+
CACHES: t.Dict[str, t.Any]

ellar/core/factory.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _build_modules(
9696

9797
injector.add_module(module_ref)
9898

99-
for module_config in injector.get_dynamic_modules():
99+
for module_config in reversed(list(injector.get_dynamic_modules())):
100100
if injector.get_module(module_config.module): # pragma: no cover
101101
continue
102102

@@ -116,11 +116,24 @@ def _create_app(
116116
global_guards: t.List[
117117
t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]
118118
] = None,
119-
config_module: str = None,
119+
config_module: t.Union[str, t.Dict] = None,
120120
) -> App:
121+
def _get_config_kwargs() -> t.Dict:
122+
if config_module is None:
123+
return {}
124+
125+
if not isinstance(config_module, (str, dict)):
126+
raise Exception(
127+
"config_module should be a importable str or a dict object"
128+
)
129+
130+
if isinstance(config_module, str):
131+
return dict(config_module=config_module)
132+
return dict(config_module)
133+
121134
assert reflect.get_metadata(MODULE_WATERMARK, module), "Only Module is allowed"
122135

123-
config = Config(app_configured=True, config_module=config_module)
136+
config = Config(app_configured=True, **_get_config_kwargs())
124137
injector = EllarInjector(auto_bind=config.INJECTOR_AUTO_BIND)
125138
injector.container.register_instance(config, concrete_type=Config)
126139
CoreServiceRegistration(injector, config).register_all()
@@ -143,7 +156,7 @@ def _create_app(
143156
routes = []
144157
module_changed = False
145158

146-
for module_config in injector.get_app_dependent_modules():
159+
for module_config in reversed(list(injector.get_app_dependent_modules())):
147160
if injector.get_module(module_config.module): # pragma: no cover
148161
continue
149162

@@ -179,7 +192,7 @@ def create_app(
179192
t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]
180193
] = None,
181194
commands: t.Sequence[t.Union[t.Callable, "EllarTyper"]] = tuple(),
182-
config_module: str = None,
195+
config_module: t.Union[str, t.Dict] = None,
183196
) -> App:
184197
from ellar.common import Module
185198

@@ -208,7 +221,7 @@ def create_from_app_module(
208221
global_guards: t.List[
209222
t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]
210223
] = None,
211-
config_module: str = None,
224+
config_module: t.Union[str, t.Dict] = None,
212225
) -> App:
213226
return cls._create_app(
214227
module, config_module=config_module, global_guards=global_guards

ellar/core/services.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import typing as t
2-
3-
from ellar.cache import BaseCacheBackend, CacheService, ICacheService
41
from ellar.di import EllarInjector
52
from ellar.services.reflector import Reflector
63

@@ -44,9 +41,5 @@ def register_all(self) -> None:
4441
self.injector.container.register_scoped(
4542
IWebSocketContextFactory, WebSocketContextFactory
4643
)
47-
cache_service = CacheService(
48-
t.cast(t.Dict[str, BaseCacheBackend], dict(self.config.CACHES))
49-
)
44+
5045
self.injector.container.register_instance(Reflector())
51-
self.injector.container.register_instance(cache_service)
52-
self.injector.container.register(ICacheService, cache_service)

ellar/core/testclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def create_test_module(
4949
global_guards: t.List[
5050
t.Union[t.Type["GuardCanActivate"], "GuardCanActivate"]
5151
] = None,
52-
config_module: str = None,
52+
config_module: t.Union[str, t.Dict] = None,
5353
) -> _TestingModule:
5454
"""
5555
Create a TestingModule to test controllers and services in isolation

ellar/openapi/docs_generators/redocs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def __init__(
2323
path: str = "redoc",
2424
title: str = "Ellar Redoc",
2525
redoc_js_url: str = "https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js",
26-
redoc_favicon_url: str = "https://eadwincode.github.io/ellar/img/Icon.svg",
26+
favicon_url: str = "https://eadwincode.github.io/ellar/img/Icon.svg",
2727
with_google_fonts: bool = True,
2828
):
2929
self._path = path
3030
self._title = title
3131
self._template_context = dict(
3232
redoc_js_url=redoc_js_url,
33-
redoc_favicon_url=redoc_favicon_url,
33+
favicon_url=favicon_url,
3434
with_google_fonts=with_google_fonts,
3535
)

ellar/openapi/docs_generators/swagger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def __init__(
2424
title: str = "Ellar Swagger Doc",
2525
swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js",
2626
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css",
27-
swagger_favicon_url: str = "https://eadwincode.github.io/ellar/img/Icon.svg",
27+
favicon_url: str = "https://eadwincode.github.io/ellar/img/Icon.svg",
2828
):
2929
self._path = path
3030
self._title = title
3131
self._template_context = dict(
3232
swagger_js_url=swagger_js_url,
3333
swagger_css_url=swagger_css_url,
34-
swagger_favicon_url=swagger_favicon_url,
34+
favicon_url=favicon_url,
3535
)

ellar/openapi/module.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,21 @@ def openapi_schema() -> t.Any:
4242
for doc_gen in _document_generator:
4343
if not isinstance(doc_gen, IDocumentationGenerator):
4444
raise Exception(
45-
f"{doc_gen.__class__.__name__ if not isinstance(doc_gen, type) else doc_gen.__name__} must be of type `IDocumentationGenerator`"
45+
f"{doc_gen.__class__.__name__ if not isinstance(doc_gen, type) else doc_gen.__name__} "
46+
f"must be of type `IDocumentationGenerator`"
4647
)
4748

49+
template_context = dict(doc_gen.template_context)
50+
template_context.setdefault(
51+
"favicon_url", "https://eadwincode.github.io/ellar/img/Icon.svg"
52+
)
4853
cls._setup_document_manager(
4954
router=router,
5055
template_name=doc_gen.template_name,
5156
path=doc_gen.path,
5257
openapi_url=openapi_url,
5358
title=doc_gen.title,
54-
**doc_gen.template_context,
59+
**template_context,
5560
)
5661
return DynamicModule(module=cls, providers=[], routers=(router,))
5762

ellar/openapi/templates/redocs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{% if with_google_fonts %}
99
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
1010
{% endif %}
11-
<link rel="shortcut icon" href="{{redoc_favicon_url}}">
11+
<link rel="shortcut icon" href="{{favicon_url}}">
1212
<!--
1313
ReDoc doesn't change outer page styles
1414
-->

ellar/openapi/templates/swagger.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8"/>
55
<link type="text/css" rel="stylesheet" href="{{swagger_css_url}}">
6-
<link rel="shortcut icon" href="{{swagger_favicon_url}}">
6+
<link rel="shortcut icon" href="{{favicon_url}}">
77
<title>{{title}}</title>
88
</head>
99
<body>

0 commit comments

Comments
 (0)