Coverage for src/cell_abm_pipeline/tasks/build_svg_image.py: 0%
19 statements
« prev ^ index » next coverage.py v7.1.0, created at 2024-06-05 19:14 +0000
« prev ^ index » next coverage.py v7.1.0, created at 2024-06-05 19:14 +0000
1import xml.etree.ElementTree as ET
3from prefect import task
5PATH_ATTRIBUTES: list[str] = ["fill", "stroke", "stroke-width", "stroke-dasharray"]
8@task
9def build_svg_image(
10 elements: list[dict], width: int, height: int, rotate: float, scale: float
11) -> str:
12 root = ET.fromstring("<svg></svg>")
13 root.set("xmlns", "http://www.w3.org/2000/svg")
14 root.set("width", str(width))
15 root.set("height", str(height))
17 for element in elements:
18 path = ET.fromstring("<path></path>")
20 cx = width / 2
21 cy = height / 2
23 path.set("d", "M" + "L".join([f"{x},{y}" for x, y in element["points"]]))
25 for attribute in PATH_ATTRIBUTES:
26 path.set(attribute, str(element[attribute]) if attribute in element else "none")
28 path.set("transform", f"rotate({rotate},{cx},{cy}) translate({cx},{cy}) scale({scale})")
29 root.insert(0, path)
31 return ET.tostring(root, encoding="unicode")