Skip to content

Commit

Permalink
fix: make kroki client (and port) configurable (#281)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
hf-kklein and Konstantin authored Oct 29, 2024
1 parent 1941b37 commit 8c564b7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
11 changes: 3 additions & 8 deletions src/rebdhuhn/graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -169,19 +169,14 @@ 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.
Optionally add the HF watermark to the svg code, controlled by the argument 'add_watermark'
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)
Expand Down
6 changes: 2 additions & 4 deletions src/rebdhuhn/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions unittests/test_table_to_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 8c564b7

Please sign in to comment.