diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 786c5d54..86c7730e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -74,7 +74,10 @@ repos: - id: rst-directive-colons - id: rst-inline-touching-normal - id: text-unicode-replacement-char - + - repo: https://github.com/rstcheck/rstcheck + rev: "v6.2.0" + hooks: + - id: rstcheck # - repo: https://github.com/mgedmin/check-manifest # rev: "0.49" # hooks: diff --git a/fastkml/styles.py b/fastkml/styles.py index 0409bc4a..8d23edef 100644 --- a/fastkml/styles.py +++ b/fastkml/styles.py @@ -200,6 +200,34 @@ class HotSpot: xunits: Units yunits: Units + @classmethod + def class_from_element( + cls, + *, + ns: str, + name_spaces: Optional[Dict[str, str]] = None, + element: Element, + strict: bool, + ) -> Optional["HotSpot"]: + hot_spot = element.find(f"{ns}hotSpot") + if hot_spot is not None: + x = hot_spot.attrib.get("x") # type: ignore[attr-defined] + y = hot_spot.attrib.get("y") # type: ignore[attr-defined] + xunits = hot_spot.attrib.get("xunits") # type: ignore[attr-defined] + yunits = hot_spot.attrib.get("yunits") # type: ignore[attr-defined] + x = float(x) if x is not None else 0 + y = float(y) if y is not None else 0 + xunits = Units(xunits) if xunits is not None else None + yunits = Units(yunits) if yunits is not None else None + element.remove(hot_spot) + return HotSpot( + x=x, + y=y, + xunits=xunits, + yunits=yunits, + ) + return None + class IconStyle(_ColorStyle): """Specifies how icons for point Placemarks are drawn.""" @@ -306,22 +334,12 @@ def _get_kwargs( href = icon.find(f"{ns}href") if href is not None: kwargs["icon_href"] = href.text - hot_spot = element.find(f"{ns}hotSpot") - if hot_spot is not None: - x = hot_spot.attrib.get("x") # type: ignore[attr-defined] - y = hot_spot.attrib.get("y") # type: ignore[attr-defined] - xunits = hot_spot.attrib.get("xunits") # type: ignore[attr-defined] - yunits = hot_spot.attrib.get("yunits") # type: ignore[attr-defined] - x = float(x) if x is not None else 0 - y = float(y) if y is not None else 0 - xunits = Units(xunits) if xunits is not None else None - yunits = Units(yunits) if yunits is not None else None - kwargs["hot_spot"] = HotSpot( - x=x, - y=y, - xunits=xunits, - yunits=yunits, - ) + kwargs["hot_spot"] = HotSpot.class_from_element( # type: ignore[attr-defined] + ns=ns, + name_spaces=name_spaces, + element=element, + strict=strict, + ) return kwargs diff --git a/fastkml/types.py b/fastkml/types.py index f5cc7b6d..16288e03 100644 --- a/fastkml/types.py +++ b/fastkml/types.py @@ -46,6 +46,9 @@ def findall(self, tag: str) -> Iterable["Element"]: def append(self, element: "Element") -> None: """Append an element to the current element.""" + def remove(self, element: "Element") -> None: + """Remove an element from the current element.""" + class KmlObjectMap(TypedDict): """TypedDict for KmlObjectMap.""" diff --git a/pyproject.toml b/pyproject.toml index b5db983c..8622b116 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -242,7 +242,6 @@ force-single-line = true [tool.setuptools] include-package-data = true -zip-safe = false [tool.setuptools.dynamic.version] attr = "fastkml.about.__version__" diff --git a/tests/styles_test.py b/tests/styles_test.py index b953a2ee..4beb9181 100644 --- a/tests/styles_test.py +++ b/tests/styles_test.py @@ -78,6 +78,7 @@ def test_icon_style_read(self) -> None: "ff2200ffrandom" "520" "http://example.com/icon.png" + '' "", ) @@ -88,6 +89,10 @@ def test_icon_style_read(self) -> None: assert icons.scale == 5.0 assert icons.icon_href == "http://example.com/icon.png" assert icons.heading == 20.0 + assert icons.hot_spot.x == 0.5 + assert icons.hot_spot.y == 0.7 + assert icons.hot_spot.xunits.value == "fraction" + assert icons.hot_spot.yunits.value == "insetPixels" def test_line_style(self) -> None: lines = styles.LineStyle(