fix vlan id bug
add new examples add magick for png output
This commit is contained in:
+31
-32
@@ -10,42 +10,15 @@ from py_d2.connection import Direction
|
||||
# https://d2lang.com/tour/themes/
|
||||
THEME_NUMBER = 200
|
||||
|
||||
"""
|
||||
PC2
|
||||
|
||||
PC2
|
||||
|
||||
PC3
|
||||
|
||||
PC3.adapter1 : {
|
||||
shape: parallelogram
|
||||
}
|
||||
|
||||
PC3.adapter2 : {
|
||||
shape: parallelogram
|
||||
}
|
||||
|
||||
netw_A : {
|
||||
shape: cloud
|
||||
}
|
||||
netw_B : {
|
||||
shape: cloud
|
||||
}
|
||||
|
||||
PC1.adapter1 -- netw_A
|
||||
PC2.adapter1 -- netw_A
|
||||
PC3.adapter1 -- netw_A
|
||||
|
||||
PC1.adapter2 -- netw_B
|
||||
PC3.adapter2 -- netw_B
|
||||
|
||||
"""
|
||||
|
||||
|
||||
def _check_d2_installed() -> bool:
|
||||
return shutil.which("d2") is not None
|
||||
|
||||
|
||||
def _check_magick_installed() -> bool:
|
||||
return shutil.which("magick") is not None
|
||||
|
||||
|
||||
def generate_d2_diagram(topology: Topology, output_path: Path) -> None:
|
||||
shapes = []
|
||||
connections = []
|
||||
@@ -80,7 +53,33 @@ def generate_d2_diagram(topology: Topology, output_path: Path) -> None:
|
||||
with open(output_path, "w", encoding="utf-8") as f:
|
||||
f.write(str(diagram))
|
||||
|
||||
_generate_picture(output_path, output_path.with_suffix(".png"))
|
||||
_generate_picture(
|
||||
output_path, output_path.with_suffix(".png")
|
||||
) # output_path not used
|
||||
|
||||
_run_magick_command(
|
||||
output_path.with_suffix(".svg"), output_path.with_suffix(".png")
|
||||
)
|
||||
|
||||
|
||||
def _run_magick_command(input_path: Path, output_path: Path) -> None:
|
||||
if not _check_magick_installed():
|
||||
raise EnvironmentError(
|
||||
"ImageMagick is not installed or 'magick' command is not found in PATH. Please install ImageMagick to use this feature."
|
||||
)
|
||||
|
||||
res = subprocess.run(
|
||||
["magick", "convert", str(input_path), str(output_path)],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
|
||||
if res.returncode != 0:
|
||||
raise RuntimeError(
|
||||
f"Image conversion failed (code {res.returncode})\n"
|
||||
f"Stdout: {res.stdout.decode()}\n"
|
||||
f"Stderr: {res.stderr.decode()}"
|
||||
)
|
||||
|
||||
|
||||
def _generate_picture(diagram: Path, output_path: Path) -> None:
|
||||
|
||||
@@ -60,7 +60,6 @@ def make_yaml(topology: Topology, output_path: Path) -> None:
|
||||
"name": interface.name,
|
||||
"members": interface.slave_interfaces,
|
||||
"ip": ip,
|
||||
"network": interface.network if interface.network else None,
|
||||
"gateway": (
|
||||
interface.default_gateway
|
||||
if interface.default_gateway
|
||||
@@ -75,12 +74,12 @@ def make_yaml(topology: Topology, output_path: Path) -> None:
|
||||
"name": interface.name,
|
||||
"parent": interface.parent_interface,
|
||||
"ip": ip,
|
||||
"network": interface.network if interface.network else None,
|
||||
"gateway": (
|
||||
interface.default_gateway
|
||||
if interface.default_gateway
|
||||
else None
|
||||
),
|
||||
"id": interface.vlan,
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
@@ -3,6 +3,10 @@ from pathlib import Path
|
||||
import graphviz
|
||||
import shutil
|
||||
|
||||
# TODO:
|
||||
# - find ithers implementations of graphviz
|
||||
# - actualize code
|
||||
|
||||
|
||||
def _check_graphviz_installed() -> bool:
|
||||
return shutil.which("dot") is not None
|
||||
|
||||
Reference in New Issue
Block a user