Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful handling of missing nodes and division by zero #131

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/converter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sketchformat.layer_common import *
from sketchformat.layer_shape import *
from sketchformat.style import *
from .errors import *
from typing import TypedDict

from . import positioning, style, prototype, utils
Expand Down
6 changes: 5 additions & 1 deletion src/converter/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from urllib.error import HTTPError
from . import component, page, font
from .errors import Fig2SketchWarning
from sketchformat.document import Swatch
from typing import Sequence, Tuple, Optional, Dict, IO, List

Expand Down Expand Up @@ -59,7 +60,10 @@ def add_symbol(self, sketch_symbol):
self._position_symbol(sketch_symbol)

def fig_node(self, fid: Sequence[int]) -> dict:
return self._node_by_id[fid]
if fid in self._node_by_id:
return self._node_by_id[fid]
else:
raise Fig2SketchWarning("NOD001")

def record_font(self, fig_font_name):
font_descriptor = (fig_font_name["family"], fig_font_name["style"])
Expand Down
21 changes: 13 additions & 8 deletions src/converter/positioning.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
from .utils import safe_div
from .errors import Fig2SketchWarning
from sketchformat.layer_common import Rect, AbstractLayer
from typing import TypedDict, Tuple, List, Sequence
Expand Down Expand Up @@ -42,16 +43,20 @@ def inv(self):
return Matrix(
[
[
self[1][1] / (self[0][0] * self[1][1] - self[0][1] * self[1][0]),
self[0][1] / (self[0][1] * self[1][0] - self[0][0] * self[1][1]),
(self[0][2] * self[1][1] - self[0][1] * self[1][2])
/ (self[0][1] * self[1][0] - self[0][0] * self[1][1]),
safe_div(self[1][1], (self[0][0] * self[1][1] - self[0][1] * self[1][0])),
safe_div(self[0][1], (self[0][1] * self[1][0] - self[0][0] * self[1][1])),
safe_div(
(self[0][2] * self[1][1] - self[0][1] * self[1][2]),
(self[0][1] * self[1][0] - self[0][0] * self[1][1]),
),
],
[
self[1][0] / (self[0][1] * self[1][0] - self[0][0] * self[1][1]),
self[0][0] / (self[0][0] * self[1][1] - self[0][1] * self[1][0]),
(self[0][2] * self[1][0] - self[0][0] * self[1][2])
/ (self[0][0] * self[1][1] - self[0][1] * self[1][0]),
safe_div(self[1][0], (self[0][1] * self[1][0] - self[0][0] * self[1][1])),
safe_div(self[0][0], (self[0][0] * self[1][1] - self[0][1] * self[1][0])),
safe_div(
(self[0][2] * self[1][0] - self[0][0] * self[1][2]),
(self[0][0] * self[1][1] - self[0][1] * self[1][0]),
),
],
[0, 0, 1],
]
Expand Down
2 changes: 1 addition & 1 deletion src/converter/prototype.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .context import context
from .errors import Fig2SketchWarning
from .errors import *
from converter import utils
from sketchformat.prototype import *
from typing import TypedDict, Tuple, Optional
Expand Down
6 changes: 4 additions & 2 deletions src/converter/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .positioning import Vector, Matrix
from converter import utils
from sketchformat.style import *
from .utils import safe_div
from typing import List, TypedDict
from .errors import Fig2SketchNodeChanged

Expand Down Expand Up @@ -257,8 +258,9 @@ def convert_gradient(fig_node: dict, fig_fill: dict) -> Gradient:
except:
x_scale = 1

ellipse_ratio = scaled_distance(point_from, point_ellipse, x_scale) / scaled_distance(
point_from, point_to, x_scale
ellipse_ratio = safe_div(
scaled_distance(point_from, point_ellipse, x_scale),
scaled_distance(point_from, point_to, x_scale),
)

return Gradient.Radial(
Expand Down
7 changes: 7 additions & 0 deletions src/converter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
issued_warnings: Dict[tuple[int, int], list[str]] = {}


def safe_div(x, y) -> float:

Check failure on line 11 in src/converter/utils.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a type annotation for one or more arguments [no-untyped-def]
if y == 0:
return 0
return x / y


def gen_object_id(fig_id: Sequence[int], suffix: bytes = b"") -> str:
# Generate UUIDs by hashing the fig GUID with a salt
salted_id = config.salt + struct.pack("<" + "I" * len(fig_id), *fig_id) + suffix
Expand Down Expand Up @@ -98,6 +104,7 @@
"IMG003": "is missing from the .fig file, it will not be converted",
"IMG004": "appears to be corrupted in the .fig file ({error}), it will not be converted",
"LAY001": "is an unsupported layer type, it will not be converted",
"NOD001": "node could not be found",
}


Expand Down
Loading