Skip to content

Commit

Permalink
Refactor KmlDateTime and TimeSpan classes, and AbstractView class
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Dec 20, 2023
1 parent 7c4e36a commit d3ed751
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
43 changes: 20 additions & 23 deletions fastkml/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from fastkml.helpers import xml_subelement
from fastkml.helpers import xml_subelement_kwarg
from fastkml.helpers import xml_subelement_list
from fastkml.helpers import xml_subelement_list_kwarg
from fastkml.links import Link
from fastkml.mixins import TimeMixin
from fastkml.styles import Style
Expand Down Expand Up @@ -356,29 +357,25 @@ def _get_kwargs(
)
name_spaces = kwargs["name_spaces"]
assert name_spaces is not None
styles = element.findall(f"{ns}Style")
kwargs["styles"] = []
if styles is not None:
for style in styles:
kwargs["styles"].append(
Style.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=style,
strict=strict,
),
)
stylemaps = element.findall(f"{ns}StyleMap")
if stylemaps is not None:
for stylemap in stylemaps:
kwargs["styles"].append(
StyleMap.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=stylemap,
strict=strict,
),
)
kwargs["styles"] = xml_subelement_list_kwarg(
element=element,
ns=ns,
name_spaces=name_spaces,
kwarg="styles",
obj_class=Style,
strict=strict,
).get("styles", [])
kwargs["styles"].extend(
xml_subelement_list_kwarg(
element=element,
ns=ns,
name_spaces=name_spaces,
kwarg="styles",
obj_class=StyleMap,
strict=strict,
).get("styles", []),
)

kwargs.update(
xml_subelement_kwarg(
element=element,
Expand Down
26 changes: 26 additions & 0 deletions fastkml/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Helper functions for fastkml."""

from typing import Dict
from typing import List
from typing import Optional
from typing import Type

Expand Down Expand Up @@ -128,3 +129,28 @@ def xml_subelement_kwarg(
strict=strict,
),
}


def xml_subelement_list_kwarg(
*,
element: Element,
ns: str,
name_spaces: Dict[str, str],
kwarg: str,
obj_class: Type[_XMLObject],
strict: bool,
) -> Dict[str, List[_XMLObject]]:
if subelements := element.findall(f"{ns}{obj_class.get_tag_name()}"):
return {
kwarg: [
obj_class.class_from_element(
ns=ns,
name_spaces=name_spaces,
element=subelement,
strict=strict,
)
for subelement in subelements
],
}
else:
return {}

0 comments on commit d3ed751

Please sign in to comment.