From 8c8d92d1ccc8189e52d13d202c6a4b71a2868327 Mon Sep 17 00:00:00 2001 From: Christian Ledermann Date: Tue, 12 Dec 2023 18:01:21 +0000 Subject: [PATCH] Refactor feature parsing in containers.py and kml.py --- fastkml/containers.py | 44 ++++++++++-------------------- fastkml/gx.py | 2 +- fastkml/kml.py | 63 +++++++++---------------------------------- 3 files changed, 27 insertions(+), 82 deletions(-) diff --git a/fastkml/containers.py b/fastkml/containers.py index 72d2cb4a..4c695618 100644 --- a/fastkml/containers.py +++ b/fastkml/containers.py @@ -143,36 +143,20 @@ def _get_kwargs( strict=strict, ) kwargs["features"] = [] - folders = element.findall(f"{ns}Folder") - kwargs["features"] += [ - Folder.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=folder, - strict=strict, - ) - for folder in folders - ] - placemarks = element.findall(f"{ns}Placemark") - kwargs["features"] += [ - Placemark.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=placemark, - strict=strict, - ) - for placemark in placemarks - ] - documents = element.findall(f"{ns}Document") - kwargs["features"] += [ - Document.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=document, - strict=strict, - ) - for document in documents - ] + for klass in ( + Folder, + Placemark, + Document, + ): + for feature in element.findall(f"{ns}{klass.__name__}"): + kwargs["features"].append( + klass.class_from_element( # type: ignore[attr-defined] + ns=ns, + name_spaces=name_spaces, + element=feature, + strict=strict, + ), + ) return kwargs diff --git a/fastkml/gx.py b/fastkml/gx.py index 18cc2516..391475e0 100644 --- a/fastkml/gx.py +++ b/fastkml/gx.py @@ -126,7 +126,7 @@ class Angle: roll: float = 0.0 -@dataclass(frozen=True) +@dataclass(frozen=True) # XXX replace with _XMLObject class TrackItem: """A track item describes an objects position and heading at a specific time.""" diff --git a/fastkml/kml.py b/fastkml/kml.py index d421f4c3..c7a347e3 100644 --- a/fastkml/kml.py +++ b/fastkml/kml.py @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2022 Christian Ledermann +# Copyright (C) 2012-2023 Christian Ledermann # # This library is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by the Free @@ -122,56 +122,17 @@ def _get_kwargs( strict=strict, ) kwargs["features"] = [] - documents = element.findall(f"{ns}Document") - for document in documents: - kwargs["features"].append( - Document.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=document, - strict=False, - ), - ) - folders = element.findall(f"{ns}Folder") - for folder in folders: - kwargs["features"].append( - Folder.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=folder, - strict=False, - ), - ) - placemarks = element.findall(f"{ns}Placemark") - for placemark in placemarks: - kwargs["features"].append( - Placemark.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=placemark, - strict=False, - ), - ) - groundoverlays = element.findall(f"{ns}GroundOverlay") - for groundoverlay in groundoverlays: - kwargs["features"].append( - GroundOverlay.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=groundoverlay, - strict=False, - ), - ) - photo_overlays = element.findall(f"{ns}PhotoOverlay") - for photo_overlay in photo_overlays: - kwargs["features"].append( - PhotoOverlay.class_from_element( - ns=ns, - name_spaces=name_spaces, - element=photo_overlay, - strict=False, - ), - ) + for klass in (Document, Folder, Placemark, GroundOverlay, PhotoOverlay): + for feature in element.findall(f"{ns}{klass.__name__}"): + kwargs["features"].append( + klass.class_from_element( # type: ignore[attr-defined] + ns=ns, + name_spaces=name_spaces, + element=feature, + strict=strict, + ), + ) + return kwargs @classmethod