Skip to content

Commit 6d979f8

Browse files
committed
refactor
1 parent fb826d1 commit 6d979f8

26 files changed

+729
-1022
lines changed

django_typer/__init__.py

Lines changed: 398 additions & 829 deletions
Large diffs are not rendered by default.

django_typer/tests/apps/test_app/management/commands/graph.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing as t
22
from django.core.management.base import CommandError
33
import typer.core
4-
from django_typer import TyperCommand, completers, get_command, Typer, CommandGroup
4+
from django_typer import TyperCommand, completers, get_command, Typer
55
import importlib
66
import sys
77

@@ -38,10 +38,10 @@ class Command(TyperCommand):
3838

3939
def handle(
4040
self,
41-
command: Annotated[
42-
str,
41+
commands: Annotated[
42+
t.List[str],
4343
typer.Argument(
44-
help="An import path to the command to graph, or simply the name of the command.",
44+
help="Import path(s) to the command to graph, or simply the name of the command.",
4545
shell_complete=completers.complete_import_path,
4646
),
4747
],
@@ -68,22 +68,32 @@ def handle(
6868
typer.Option(help="Instantiate the command before graphing the app tree."),
6969
] = True,
7070
):
71-
self.cmd_name = command.split(".")[-1]
72-
if "." in command:
73-
self.cmd = getattr(importlib.import_module(command), "Command")
74-
if instantiate:
75-
self.cmd = self.cmd()
76-
elif instantiate:
77-
self.cmd = get_command(command, TyperCommand)
78-
else:
79-
raise CommandError("Cannot instantiate a command that is not imported.")
80-
81-
output = Path(output.parent) / Path(output.name.format(command=self.cmd_name))
82-
83-
self.visit_app(self.cmd.typer_app)
84-
self.dot.render(output, format=format, view=True)
85-
86-
def get_node_name(self, obj: t.Union[typer.models.CommandInfo, CommandGroup]):
71+
for command in commands:
72+
self.level = -1
73+
self.cmd_name = command.split(".")[-1]
74+
if "." in command:
75+
self.cmd = getattr(importlib.import_module(command), "Command")
76+
if instantiate:
77+
self.cmd = self.cmd()
78+
elif instantiate:
79+
self.cmd = get_command(command, TyperCommand)
80+
else:
81+
raise CommandError("Cannot instantiate a command that is not imported.")
82+
83+
output = Path(output.parent) / Path(
84+
output.name.format(command=self.cmd_name)
85+
)
86+
87+
self.visit_app(self.cmd.typer_app)
88+
89+
of = output.parent / f"{output.stem}.{format}"
90+
num = 1
91+
while of.exists():
92+
of = output.parent / f"{output.stem}({num}).{format}"
93+
num += 1
94+
self.dot.render(of.parent / of.stem, format=format, view=len(commands) == 1)
95+
96+
def get_node_name(self, obj: t.Union[typer.models.CommandInfo, Typer]):
8797
assert obj.callback
8898
name = (
8999
obj.callback.__name__
@@ -106,13 +116,16 @@ def visit_app(self, app: Typer):
106116
self.level += 1
107117
parent_node = self.get_node_name(app) if self.level else self.cmd_name
108118

109-
self.dot.node(str(id(app)), parent_node)
119+
style = {}
120+
if self.level:
121+
style = {"style": "filled", "fillcolor": "lightblue"}
122+
self.dot.node(str(id(app)), parent_node, **style)
110123
for cmd in app.registered_commands:
111124
node_name = self.get_node_name(cmd)
112-
self.dot.node(str(id(cmd)), node_name)
125+
self.dot.node(str(id(cmd)), node_name, style="filled", fillcolor="green")
113126
self.dot.edge(str(id(app)), str(id(cmd)))
114127
for grp in app.registered_groups:
115128
assert grp.typer_instance
116-
node_name = self.get_node_name(t.cast(CommandGroup, grp.typer_instance))
129+
node_name = self.get_node_name(t.cast(Typer, grp.typer_instance))
117130
self.dot.edge(str(id(app)), str(id(grp.typer_instance)))
118131
self.visit_app(t.cast(Typer, grp.typer_instance))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import typing as t
2+
from django_typer import TyperCommand
3+
4+
5+
class Command(TyperCommand):
6+
help = "Test various forms of handle override."
7+
8+
def handle(self) -> str:
9+
return "handle"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import typing as t
2+
from django_typer import command
3+
from .handle import Command as Handle
4+
5+
6+
class Command(Handle):
7+
help = "Test various forms of handle override."
8+
9+
@command()
10+
def handle(self) -> str:
11+
return "handle1"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import typing as t
2+
from django_typer import TyperCommand, command
3+
4+
5+
class Command(TyperCommand):
6+
help = "Test various forms of handle override."
7+
8+
@command()
9+
def handle(self) -> str:
10+
return "handle2"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import typing as t
2+
from django_typer import TyperCommand, command
3+
from .handle import Command as Handle
4+
5+
6+
class Command(Handle):
7+
help = "Test various forms of handle override."
8+
9+
def handle(self) -> str:
10+
return "handle3"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .handle2 import Command as Handle
2+
3+
4+
class Command(Handle):
5+
help = "Test various forms of handle override."
6+
7+
def handle(self) -> str:
8+
return "handle4"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django_typer import TyperCommand
2+
3+
4+
class Command(TyperCommand):
5+
help = "Test no commands."

django_typer/tests/apps/test_app/management/commands/native_groups.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from django_typer import Typer, TyperCommand, CommandGroup
1+
from django_typer import Typer, TyperCommand
22
from django_typer.types import Verbosity
3-
import typing as t
43

54
Command: TyperCommand
65

@@ -24,9 +23,6 @@ def main(name: str):
2423

2524
grp2 = Typer()
2625

27-
# # this does not work
28-
# app.add_typer(t.cast(CommandGroup, grp2))
29-
3026

3127
@grp2.callback(name="grp1")
3228
def init_grp1(flag: bool = False):
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from .native import Command as Native
1+
from . import native
2+
from django_typer import Typer
23

3-
4-
class Command(Native):
5-
pass
4+
app = Typer(native.app)

0 commit comments

Comments
 (0)