Skip to content

Commit

Permalink
Add hypothesis test for styles and refactor StyleUrl class
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Nov 9, 2024
1 parent 5e22dc1 commit b9d37e0
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 40 deletions.
9 changes: 8 additions & 1 deletion docs/working_with_kml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Now we can create a new KML object and confirm that the new element is parsed co
To be able to open the KML file in Google Earth Pro, we need to transform the
CascadingStyle element into a supported Style element.
To achieve this we copy the styles into the document styles and adjust their id
to match the id of the CascadingStyle.

.. code-block:: pycon
Expand All @@ -173,6 +175,11 @@ CascadingStyle element into a supported Style element.
... kml_style.id = cascading_style.id
... document.styles.append(kml_style)
...
Now we can remove the CascadingStyle from the document and have a look at the result.

.. code-block:: pycon
>>> document.gx_cascading_style = []
>>> print(document.to_string(prettyprint=True))
<kml:Document xmlns:kml="http://www.opengis.net/kml/2.2">
Expand Down Expand Up @@ -222,7 +229,7 @@ CascadingStyle element into a supported Style element.
<kml:color>80000000</kml:color>
</kml:PolyStyle>
</kml:Style>
<kml:Placemark id="04AFE6060F147CE66FBD">
<kml:Placemark id="04SAFE6060F147CE66FBD">
<kml:name>Ort1</kml:name>
<kml:LookAt>
<kml:longitude>10.06256752902339</kml:longitude>
Expand Down
20 changes: 6 additions & 14 deletions fastkml/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
logger = logging.getLogger(__name__)


class StyleUrl(_BaseObject):
class StyleUrl(_XMLObject):
"""
URL of a <Style> or <StyleMap> defined in a Document.
Expand All @@ -73,14 +73,14 @@ class StyleUrl(_BaseObject):
https://developers.google.com/kml/documentation/kmlreference#styleurl
"""

_default_nsid = config.KML

url: Optional[str]

def __init__(
self,
ns: Optional[str] = None,
name_spaces: Optional[Dict[str, str]] = None,
id: Optional[str] = None,
target_id: Optional[str] = None,
url: Optional[str] = None,
**kwargs: Any,
) -> None:
Expand All @@ -93,10 +93,6 @@ def __init__(
The namespace of the Style object.
name_spaces : dict, optional
A dictionary of namespace prefixes and their corresponding URIs.
id : str, optional
The ID of the Style object.
target_id : str, optional
The ID of the target object that this Style object applies to.
url : str, optional
The URL of the Style object.
**kwargs : Any
Expand All @@ -110,8 +106,6 @@ def __init__(
super().__init__(
ns=ns,
name_spaces=name_spaces,
id=id,
target_id=target_id,
**kwargs,
)
self.url = url
Expand All @@ -122,8 +116,6 @@ def __repr__(self) -> str:
f"{self.__class__.__module__}.{self.__class__.__name__}("
f"ns={self.ns!r}, "
f"name_spaces={self.name_spaces!r}, "
f"id={self.id!r}, "
f"target_id={self.target_id!r}, "
f"url={self.url!r}, "
f"**{self._get_splat()!r},"
")"
Expand Down Expand Up @@ -352,10 +344,10 @@ def __bool__(self) -> bool:
Returns
-------
bool: True if both x and y are not None, False otherwise.
bool: True.
"""
return all((self.x is not None, self.y is not None))
return True

@classmethod
def get_tag_name(cls) -> str:
Expand Down Expand Up @@ -982,7 +974,7 @@ def __init__(
)
self.bg_color = bg_color
self.text_color = text_color
self.text = text
self.text = text.strip() or None if text else None
self.display_mode = display_mode

def __repr__(self) -> str:
Expand Down
6 changes: 6 additions & 0 deletions tests/hypothesis/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
import fastkml
from fastkml.base import _XMLObject
from fastkml.enums import AltitudeMode
from fastkml.enums import ColorMode
from fastkml.enums import DataType
from fastkml.enums import DateTimeResolution
from fastkml.enums import DisplayMode
from fastkml.enums import RefreshMode
from fastkml.enums import Units
from fastkml.enums import Verbosity
from fastkml.enums import ViewRefreshMode
from fastkml.gx import Angle
Expand All @@ -61,6 +64,9 @@
"datetime": datetime,
"DateTimeResolution": DateTimeResolution,
"DataType": DataType,
"Units": Units,
"ColorMode": ColorMode,
"DisplayMode": DisplayMode,
"tzutc": tzutc,
"tzfile": tzfile,
}
Expand Down
11 changes: 11 additions & 0 deletions tests/hypothesis/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,14 @@ def query_strings(draw: st.DrawFn) -> str:
),
)
return urlencode(params)


@st.composite
def kml_colors(draw: st.DrawFn) -> str:
return draw(
st.text(
alphabet=string.hexdigits,
min_size=8,
max_size=8,
),
)
Loading

0 comments on commit b9d37e0

Please sign in to comment.