Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/dishka/plotter/mermaid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import re
import html

from dishka.plotter.model import Group, Node, NodeType, Renderer

Expand All @@ -24,6 +24,12 @@
"""


# these symbols should be escaped additionally to html.escape
MERMAID_SYMBOLS_SUBST = str.maketrans({
"<": "&lt",
">": "&gt",
})

class MermaidRenderer(Renderer):
def __init__(self) -> None:
self.nodes: dict[str, Node] = {}
Expand All @@ -32,18 +38,20 @@ def _render_node(self, node: Node) -> str:
if node.type is NodeType.ALIAS:
return ""
name = self._node_type(node) + self._escape(node.name)
return (
f'class {node.id}["{name}"]'
+ "{\n"
+ (f" {node.source_name}()\n" if node.source_name else " \n")
+ "".join(
f" {self.nodes[dep].name}\n" for dep in node.dependencies
)
+ "}\n"
)
source_name = self._escape(node.source_name)
return "\n".join([
f'class {node.id}["{name}"]{{',
f"{source_name}()" if source_name else " ",
*(
f"{self._escape(self.nodes[dep].name)}"
for dep in node.dependencies
),
"}",
])

def _escape(self, line: str) -> str:
return re.sub(r"[^\w_.\-]", "_", line)
line = line.translate(MERMAID_SYMBOLS_SUBST)
return html.escape(line, quote=True)

def _render_node_deps(self, node: Node) -> list[str]:
res: list[str] = []
Expand Down
6 changes: 6 additions & 0 deletions src/dishka/text_rendering/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def _render_args(hint: Any) -> str:


def get_name(hint: Any, *, include_module: bool) -> str:
if isinstance(hint, list):
res = ",".join(
get_name(item, include_module=include_module)
for item in hint
)
return f"[{res}]"
if hint is ...:
return "..."
if func := getattr(object, "__func__", None):
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/text_rendering/test_name.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Callable
from typing import Generic, TypeVar

import pytest
Expand Down Expand Up @@ -42,6 +43,7 @@ class GenericA(Generic[T]):
(dishka.Scope, True, "dishka.entities.scope.Scope"),
(GenericA[int], False, "GenericA[int]"),
(GenericA[T], False, "GenericA[T]"),
(Callable[[str], str], False, "Callable[[str], str]"),
(GenericA, False, "GenericA"),
],
)
Expand Down
Loading