Skip to content

Commit 48bb109

Browse files
committed
feat: enhance TaggedObjects schema so that it is easier to subclass
1 parent 531c2bd commit 48bb109

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

gmc/schemas/tagged_objects/__init__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,20 @@ def shape(self):
264264

265265

266266
class TaggedObjects(OneSourceOneDestination, MarkupSchema):
267+
_mapping: ClassVar[dict[str, type[HasTags]]] = {
268+
"quad": CustomQuadrangle,
269+
"line": CustomLine,
270+
"seg": CustomSegment,
271+
"point": CustomPoint,
272+
"rect": CustomRectangle,
273+
"region": CustomRegion,
274+
"path": CustomPath,
275+
}
276+
267277
_cls_to_type: ClassVar[dict[str, str]] = {
268-
"CustomQuadrangle": "quad",
269-
"CustomLine": "line",
270-
"CustomSegment": "seg",
271-
"CustomPoint": "point",
272-
"CustomRectangle": "rect",
273-
"CustomRegion": "region",
274-
"CustomPath": "path",
278+
cls.__name__: name for name, cls in _mapping.items()
275279
}
280+
276281
_current_root_properties: dict[str, Any] | None
277282
_current_properties: dict[str, Any] | None
278283
last_used_default_action: ClassVar[str] = settings.value(
@@ -775,15 +780,6 @@ def open_markup(self, src_data_path: str, dst_markup_path: str) -> None:
775780
self._current_root_properties = None
776781
scene = self._image_widget.scene()
777782
item = None
778-
mapping = {
779-
"quad": CustomQuadrangle,
780-
"line": CustomLine,
781-
"seg": CustomSegment,
782-
"point": CustomPoint,
783-
"rect": CustomRectangle,
784-
"path": CustomPath,
785-
"region": CustomRegion,
786-
}
787783
warnings: list[str] = []
788784

789785
for obj in self._original_markup.get("objects", ()):
@@ -793,10 +789,10 @@ def open_markup(self, src_data_path: str, dst_markup_path: str) -> None:
793789
case _:
794790
warnings.append(f"invalid type for {obj!r}")
795791
continue
796-
if the_type not in mapping:
792+
if the_type not in self._mapping:
797793
warnings.append(f"ignoring unknown object type `{the_type}`")
798794
continue
799-
cls = mapping[the_type]
795+
cls = self._mapping[the_type]
800796
try:
801797
item = cls.from_json(self, rest)
802798
except ValueError as e:

0 commit comments

Comments
 (0)