-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (60 loc) · 2.22 KB
/
Copy pathmain.py
File metadata and controls
73 lines (60 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import getopt
import sys
import typing as t
import typer
from ellar.commands import EllarTyper
from ellar.constants import CALLABLE_COMMAND_INFO, MODULE_METADATA
from ellar.core.factory import AppFactory
from ellar.services import Reflector
from typer import Typer
from typer.models import CommandInfo
from ellar_cli.constants import ELLAR_META
from .manage_commands import create_module, create_project, new_command, runserver
from .service import EllarCLIService
__all__ = ["build_typers", "_typer", "typer_callback"]
_typer = Typer(name="ellar")
_typer.command(name="new")(new_command)
_typer.command()(runserver)
_typer.command(name="create-project")(create_project)
_typer.command(name="create-module")(create_module)
@_typer.callback()
def typer_callback(
ctx: typer.Context,
project: t.Optional[str] = typer.Option(
None,
"-p",
"--project",
show_default=True,
exists=True,
help="Run Specific Command on a specific project",
),
) -> None:
meta_: t.Optional[EllarCLIService] = EllarCLIService.import_project_meta(project)
ctx.meta[ELLAR_META] = meta_
def build_typers() -> None:
try:
options, args = getopt.getopt(
sys.argv[1:],
"p:",
["project=", "help"],
)
app_name: t.Optional[str] = None
for k, v in options:
if k in ["-p", "--project"] and v:
app_name = v
except Exception:
raise typer.Abort()
meta_: t.Optional[EllarCLIService] = EllarCLIService.import_project_meta(app_name)
if meta_ and meta_.has_meta:
modules = AppFactory.get_all_modules(meta_.import_root_module())
reflector = Reflector()
for module in modules:
typers_commands = reflector.get(MODULE_METADATA.COMMANDS, module) or []
for typer_command in typers_commands:
if isinstance(typer_command, EllarTyper):
_typer.add_typer(typer_command)
elif hasattr(typer_command, CALLABLE_COMMAND_INFO):
command_info: CommandInfo = typer_command.__dict__[
CALLABLE_COMMAND_INFO
]
_typer.registered_commands.append(command_info)