From 8c564b7cea513cb67acce02c64b89b3480eb20c8 Mon Sep 17 00:00:00 2001 From: konstantin Date: Tue, 29 Oct 2024 17:39:45 +0100 Subject: [PATCH] fix: make kroki client (and port) configurable (#281) Co-authored-by: Konstantin --- README.md | 4 +++- src/rebdhuhn/graphviz.py | 11 +++-------- src/rebdhuhn/plantuml.py | 6 ++---- unittests/test_table_to_graph.py | 6 +++--- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 2819107..42ef07d 100644 --- a/README.md +++ b/README.md @@ -144,8 +144,10 @@ docker-compose up -d To export the graph as SVG, use ```python from rebdhuhn import convert_plantuml_to_svg_kroki +from rebdhuhn.kroki import Kroki -svg_code = convert_plantuml_to_svg_kroki(plantuml_code) +kroki_client = Kroki() +svg_code = convert_plantuml_to_svg_kroki(plantuml_code, kroki_client) with open("e_0003.svg", "w+", encoding="utf-8") as svg_file: svg_file.write(svg_code) ``` diff --git a/src/rebdhuhn/graphviz.py b/src/rebdhuhn/graphviz.py index c64bc4c..91d3402 100644 --- a/src/rebdhuhn/graphviz.py +++ b/src/rebdhuhn/graphviz.py @@ -2,13 +2,13 @@ This module contains logic to convert EbdGraph data to dot code (Graphviz) and further to parse this code to SVG images. """ -from typing import List, Optional +from typing import List from xml.sax.saxutils import escape from rebdhuhn.add_watermark import add_background as add_background_function from rebdhuhn.add_watermark import add_watermark as add_watermark_function from rebdhuhn.graph_utils import _mark_last_common_ancestors -from rebdhuhn.kroki import DotToSvgConverter, Kroki +from rebdhuhn.kroki import DotToSvgConverter from rebdhuhn.models import DecisionNode, EbdGraph, EbdGraphEdge, EndNode, OutcomeNode, StartNode, ToNoEdge, ToYesEdge ADD_INDENT = " " #: This is just for style purposes to make the plantuml files human-readable. @@ -169,10 +169,7 @@ def convert_graph_to_dot(ebd_graph: EbdGraph) -> str: def convert_dot_to_svg_kroki( - dot_code: str, - add_watermark: bool = True, - add_background: bool = True, - dot_to_svg_converter: Optional[DotToSvgConverter] = None, + dot_code: str, dot_to_svg_converter: DotToSvgConverter, add_watermark: bool = True, add_background: bool = True ) -> str: """ Converts dot code to svg (code) and returns the result as string. It uses kroki.io. @@ -180,8 +177,6 @@ def convert_dot_to_svg_kroki( Optionally add a background with the color 'HF white', controlled by the argument 'add_background' If 'add_background' is False, the background is transparent. """ - if dot_to_svg_converter is None: - dot_to_svg_converter = Kroki() svg_out = dot_to_svg_converter.convert_dot_to_svg(dot_code) if add_watermark: svg_out = add_watermark_function(svg_out) diff --git a/src/rebdhuhn/plantuml.py b/src/rebdhuhn/plantuml.py index fd99b13..cd888f6 100644 --- a/src/rebdhuhn/plantuml.py +++ b/src/rebdhuhn/plantuml.py @@ -7,7 +7,7 @@ from networkx import DiGraph # type:ignore[import-untyped] from rebdhuhn.graph_utils import COMMON_ANCESTOR_FIELD, _get_yes_no_edges, _mark_last_common_ancestors -from rebdhuhn.kroki import Kroki, PlantUmlToSvgConverter +from rebdhuhn.kroki import PlantUmlToSvgConverter from rebdhuhn.models import DecisionNode, EbdGraph, EndNode, OutcomeNode from rebdhuhn.models.errors import GraphTooComplexForPlantumlError, NotExactlyTwoOutgoingEdgesError @@ -189,10 +189,8 @@ def convert_graph_to_plantuml(graph: EbdGraph) -> str: return plantuml_code + "\n@enduml\n" -def convert_plantuml_to_svg_kroki(plantuml_code: str, converter: PlantUmlToSvgConverter | None = None) -> str: +def convert_plantuml_to_svg_kroki(plantuml_code: str, converter: PlantUmlToSvgConverter) -> str: """ Converts plantuml code to svg code using kroki """ - if converter is None: - converter = Kroki() # with its default address (e.g. localhost:8125) return converter.convert_plantuml_to_svg(plantuml_code) diff --git a/unittests/test_table_to_graph.py b/unittests/test_table_to_graph.py index e88ae72..62af2ec 100644 --- a/unittests/test_table_to_graph.py +++ b/unittests/test_table_to_graph.py @@ -160,7 +160,7 @@ def test_table_to_digraph(self, table: EbdTable, expected_description: str): Path(__file__).parent / "output" / f"{ebd_graph.metadata.ebd_code}.puml", "w+", encoding="utf-8" ) as uml_file: uml_file.write(plantuml_code) - svg_code = convert_plantuml_to_svg_kroki(plantuml_code) # Raises an error if conversion fails + svg_code = convert_plantuml_to_svg_kroki(plantuml_code, Kroki()) # Raises an error if conversion fails os.makedirs(Path(__file__).parent / "output", exist_ok=True) with open( Path(__file__).parent / "output" / f"{ebd_graph.metadata.ebd_code}.puml.svg", "w+", encoding="utf-8" @@ -298,7 +298,7 @@ def create_and_save_watermark_and_background_svg(add_background: bool) -> str: svg_file.write(svg_code) svg_code_with_watermark = convert_dot_to_svg_kroki( - dot_code, add_watermark=True, add_background=add_background + dot_code, add_watermark=True, add_background=add_background, dot_to_svg_converter=kroki_client ) # Raises an error if conversion fails file_path2 = ( @@ -392,7 +392,7 @@ def test_table_to_digraph_dot_with_background(self, requests_mock) -> None: svg_file.write(svg_code) svg_code = convert_dot_to_svg_kroki( - dot_code, add_watermark=False, add_background=True + dot_code, add_watermark=False, add_background=True, dot_to_svg_converter=Kroki() ) # Raises an error if conversion fails file_path2 = Path(__file__).parent / "output" / f"{ebd_graph.metadata.ebd_code}_with_background.dot.svg"