Source code for pyvcad_rendering.tree_diagram

import shlex
import subprocess
import tempfile
from pathlib import Path

import pyvcad as pv


def _tree_to_mermaid_source(tree_or_diagram):
    if isinstance(tree_or_diagram, pv.TreeDiagram):
        return tree_or_diagram.to_mermaid()

    return pv.TreeDiagram(tree_or_diagram).to_mermaid()


def _save_mermaid_source(mermaid_source, output_path, mermaid_command):
    output_path = Path(output_path)
    output_path.parent.mkdir(parents=True, exist_ok=True)

    suffix = output_path.suffix.lower()
    if suffix == ".mmd":
        output_path.write_text(mermaid_source, encoding="utf-8")
        return mermaid_source

    if suffix not in {".svg", ".png"}:
        raise ValueError("Tree diagram rendering only supports .mmd, .svg, or .png outputs.")

    with tempfile.NamedTemporaryFile("w", suffix=".mmd", delete=False, encoding="utf-8") as handle:
        handle.write(mermaid_source)
        input_path = Path(handle.name)

    command = shlex.split(mermaid_command)
    command.extend(["-i", str(input_path), "-o", str(output_path)])

    try:
        subprocess.run(command, check=True, capture_output=True, text=True)
    except FileNotFoundError as exc:
        raise RuntimeError(
            "Mermaid CLI command was not found. Install `mmdc` and ensure it is on PATH."
        ) from exc
    except subprocess.CalledProcessError as exc:
        message = exc.stderr.strip() or exc.stdout.strip() or str(exc)
        raise RuntimeError("Mermaid CLI failed: " + message) from exc
    finally:
        input_path.unlink(missing_ok=True)

    return mermaid_source


[docs] def render_tree_diagram(tree_or_diagram, output_path, mermaid_command="mmdc"): """Save a Mermaid diagram for an authored OpenVCAD tree. Arguments: tree_or_diagram (Node | TreeDiagram): The root node or a pre-built TreeDiagram. output_path (str | Path): Destination `.mmd`, `.svg`, or `.png` path. mermaid_command (str): Mermaid CLI executable or command prefix. Defaults to `mmdc`. Returns: str: The Mermaid source used to generate the saved output. """ mermaid_source = _tree_to_mermaid_source(tree_or_diagram) return _save_mermaid_source(mermaid_source, output_path, mermaid_command)
[docs] def render_tree_legend(output_path, mermaid_command="mmdc"): """Save the hardcoded OpenVCAD tree-diagram legend. Arguments: output_path (str | Path): Destination `.mmd`, `.svg`, or `.png` path. mermaid_command (str): Mermaid CLI executable or command prefix. Defaults to `mmdc`. Returns: str: The Mermaid source used to generate the saved output. """ return _save_mermaid_source(pv.TreeDiagram.master_legend(), output_path, mermaid_command)
[docs] def Render_Tree_Diagram(tree_or_diagram, output_path, mermaid_command="mmdc"): """Alias for :func:`render_tree_diagram`.""" return render_tree_diagram(tree_or_diagram, output_path, mermaid_command)
[docs] def Render_Tree_Legend(output_path, mermaid_command="mmdc"): """Alias for :func:`render_tree_legend`.""" return render_tree_legend(output_path, mermaid_command)
__all__ = ["render_tree_diagram", "Render_Tree_Diagram", "render_tree_legend", "Render_Tree_Legend"]