Skip to content

Commit

Permalink
Add rstcheck hook to pre-commit-config.yaml and update HotSpot class …
Browse files Browse the repository at this point in the history
…in styles.py
  • Loading branch information
cleder committed Nov 20, 2023
1 parent 86893d8 commit ebd08e4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
50 changes: 34 additions & 16 deletions fastkml/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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


Expand Down
3 changes: 3 additions & 0 deletions fastkml/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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__"
Expand Down
5 changes: 5 additions & 0 deletions tests/styles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_icon_style_read(self) -> None:
"<kml:color>ff2200ff</kml:color><kml:colorMode>random</kml:colorMode>"
"<kml:scale>5</kml:scale><kml:heading>20</kml:heading><kml:Icon>"
"<kml:href>http://example.com/icon.png</kml:href></kml:Icon>"
'<kml:hotSpot x="0.5" y="0.7" xunits="fraction" yunits="insetPixels"/>'
"</kml:IconStyle>",
)

Expand All @@ -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(
Expand Down

0 comments on commit ebd08e4

Please sign in to comment.