Add graph, analyzers, and exporters modules
This commit is contained in:
60
src/exporters/png.py
Normal file
60
src/exporters/png.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from src.graph.builder import GraphBuilder
|
||||
from src.exporters.dot import DOTExporter
|
||||
|
||||
|
||||
class PNGExporter:
|
||||
def __init__(self, graph_builder: GraphBuilder):
|
||||
self.graph_builder = graph_builder
|
||||
self.dot_exporter = DOTExporter(graph_builder)
|
||||
|
||||
def export(
|
||||
self,
|
||||
output_path: Path,
|
||||
layout: str = "dot",
|
||||
rankdir: str = "TB",
|
||||
format: str = "png",
|
||||
) -> None:
|
||||
dot_content = self.dot_exporter.get_string(layout=layout, rankdir=rankdir)
|
||||
|
||||
self._render_graph(dot_content, output_path, format)
|
||||
|
||||
def export_to_svg(self, output_path: Path, layout: str = "dot", rankdir: str = "TB") -> None:
|
||||
dot_content = self.dot_exporter.get_string(layout=layout, rankdir=rankdir)
|
||||
self._render_graph(dot_content, output_path, "svg")
|
||||
|
||||
def _render_graph(self, dot_content: str, output_path: Path, format: str) -> None:
|
||||
try:
|
||||
subprocess.run(
|
||||
["dot", f"-T{format}", "-o", str(output_path)],
|
||||
input=dot_content,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
raise RuntimeError(
|
||||
"Graphviz is not installed. Please install it with: "
|
||||
"apt-get install graphviz (Ubuntu) or brew install graphviz (macOS)"
|
||||
)
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise RuntimeError(f"Graphviz rendering failed: {e.stderr}")
|
||||
|
||||
def preview(self, layout: str = "dot") -> None:
|
||||
dot_content = self.dot_exporter.get_string(layout=layout)
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["dot", "-Tpng"],
|
||||
input=dot_content,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
print(f"PNG preview generated ({len(result.stdout)} bytes)")
|
||||
except FileNotFoundError:
|
||||
print("Graphviz not installed for preview")
|
||||
|
||||
def get_available_layouts(self) -> list[str]:
|
||||
return ["dot", "neato", "fdp", "sfdp", "twopi", "circo"]
|
||||
Reference in New Issue
Block a user