Skip to content

Commit

Permalink
Merge pull request #268 from cleder/261-refactor-_abstractview-camera…
Browse files Browse the repository at this point in the history
…-and-lookat

Refactor views.py to use classmethod for parsing XML elements
  • Loading branch information
cleder authored Nov 11, 2023
2 parents 58041e9 + 178ae5d commit e065244
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 62 deletions.
157 changes: 100 additions & 57 deletions fastkml/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Any
from typing import Dict
from typing import Optional
from typing import SupportsFloat
Expand Down Expand Up @@ -162,51 +163,6 @@ def altitude_mode(self) -> AltitudeMode:
def altitude_mode(self, mode: AltitudeMode) -> None:
self._altitude_mode = mode

def from_element(self, element: Element) -> None:
super().from_element(element)
longitude = element.find(f"{self.ns}longitude")
if longitude is not None:
self.longitude = float(longitude.text)
latitude = element.find(f"{self.ns}latitude")
if latitude is not None:
self.latitude = float(latitude.text)
altitude = element.find(f"{self.ns}altitude")
if altitude is not None:
self.altitude = float(altitude.text)
heading = element.find(f"{self.ns}heading")
if heading is not None:
self.heading = float(heading.text)
tilt = element.find(f"{self.ns}tilt")
if tilt is not None:
self.tilt = float(tilt.text)
altitude_mode = element.find(f"{self.ns}altitudeMode")
if altitude_mode is None:
altitude_mode = element.find(f"{self.name_spaces['gx']}altitudeMode")
if altitude_mode is not None:
self.altitude_mode = AltitudeMode(altitude_mode.text)
timespan = element.find(f"{self.ns}TimeSpan")
if timespan is not None:
self._timespan = cast(
TimeSpan,
TimeSpan.class_from_element(
ns=self.ns,
name_spaces=self.name_spaces,
element=timespan,
strict=False,
),
)
timestamp = element.find(f"{self.ns}TimeStamp")
if timestamp is not None:
self._timestamp = cast(
TimeStamp,
TimeStamp.class_from_element(
ns=self.ns,
name_spaces=self.name_spaces,
element=timestamp,
strict=False,
),
)

def etree_element(
self,
precision: Optional[int] = None,
Expand Down Expand Up @@ -273,6 +229,65 @@ def etree_element(
# TODO: <gx:ViewerOptions>
# TODO: <gx:horizFov>

@classmethod
def _get_kwargs(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> Dict[str, Any]:
kwargs = super()._get_kwargs(
ns=ns,
name_spaces=name_spaces,
element=element,
strict=strict,
)
longitude = element.find(f"{ns}longitude")
if longitude is not None:
kwargs["longitude"] = float(longitude.text)
latitude = element.find(f"{ns}latitude")
if latitude is not None:
kwargs["latitude"] = float(latitude.text)
altitude = element.find(f"{ns}altitude")
if altitude is not None:
kwargs["altitude"] = float(altitude.text)
heading = element.find(f"{ns}heading")
if heading is not None:
kwargs["heading"] = float(heading.text)
tilt = element.find(f"{ns}tilt")
if tilt is not None:
kwargs["tilt"] = float(tilt.text)
altitude_mode = element.find(f"{ns}altitudeMode")
if altitude_mode is None:
altitude_mode = element.find(f"{kwargs['name_spaces']['gx']}altitudeMode")
if altitude_mode is not None:
kwargs["altitude_mode"] = AltitudeMode(altitude_mode.text)
timespan = element.find(f"{ns}TimeSpan")
if timespan is not None:
kwargs["time_primitive"] = cast(
TimeSpan,
TimeSpan.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=timespan,
strict=strict,
),
)
timestamp = element.find(f"{ns}TimeStamp")
if timestamp is not None:
kwargs["time_primitive"] = cast(
TimeStamp,
TimeStamp.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=timestamp,
strict=strict,
),
)
return kwargs


class Camera(_AbstractView):
"""
Expand Down Expand Up @@ -332,12 +347,6 @@ def __init__(
)
self._roll = roll

def from_element(self, element: Element) -> None:
super().from_element(element)
roll = element.find(f"{self.ns}roll")
if roll is not None:
self.roll = float(roll.text)

def etree_element(
self,
precision: Optional[int] = None,
Expand All @@ -360,6 +369,26 @@ def roll(self) -> Optional[float]:
def roll(self, value: float) -> None:
self._roll = value

@classmethod
def _get_kwargs(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> Dict[str, Any]:
kwargs = super()._get_kwargs(
ns=ns,
name_spaces=name_spaces,
element=element,
strict=strict,
)
roll = element.find(f"{ns}roll")
if roll is not None:
kwargs["roll"] = float(roll.text)
return kwargs


class LookAt(_AbstractView):
__name__ = "LookAt"
Expand Down Expand Up @@ -406,12 +435,6 @@ def range(self) -> Optional[float]:
def range(self, value: float) -> None:
self._range = value

def from_element(self, element: Element) -> None:
super().from_element(element)
range_var = element.find(f"{self.ns}range")
if range_var is not None:
self.range = float(range_var.text)

def etree_element(
self,
precision: Optional[int] = None,
Expand All @@ -426,6 +449,26 @@ def etree_element(
range_var.text = str(self._range)
return element

@classmethod
def _get_kwargs(
cls,
*,
ns: str,
name_spaces: Optional[Dict[str, str]] = None,
element: Element,
strict: bool,
) -> Dict[str, Any]:
kwargs = super()._get_kwargs(
ns=ns,
name_spaces=name_spaces,
element=element,
strict=strict,
)
range_var = element.find(f"{ns}range")
if range_var is not None:
kwargs["range"] = float(range_var.text)
return kwargs


__all__ = [
"Camera",
Expand Down
7 changes: 2 additions & 5 deletions tests/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ def test_camera_read(self) -> None:
"<kml:altitudeMode>relativeToGround</kml:altitudeMode>"
"</kml:Camera>"
)
camera = views.Camera()

camera.from_string(camera_xml)
camera = views.Camera.class_from_string(camera_xml)

assert camera.heading == 10
assert camera.tilt == 20
Expand Down Expand Up @@ -157,9 +156,7 @@ def test_look_at_read(self) -> None:
"<kml:range>30</kml:range>"
"</kml:LookAt>"
)
look_at = views.LookAt()

look_at.from_string(look_at_xml)
look_at = views.LookAt.class_from_string(look_at_xml)

assert look_at.heading == 10
assert look_at.tilt == 20
Expand Down

0 comments on commit e065244

Please sign in to comment.