Skip to content

Commit

Permalink
Refactor XML attribute handling in overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 21, 2023
1 parent b0605e0 commit 9155913
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 293 deletions.
38 changes: 37 additions & 1 deletion fastkml/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ def bool_subelement(
subelement.text = str(int(getattr(obj, attr_name)))


def int_subelement(
obj: _XMLObject,
*,
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(getattr(obj, attr_name))


def float_subelement(
obj: _XMLObject,
*,
Expand Down Expand Up @@ -141,13 +157,33 @@ def subelement_bool_kwarg(
return {}


def subelement_int_kwarg(
*,
element: Element,
ns: str,
node_name: str,
kwarg: str,
strict: bool,
) -> Dict[str, int]:
node = element.find(f"{ns}{node_name}")
if node is None:
return {}
if node.text and node.text.strip():
try:
return {kwarg: int(node.text.strip())}
except ValueError:
if strict:
raise
return {}
return {}


def subelement_float_kwarg(
*,
element: Element,
ns: str,
node_name: str,
kwarg: str,
precision: Optional[int],
strict: bool,
) -> Dict[str, float]:
node = element.find(f"{ns}{node_name}")
Expand Down
3 changes: 0 additions & 3 deletions fastkml/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def _get_kwargs(
ns=ns,
node_name="refreshInterval",
kwarg="refresh_interval",
precision=None,
strict=strict,
),
)
Expand All @@ -211,7 +210,6 @@ def _get_kwargs(
ns=ns,
node_name="viewRefreshTime",
kwarg="view_refresh_time",
precision=None,
strict=strict,
),
)
Expand All @@ -221,7 +219,6 @@ def _get_kwargs(
ns=ns,
node_name="viewBoundScale",
kwarg="view_bound_scale",
precision=None,
strict=strict,
),
)
Expand Down

0 comments on commit 9155913

Please sign in to comment.