Skip to content

Commit b3e2b79

Browse files
committed
Add another simplification and reuse mapping for geometries
1 parent ba06263 commit b3e2b79

File tree

2 files changed

+36
-98
lines changed

2 files changed

+36
-98
lines changed

pygdtf/__init__.py

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -268,78 +268,12 @@ def _read_xml(self):
268268
self.geometries = Geometries()
269269
geometry_collect = self._root.find("Geometries")
270270
if geometry_collect is not None:
271-
for i in geometry_collect.findall("Geometry"):
272-
self.geometries.append(
273-
Geometry(xml_node=i, xml_parent=geometry_collect)
274-
)
275-
for i in geometry_collect.findall("Axis"):
276-
self.geometries.append(
277-
GeometryAxis(xml_node=i, xml_parent=geometry_collect)
278-
)
279-
for i in geometry_collect.findall("FilterBeam"):
280-
self.geometries.append(
281-
GeometryFilterBeam(xml_node=i, xml_parent=geometry_collect)
282-
)
283-
for i in geometry_collect.findall("FilterColor"):
284-
self.geometries.append(
285-
GeometryFilterColor(xml_node=i, xml_parent=geometry_collect)
286-
)
287-
for i in geometry_collect.findall("FilterGobo"):
288-
self.geometries.append(
289-
GeometryFilterGobo(xml_node=i, xml_parent=geometry_collect)
290-
)
291-
for i in geometry_collect.findall("FilterShaper"):
292-
self.geometries.append(
293-
GeometryFilterShaper(xml_node=i, xml_parent=geometry_collect)
294-
)
295-
for i in geometry_collect.findall("MediaServerMaster"):
296-
self.geometries.append(
297-
GeometryMediaServerMaster(xml_node=i, xml_parent=geometry_collect)
298-
)
299-
for i in geometry_collect.findall("MediaServerLayer"):
300-
self.geometries.append(
301-
GeometryMediaServerLayer(xml_node=i, xml_parent=geometry_collect)
302-
)
303-
for i in geometry_collect.findall("MediaServerCamera"):
304-
self.geometries.append(
305-
GeometryMediaServerCamera(xml_node=i, xml_parent=geometry_collect)
306-
)
307-
for i in geometry_collect.findall("Inventory"):
308-
self.geometries.append(
309-
GeometryInventory(xml_node=i, xml_parent=geometry_collect)
310-
)
311-
for i in geometry_collect.findall("Beam"):
312-
self.geometries.append(
313-
GeometryBeam(xml_node=i, xml_parent=geometry_collect)
314-
)
315-
for i in geometry_collect.findall("WiringObject"):
316-
self.geometries.append(
317-
GeometryWiringObject(xml_node=i, xml_parent=geometry_collect)
318-
)
319-
for i in geometry_collect.findall("GeometryReference"):
320-
self.geometries.append(
321-
GeometryReference(xml_node=i, xml_parent=geometry_collect)
322-
)
323-
for i in geometry_collect.findall("Laser"):
324-
self.geometries.append(
325-
GeometryLaser(xml_node=i, xml_parent=geometry_collect)
326-
)
327-
for i in geometry_collect.findall("Support"):
328-
self.geometries.append(
329-
GeometrySupport(xml_node=i, xml_parent=geometry_collect)
330-
)
331-
for i in geometry_collect.findall("Structure"):
332-
self.geometries.append(
333-
GeometryStructure(xml_node=i, xml_parent=geometry_collect)
334-
)
335-
for i in geometry_collect.findall("Display"):
336-
self.geometries.append(
337-
GeometryDisplay(xml_node=i, xml_parent=geometry_collect)
338-
)
339-
for i in geometry_collect.findall("Magnet"):
340-
self.geometries.append(
341-
GeometryMagnet(xml_node=i, xml_parent=geometry_collect)
342-
)
271+
for geometry_node in list(geometry_collect):
272+
cls = TAG_TO_GEOMETRY_CLASS.get(geometry_node.tag)
273+
if cls:
274+
self.geometries.append(
275+
cls(xml_node=geometry_node, xml_parent=geometry_collect)
276+
)
343277

