Skip to content

Commit 7ad4472

Browse files
authored
Merge pull request #49 from python-ellar/simple_lazy_update
Simple lazy update
2 parents d129584 + 75bcaff commit 7ad4472

File tree

12 files changed

+80
-42
lines changed

12 files changed

+80
-42
lines changed

ellar_cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Ellar CLI Tool for Scaffolding Ellar Projects, Modules and also running Ellar Commands"""
22

3-
__version__ = "0.2.4"
3+
__version__ = "0.2.5"

ellar_cli/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from ellar.app import AppFactory
99
from ellar.common.commands import EllarTyper
1010
from ellar.common.constants import CALLABLE_COMMAND_INFO, MODULE_METADATA
11+
from ellar.core import reflector
1112
from ellar.core.modules import ModuleSetup
12-
from ellar.core.services import Reflector
1313
from typer.models import CommandInfo
1414

1515
import ellar_cli
@@ -59,6 +59,8 @@ def typer_callback(
5959
) -> None:
6060
meta_: t.Optional[EllarCLIService] = EllarCLIService.import_project_meta(project)
6161
ctx.meta[ELLAR_META] = meta_
62+
if meta_ and meta_.has_meta:
63+
ctx.with_resource(meta_.get_application_context())
6264

6365

6466
def build_typers() -> t.Any: # pragma: no cover
@@ -82,7 +84,6 @@ def build_typers() -> t.Any: # pragma: no cover
8284
module_configs = AppFactory.get_all_modules(
8385
ModuleSetup(meta_.import_root_module())
8486
)
85-
reflector = Reflector()
8687

8788
for module_config in module_configs:
8889
typers_commands = (

ellar_cli/manage_commands/create_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pathlib import Path
66

77
import typer
8-
from ellar.common.helper.module_loading import module_dir
8+
from ellar.common.utils.module_loading import module_dir
99

1010
from ellar_cli import scaffolding
1111
from ellar_cli.schema import EllarScaffoldSchema

ellar_cli/manage_commands/create_project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pathlib import Path
66

77
import typer
8-
from ellar.common.helper.module_loading import module_dir
8+
from ellar.common.utils.module_loading import module_dir
99

1010
from ellar_cli import scaffolding
1111
from ellar_cli.constants import ELLAR_META

ellar_cli/manage_commands/new.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import typing as t
55

66
import typer
7-
from ellar.common.helper.module_loading import module_dir
7+
from ellar.common.utils.module_loading import module_dir
88

99
from ellar_cli import scaffolding
1010
from ellar_cli.schema import EllarScaffoldSchema

ellar_cli/manage_commands/runserver.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import ssl
22
import sys
33
import typing as t
4+
from datetime import datetime
45
from pathlib import Path
56

7+
import ellar
68
import typer
7-
from ellar.common.helper.enums import create_enums_from_list
9+
from ellar.app import current_config
10+
from ellar.common.utils.enums import create_enums_from_list
811
from h11._connection import DEFAULT_MAX_INCOMPLETE_EVENT_SIZE
912
from uvicorn.config import (
1013
HTTP_PROTOCOLS,
@@ -245,12 +248,12 @@ def runserver(
245248
"No available project found. please create ellar project with `ellar create-project 'project-name'`"
246249
)
247250

248-
application = None if factory else ellar_project_meta.project_meta.application
251+
application_import_string = (
252+
None if factory else ellar_project_meta.project_meta.application
253+
)
249254

250-
config = ellar_project_meta.get_application_config()
251-
252-
log_config = config.LOGGING_CONFIG
253-
_log_level = config.LOG_LEVEL
255+
log_config = current_config.LOGGING_CONFIG
256+
_log_level = current_config.LOG_LEVEL
254257

255258
_log_level = log_level if log_level else _log_level or LOG_LEVELS.info
256259

@@ -304,4 +307,12 @@ def runserver(
304307
ws_per_message_deflate=ws_per_message_deflate,
305308
)
306309

307-
uvicorn_run(application, **init_kwargs)
310+
now = datetime.now().strftime("%B %d, %Y - %X")
311+
version = ellar.__version__
312+
print(
313+
f"\nStarting Uvicorn server...\n"
314+
f"{now}\n"
315+
f"Ellar version {version}, using settings {current_config.config_module!r}\n"
316+
)
317+
318+
uvicorn_run(application_import_string, **init_kwargs)

ellar_cli/service.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import functools
12
import os
23
import typing as t
34

45
from click import ClickException
56
from ellar.app import App
7+
from ellar.app.context import ApplicationContext
68
from ellar.common.constants import ELLAR_CONFIG_MODULE
7-
from ellar.common.helper.importer import import_from_string
9+
from ellar.common.utils.importer import import_from_string
810
from ellar.core import Config, ModuleBase
911
from tomlkit import dumps as tomlkit_dumps
1012
from tomlkit import parse as tomlkit_parse
@@ -19,6 +21,21 @@
1921
ELLAR_PROJECTS_KEY = "projects"
2022

2123

24+
def _export_ellar_config_module(func: t.Callable) -> t.Callable:
25+
"""Ensure Ellar Config Module is Exported"""
26+
27+
@functools.wraps(func)
28+
def _wrap(self: "EllarCLIService", *args: t.Any, **kwargs: t.Any) -> t.Any:
29+
if os.environ.get(ELLAR_CONFIG_MODULE) is None and self.has_meta:
30+
# export ELLAR_CONFIG_MODULE module
31+
os.environ.setdefault(
32+
ELLAR_CONFIG_MODULE, self._meta.config # type:ignore[union-attr]
33+
)
34+
return func(self, *args, **kwargs)
35+
36+
return _wrap
37+
38+
2239
class EllarCLIException(ClickException):
2340
pass
2441

@@ -189,6 +206,7 @@ def write_py_project(path: str, content: Table) -> None:
189206
with open(path, mode="w") as fw:
190207
fw.writelines(tomlkit_dumps(content))
191208

209+
@_export_ellar_config_module
192210
def import_application(self) -> "App":
193211
assert self._meta
194212
application_module = t.cast("App", import_from_string(self._meta.application))
@@ -199,14 +217,21 @@ def import_configuration(self) -> t.Type["Config"]:
199217
config = t.cast(t.Type["Config"], import_from_string(self._meta.config))
200218
return config
201219

220+
@_export_ellar_config_module
202221
def get_application_config(self) -> "Config":
203222
assert self._meta
204223
config = Config(os.environ.get(ELLAR_CONFIG_MODULE, self._meta.config))
205224
return config
206225

226+
@_export_ellar_config_module
207227
def import_root_module(self) -> t.Type["ModuleBase"]:
208228
assert self._meta
209229
root_module = t.cast(
210230
t.Type["ModuleBase"], import_from_string(self._meta.root_module)
211231
)
212232
return root_module
233+
234+
@_export_ellar_config_module
235+
def get_application_context(self) -> ApplicationContext:
236+
app = t.cast(App, self.import_application())
237+
return app.application_context()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dependencies = [
4343
# exclude 0.11.2 and 0.11.3 due to https://github.com/sdispater/tomlkit/issues/225
4444
"tomlkit >=0.11.1,<1.0.0,!=0.11.2,!=0.11.3",
4545
"uvicorn[standard] == 0.23.2",
46-
"ellar >= 0.5.3"
46+
"ellar >= 0.5.7"
4747
]
4848

4949
[project.scripts]

tests/test_ellar_commands/test_create_module_command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_create_module_fails_for_invalid_module_name(
1414

1515
def test_create_module_with_directory_case_1(process_runner, tmpdir):
1616
result = process_runner(["ellar", "create-module", "test_new_module", "app/"])
17-
assert result.returncode == 0
17+
assert result.returncode == 0, result.stderr
1818
assert result.stdout == b"test_new_module module completely scaffolded\n"
1919

2020
module_path = os.path.join(tmpdir, "app", "test_new_module")
@@ -36,7 +36,7 @@ def test_create_module_with_directory_case_2(process_runner, tmpdir):
3636
result = process_runner(
3737
["ellar", "create-module", "test_new_module", "."], cwd=tmpdir
3838
)
39-
assert result.returncode == 0
39+
assert result.returncode == 0, result.stderr
4040
assert result.stdout == b"test_new_module module completely scaffolded\n"
4141

4242
files = os.listdir(tmpdir / "test_new_module")
@@ -101,7 +101,7 @@ def test_create_module_works(tmpdir, process_runner, write_empty_py_project):
101101
"test_new_module",
102102
]
103103
)
104-
assert result.returncode == 0
104+
assert result.returncode == 0, result.stderr
105105
assert result.stdout == b"test_new_module module completely scaffolded\n"
106106

107107
module_path = os.path.join(tmpdir, "test_new_module")

tests/test_ellar_commands/test_create_project_command.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ def test_create_project_fails_for_py_project_none(cli_runner):
1313

1414

1515
def test_create_project_fails_for_existing_project_name(
16-
cli_runner, add_ellar_project_to_py_project
16+
cli_runner, write_empty_py_project, tmpdir
1717
):
18-
add_ellar_project_to_py_project("testing_new_project")
19-
result = cli_runner.invoke_ellar_command(["create-project", "testing_new_project"])
18+
module_name = "testing_new_project"
19+
os.makedirs(os.path.join(tmpdir, module_name), exist_ok=True)
20+
# add_ellar_project_to_py_project("testing_new_project")
21+
result = cli_runner.invoke_ellar_command(["create-project", module_name])
22+
assert result.exit_code == 0
23+
result = cli_runner.invoke_ellar_command(["create-project", module_name])
24+
2025
assert result.exit_code == 1
2126
assert result.output == "Error: Ellar Project already exist.\n"
2227

@@ -54,7 +59,7 @@ def test_create_project_works_for_existing_folder_with_same_project_name(
5459
os.makedirs(os.path.join(tmpdir, module_name), exist_ok=True)
5560

5661
result = cli_runner.invoke_ellar_command(["create-project", module_name])
57-
assert result.exit_code == 0
62+
assert result.exit_code == 0, result.stderr
5863
assert result.output == (
5964
"`new_project_module_2` project scaffold completed. To start your server, run the command below\n"
6065
"ellar --project new_project_module_2 runserver --reload\n"
@@ -64,7 +69,7 @@ def test_create_project_works_for_existing_folder_with_same_project_name(
6469

6570
def test_create_project_command_works(tmpdir, process_runner, write_empty_py_project):
6671
result = process_runner(["ellar", "create-project", "ellar_project"])
67-
assert result.returncode == 0
72+
assert result.returncode == 0, result.stderr
6873
assert result.stdout.decode("utf8") == (
6974
"`ellar_project` project scaffold completed. To start your server, run the command below\n"
7075
"ellar --project ellar_project runserver --reload\n"
@@ -86,7 +91,7 @@ def test_create_project_command_works_with_specific_directory(
8691
tmpdir, process_runner, write_empty_py_project
8792
):
8893
result = process_runner(["ellar", "create-project", "ellar_project", "Another/me"])
89-
assert result.returncode == 0
94+
assert result.returncode == 0, result.stderr
9095
assert result.stdout.decode("utf8") == (
9196
"`ellar_project` project scaffold completed. To start your server, run the command below\n"
9297
"ellar --project ellar_project runserver --reload\n"

0 commit comments

Comments
 (0)