Skip to content

Commit

Permalink
Merge branch 'cleder:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
apurvabanka authored Nov 16, 2024
2 parents e1aa026 + ac78a7e commit 7c02d72
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-all-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
pytest tests --cov=fastkml --cov=tests --cov-fail-under=95 --cov-report=xml
- name: "Upload coverage to Codecov"
if: ${{ matrix.python-version==3.11 }}
uses: codecov/codecov-action@v4
uses: codecov/codecov-action@v5
with:
fail_ci_if_error: true
verbose: true
Expand Down
19 changes: 10 additions & 9 deletions fastkml/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,15 +1100,16 @@ def datetime_subelement_kwarg(
return {}
node_text = node.text.strip() if node.text else ""
if node_text:
if kdt := cls.parse(node_text): # type: ignore[attr-defined]
return {kwarg: kdt}
handle_error(
error=ValueError(f"Invalid DateTime value: {node_text}"),
strict=strict,
element=element,
node=node,
expected="DateTime",
)
try:
return {kwarg: cls.parse(node_text)} # type: ignore[attr-defined]
except ValueError as exc:
handle_error(
error=exc,
strict=strict,
element=element,
node=node,
expected="DateTime",
)
return {}


Expand Down
8 changes: 3 additions & 5 deletions fastkml/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

# regular expression to parse a gYearMonth string
# year and month may be separated by an optional dash
# year is always 4 digits, month is always 2 digits
# year is always 4 digits, month, day is always 2 digits
year_month_day = re.compile(
r"^(?P<year>\d{4})(?:-)?(?P<month>\d{2})?(?:-)?(?P<day>\d{2})?$",
)
Expand Down Expand Up @@ -205,10 +205,8 @@ def parse(cls, datestr: str) -> Optional["KmlDateTime"]:
resolution = DateTimeResolution.year_month
if year_month_day_match.group("month") is None:
resolution = DateTimeResolution.year
elif len(datestr) > 10: # noqa: PLR2004
dt = arrow.get(datestr).datetime
resolution = DateTimeResolution.datetime
return cls(dt, resolution) if dt else None
return cls(dt, resolution)
return cls(arrow.get(datestr).datetime, DateTimeResolution.datetime)

@classmethod
def get_ns_id(cls) -> str:
Expand Down
40 changes: 29 additions & 11 deletions fastkml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ def has_attribute_values(obj: object, **kwargs: Any) -> bool:
return False


def get_all_attrs(obj: object) -> Generator[object, None, None]:
"""
Get all attributes of an object.
Args:
----
obj: The object to get attributes from.
Returns:
-------
An iterable of all attributes of the object or, if the attribute itself is
iterable, iterate over the attribute values.
"""
try:
attrs = (attr for attr in obj.__dict__ if not attr.startswith("_"))
except AttributeError:
return
for attr_name in attrs:
attr = getattr(obj, attr_name)
try:
yield from attr
except TypeError:
yield attr


def find_all(
obj: object,
*,
Expand All @@ -55,17 +81,9 @@ def find_all(
**kwargs,
):
yield obj
try:
attrs = (attr for attr in obj.__dict__ if not attr.startswith("_"))
except AttributeError:
return
for attr_name in attrs:
attr = getattr(obj, attr_name)
try:
for item in attr:
yield from find_all(item, of_type=of_type, **kwargs)
except TypeError:
yield from find_all(attr, of_type=of_type, **kwargs)

for attr in get_all_attrs(obj):
yield from find_all(attr, of_type=of_type, **kwargs)


def find(
Expand Down
12 changes: 10 additions & 2 deletions tests/times_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,19 @@ def test_parse_datetime_no_tz(self) -> None:
assert dt.dt == datetime.datetime(1997, 7, 16, 7, 30, 15, tzinfo=tzutc())

def test_parse_datetime_empty(self) -> None:
assert KmlDateTime.parse("") is None
with pytest.raises(
ValueError,
match="^Could not match input '' to any of the following formats:",
):
KmlDateTime.parse("")

def test_parse_year_month_5(self) -> None:
"""Test that a single digit month is invalid."""
assert KmlDateTime.parse("19973") is None
with pytest.raises(
ValueError,
match="^Could not match input '19973' to any of the following formats:",
):
KmlDateTime.parse("19973")


class TestStdLibrary(StdLibrary):
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ per-file-ignores =
examples/*.py: DALL
fastkml/gx.py: LIT002
fastkml/views.py: LIT002
fastkml/utils.py: CCR001
fastkml/registry.py: E704
docs/conf.py: E402
enable-extensions=G
Expand Down

0 comments on commit 7c02d72

Please sign in to comment.