344278
dmx_mode_collect = self._root.find("DMXModes")
345279
if dmx_mode_collect is not None:

pygdtf/geometries.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,21 @@ def _read_xml(self, xml_node: "Element", xml_parent: Optional["Element"] = None)
113113

114114
self.model = xml_node.attrib.get("Model")
115115
self.position = Matrix(xml_node.attrib.get("Position", 0))
116-
tag_to_class = {
117-
"Geometry": Geometry,
118-
"Axis": GeometryAxis,
119-
"FilterBeam": GeometryFilterBeam,
120-
"FilterColor": GeometryFilterColor,
121-
"FilterGobo": GeometryFilterGobo,
122-
"FilterShaper": GeometryFilterShaper,
123-
"MediaServerMaster": GeometryMediaServerMaster,
124-
"MediaServerLayer": GeometryMediaServerLayer,
125-
"MediaServerCamera": GeometryMediaServerCamera,
126-
"Inventory": GeometryInventory,
127-
"Beam": GeometryBeam,
128-
"WiringObject": GeometryWiringObject,
129-
"GeometryReference": GeometryReference,
130-
"Laser": GeometryLaser,
131-
"Structure": GeometryStructure,
132-
"Support": GeometrySupport,
133-
"Magnet": GeometryMagnet,
134-
"Display": GeometryDisplay,
135-
}
136116
for child in list(xml_node):
137-
cls = tag_to_class.get(child.tag)
117+
cls = TAG_TO_GEOMETRY_CLASS.get(child.tag)
138118
if cls:
139119
self.geometries.append(cls(xml_node=child, xml_parent=xml_node))
140120

141121
def __str__(self):
142122
return f"{self.name} ({self.model})"
143123

144124
def _tag(self):
145-
if type(self) is Geometry:
146-
return "Geometry"
147-
if self.__class__.__name__.startswith("Geometry"):
148-
return self.__class__.__name__.replace("Geometry", "", 1)
149-
return type(self).__name__
125+
tag = CLASS_TO_GEOMETRY_TAG.get(type(self))
126+
if tag:
127+
return tag
128+
raise KeyError(
129+
f"Geometry class not registered in mapping: {type(self).__name__}"
130+
)
150131

151132
def to_xml(self):
152133
attrs = {}
@@ -572,3 +553,26 @@ def _read_xml(self, xml_node: "Element", xml_parent: Optional["Element"] = None)
572553

573554
def __str__(self):
574555
return f"{self.name} ({self.model})"
556+
557+
558+
TAG_TO_GEOMETRY_CLASS = {
559+
"Geometry": Geometry,
560+
"Axis": GeometryAxis,
561+
"FilterBeam": GeometryFilterBeam,
562+
"FilterColor": GeometryFilterColor,
563+
"FilterGobo": GeometryFilterGobo,
564+
"FilterShaper": GeometryFilterShaper,
565+
"MediaServerMaster": GeometryMediaServerMaster,
566+
"MediaServerLayer": GeometryMediaServerLayer,
567+
"MediaServerCamera": GeometryMediaServerCamera,
568+
"Inventory": GeometryInventory,
569+
"Beam": GeometryBeam,
570+
"WiringObject": GeometryWiringObject,
571+
"GeometryReference": GeometryReference,
572+
"Laser": GeometryLaser,
573+
"Structure": GeometryStructure,
574+
"Support": GeometrySupport,
575+
"Magnet": GeometryMagnet,
576+
"Display": GeometryDisplay,
577+
}
578+
CLASS_TO_GEOMETRY_TAG = {cls: tag for tag, cls in TAG_TO_GEOMETRY_CLASS.items()}

0 commit comments

Comments
 (0)