diff --git a/fastkml/times.py b/fastkml/times.py index 7171562b..6f5fbb00 100644 --- a/fastkml/times.py +++ b/fastkml/times.py @@ -22,6 +22,7 @@ https://developers.google.com/kml/documentation/time """ +from __future__ import annotations import re from datetime import date from datetime import datetime @@ -30,6 +31,7 @@ from typing import Optional from typing import Union + import arrow from fastkml import config @@ -174,7 +176,7 @@ def __init__( name_spaces: Optional[Dict[str, str]] = None, id: Optional[str] = None, target_id: Optional[str] = None, - timestamp: Optional[KmlDateTime] = None, + point_in_time: Optional[KmlDateTime | When] = None, **kwargs: Any, ) -> None: """ @@ -207,7 +209,8 @@ def __init__( target_id=target_id, **kwargs, ) - self.timestamp = timestamp + self.point_in_time = point_in_time + def __repr__(self) -> str: """Create a string (c)representation for TimeStamp.""" @@ -217,60 +220,14 @@ def __repr__(self) -> str: f"name_spaces={self.name_spaces!r}, " f"id={self.id!r}, " f"target_id={self.target_id!r}, " - f"timestamp={self.timestamp!r}, " + f"point_in_time={self.point_in_time!r}, " f"**{self._get_splat()!r}," ")" ) def __bool__(self) -> bool: """Return True if the timestamp is valid.""" - return bool(self.timestamp) - - def etree_element( - self, - precision: Optional[int] = None, - verbosity: Verbosity = Verbosity.normal, - ) -> Element: - """ - Create an ElementTree element representing the TimeStamp object. - - Args: - ---- - precision (Optional[int]): The precision of the timestamp. - verbosity (Verbosity): The verbosity level of the element. - - Returns: - ------- - Element: The ElementTree element representing the TimeStamp object. - - """ - element = super().etree_element(precision=precision, verbosity=verbosity) - when = config.etree.SubElement( - element, - f"{self.ns}when", - ) - when.text = str(self.timestamp) - return element - - @classmethod - def _get_kwargs( - cls, - *, - ns: str, - name_spaces: Optional[Dict[str, str]] = None, - element: Element, - strict: bool, - ) -> Dict[str, Any]: - kwargs = super()._get_kwargs( - ns=ns, - name_spaces=name_spaces, - element=element, - strict=strict, - ) - when = element.find(f"{ns}when") - if when is not None: - kwargs["timestamp"] = KmlDateTime.parse(when.text) - return kwargs + return bool(self.point_in_time) class TimeSpan(_TimePrimitive): @@ -337,60 +294,14 @@ def __bool__(self) -> bool: """Return True if the begin or end date is valid.""" return bool(self.begin) or bool(self.end) - def etree_element( - self, - precision: Optional[int] = None, - verbosity: Verbosity = Verbosity.normal, - ) -> Element: - """ - Create an Element object representing the time interval. - Args: - ---- - precision (Optional[int]): The precision of the time values. - verbosity (Verbosity): The verbosity level for the element. +class When(_BaseObject): + ... - Returns: - ------- - Element: The created Element object. - """ - element = super().etree_element(precision=precision, verbosity=verbosity) - if self.begin is not None: # noqa: SIM102 - if text := str(self.begin): - begin = config.etree.SubElement( - element, - f"{self.ns}begin", - ) - begin.text = text - if self.end is not None: # noqa: SIM102 - if text := str(self.end): - end = config.etree.SubElement( - element, - f"{self.ns}end", - ) - end.text = text - return element +class Begin(_BaseObject): + ... - @classmethod - def _get_kwargs( - cls, - *, - ns: str, - name_spaces: Optional[Dict[str, str]] = None, - element: Element, - strict: bool, - ) -> Dict[str, Any]: - kwargs = super()._get_kwargs( - ns=ns, - name_spaces=name_spaces, - element=element, - strict=strict, - ) - begin = element.find(f"{ns}begin") - if begin is not None: - kwargs["begin"] = KmlDateTime.parse(begin.text) - end = element.find(f"{ns}end") - if end is not None: - kwargs["end"] = KmlDateTime.parse(end.text) - return kwargs + +class End(_BaseObject): + ...