Skip to content

Commit

Permalink
Refactor times.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitc committed Nov 1, 2024
1 parent 68a3a8a commit 9a27a1c
Showing 1 changed file with 14 additions and 103 deletions.
117 changes: 14 additions & 103 deletions fastkml/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,7 @@
from typing import Optional
from typing import Union


import arrow

from fastkml import config
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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."""
Expand All @@ -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):
Expand Down Expand Up @@ -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):
...

0 comments on commit 9a27a1c

Please sign in to comment.