Skip to content

Commit

Permalink
typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hbmartin committed Jul 7, 2024
1 parent 97c64ec commit 518ee7a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 47 deletions.
17 changes: 0 additions & 17 deletions .deepsource.toml

This file was deleted.

7 changes: 7 additions & 0 deletions graphviz2drawio/models/Errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ class MissingTitleError(GdValueError):
def __init__(self, g) -> None:
super().__init__("Title missing from SVG element.")
self.g = g


class InvalidBezierParameterError(GdValueError):
"""Invalid Bezier parameter, must be 0 <= t <= 1."""

def __init__(self, t) -> None:
super().__init__(f"Invalid Bezier parameter (t={t}), must be 0 <= t <= 1.")
20 changes: 10 additions & 10 deletions graphviz2drawio/mx/Edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .Curve import Curve
from .GraphObj import GraphObj
from .Text import Text


class Edge(GraphObj):
Expand All @@ -13,7 +14,7 @@ def __init__(
fr: str,
to: str,
curve: Curve | None,
labels: str,
labels: list[Text],
) -> None:
super().__init__(sid=sid, gid=f"{fr}->{to}")
self.fr = fr
Expand All @@ -30,14 +31,14 @@ def curve_start_end(self):
return self.curve.start, self.curve.end

def text_to_mx_value(self):
value = ""
last_text = len(self.labels) - 1
for i, t in enumerate(self.labels):
style = t.get_mx_style()
value += "<p style='" + style + "'>" + t.text + "</p>"
if i != last_text:
value += "<hr size='1'/>"
return value
value = ""
last_text = len(self.labels) - 1
for i, t in enumerate(self.labels):
style = t.get_mx_style()
value += "<p style='" + style + "'>" + t.text + "</p>"
if i != last_text:
value += "<hr size='1'/>"
return value

@property
def key_for_label(self) -> str:
Expand All @@ -48,4 +49,3 @@ def __repr__(self) -> str:
f"{self.fr}->{self.to}: "
f"{self.labels}, {self.line_style}, {self.dir}, {self.arrowtail}"
)

33 changes: 17 additions & 16 deletions graphviz2drawio/mx/EdgeFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ def __init__(self, coords) -> None:
super().__init__()
self.curve_factory = CurveFactory(coords)

def _get_labels(g):
texts = []
current_text = None
for t in g:
if SVG.is_tag(t, "text"):
if current_text is None:
current_text = Text.from_svg(t)
else:
current_text.text += "<br/>" + t.text
elif current_text is not None:
texts.append(current_text)
current_text = None
if current_text is not None:
texts.append(current_text)
return texts
@staticmethod
def _get_labels(g) -> list[Text]:
texts = []
current_text = None
for t in g:
if SVG.is_tag(t, "text"):
if current_text is None:
current_text = Text.from_svg(t)
else:
current_text.text += "<br/>" + t.text
elif current_text is not None:
texts.append(current_text)
current_text = None
if current_text is not None:
texts.append(current_text)
return texts

def from_svg(self, g) -> Edge:
title = SVG.get_title(g)
Expand All @@ -35,7 +36,7 @@ def from_svg(self, g) -> Edge:
fr = fr.split(":")[0]
to = to.split(":")[0]
curve = None
labels = _get_labels(g)
labels = self._get_labels(g)
if (path := SVG.get_first(g, "path")) is not None:
if "d" in path.attrib:
curve = self.curve_factory.from_svg(path.attrib["d"])
Expand Down
11 changes: 8 additions & 3 deletions graphviz2drawio/mx/bezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
# See also https://pomax.github.io/bezierinfo/

import math
from typing import TypeAlias

from graphviz2drawio.models.Errors import InvalidBezierParameterError

EPSILON: float = 1e-03
Cubic: TypeAlias = tuple[complex, complex, complex, complex]
Quadratic: TypeAlias = tuple[complex, complex, complex]


def controlpoints_at(
Expand Down Expand Up @@ -44,7 +49,7 @@ def subdivide(
c2: complex,
p2: complex,
t: float,
) -> tuple[tuple, tuple | None]:
) -> tuple[Cubic, Cubic]:
"""Subdivide this curve at the point on the curve at `t`.
Split curve into two cubic Bézier curves, where 0<t<1.
Expand All @@ -56,7 +61,7 @@ def subdivide(
"""
if t < 0 or t > 1:
return (p1, c1, c2, p2), None
raise InvalidBezierParameterError(t)
cp0, cp1, p, cp2, cp3 = controlpoints_at(p1, c1, c2, p2, t)
curve1 = (p1, cp0, cp1, p)
curve2 = (p, cp2, cp3, p2)
Expand Down Expand Up @@ -179,7 +184,7 @@ def approximate_cubic_bezier_as_quadratic(
c1: complex,
c2: complex,
p2: complex,
) -> tuple[complex, complex, complex]:
) -> Quadratic:
"""Approximates a cubic Bézier as a quadratic using tangent intersection."""
# Calculate tangent vectors at start and end points
tan_start = 3.0 * (c1 - p0)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ indent-width = 4

lint.select = ["ALL"]
lint.ignore = ["ANN001", "ANN003", "ANN101", "ANN201", "ANN202", "ANN205", "D100", "D101", "D102", "D103", "D104", "D105", "D107", "D203", "D213", "PLR0913", "S314", "SIM102", "TD002", "TID252", "N999"]
target-version = "py312"
target-version = "py310"

[tool.ruff.format]
# Like Black, use double quotes for strings.
Expand Down

0 comments on commit 518ee7a

Please sign in to comment.