Skip to content

Commit

Permalink
Add bool_subelement helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 19, 2023
1 parent 61607a2 commit 81c6366
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
30 changes: 5 additions & 25 deletions fastkml/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from fastkml.geometry import Point
from fastkml.geometry import Polygon
from fastkml.geometry import create_kml_geometry
from fastkml.helpers import bool_subelement
from fastkml.helpers import simple_text_subelement
from fastkml.links import Link
from fastkml.mixins import TimeMixin
Expand Down Expand Up @@ -319,19 +320,8 @@ def etree_element(
simple_text_subelement(self, element, "phone_number", "phoneNumber")
simple_text_subelement(self, element, "description", "description")
simple_text_subelement(self, element, "name", "name")

if self.visibility is not None:
visibility = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}visibility",
)
visibility.text = str(int(self.visibility))
if self.isopen:
isopen = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}open",
)
isopen.text = str(int(self.isopen))
bool_subelement(self, element, "visibility", "visibility")
bool_subelement(self, element, "isopen", "open")
return element

@classmethod
Expand Down Expand Up @@ -740,19 +730,9 @@ def etree_element(
verbosity: Verbosity = Verbosity.normal,
) -> Element:
element = super().etree_element(precision=precision, verbosity=verbosity)
if self.refresh_visibility is not None:
refresh_visibility = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}refreshVisibility",
)
refresh_visibility.text = str(int(self.refresh_visibility))
bool_subelement(self, element, "refresh_visibility", "refreshVisibility")
bool_subelement(self, element, "fly_to_view", "flyToView")

if self.fly_to_view is not None:
fly_to_view = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{self.ns}flyToView",
)
fly_to_view.text = str(int(self.fly_to_view))
if self.link is not None:
element.append(self.link.etree_element())
return element
Expand Down
15 changes: 15 additions & 0 deletions fastkml/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@ def simple_text_subelement(
f"{obj.ns}{node_name}",
)
subelement.text = getattr(obj, attr_name)


def bool_subelement(
obj: _BaseObject,
element: Element,
attr_name: str,
node_name: str,
) -> None:
"""Set the value of an attribute from a subelement with a text node."""
if getattr(obj, attr_name, None):
subelement = config.etree.SubElement( # type: ignore[attr-defined]
element,
f"{obj.ns}{node_name}",
)
subelement.text = str(int(getattr(obj, attr_name)))

0 comments on commit 81c6366

Please sign in to comment.