Skip to content

Commit

Permalink
Compat wrapper for deprecated read_text (#38)
Browse files Browse the repository at this point in the history
* Compat wrapper for importlib.resources.read_text

* Regression test the full repr and rename tests
  • Loading branch information
aazuspan authored Nov 29, 2024
1 parent 122be19 commit 2ece465
Show file tree
Hide file tree
Showing 45 changed files with 330 additions and 15 deletions.
28 changes: 22 additions & 6 deletions eerepr/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import uuid
from functools import _lru_cache_wrapper, lru_cache
from html import escape
from importlib.resources import read_text
from typing import Any, Union
from warnings import warn

Expand All @@ -15,21 +14,38 @@
REPR_HTML = "_repr_html_"
EEObject = Union[ee.Element, ee.ComputedObject]

# Track which html reprs have been set so we can overwrite them if needed.
# Track which repr methods have been set so we can overwrite them if needed.
reprs_set = set()


@lru_cache(maxsize=None)
def _load_file(package: str, resource: str) -> str:
"""
Compatibility wrapper for deprecated `importlib.resources.read_text`.
Replace with `importlib.resources.files` once support for Python < 3.9 is dropped.
"""
try:
# Python >= 3.9
from importlib.resources import files

return files(package).joinpath(resource).read_text()
except ImportError:
from importlib.resources import read_text

return read_text(package, resource)


@lru_cache(maxsize=1)
def _load_css() -> str:
return read_text("eerepr.static.css", "style.css")
return _load_file("eerepr.static.css", "style.css")


@lru_cache(maxsize=None)
@lru_cache(maxsize=1)
def _load_js() -> str:
"""Note: JS is only needed because the CSS `:has()` selector isn't well supported
yet, preventing a pure CSS solution to the collapsible lists.
"""
return read_text("eerepr.static.js", "script.js")
return _load_file("eerepr.static.js", "script.js")


def _attach_html_repr(cls: type, repr: Any) -> None:
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from pathlib import Path

import ee
import pytest


def pytest_sessionstart(session):
ee.Initialize()


@pytest.fixture(scope="session")
def original_datadir():
"""Set the pytest-regressions directory."""
return Path(__file__).parent / "data"
286 changes: 286 additions & 0 deletions tests/data/test_full_repr.yml

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 2 additions & 8 deletions tests/test_html.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pathlib import Path

import ee
import pytest

Expand Down Expand Up @@ -90,11 +88,7 @@ def get_test_objects() -> list:
}


@pytest.fixture(scope="session")
def original_datadir():
return Path(__file__).parent / "data"


@pytest.mark.parametrize("key_val", get_test_objects().items(), ids=lambda kv: kv[0])
def test_regression(key_val, data_regression):
def test_regression_objects(key_val, data_regression):
"""Test the HTML repr of various EE objects."""
data_regression.check(convert_to_html(get_info(key_val[1])))
12 changes: 11 additions & 1 deletion tests/test_reprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
import pytest

import eerepr # noqa: F401
from eerepr.repr import _repr_html_


def test_error():
"""Test that an object that raises on getInfo falls abck to the string repr and
"""Test that an object that raises on getInfo falls back to the string repr and
warns.
"""
with pytest.warns(UserWarning):
rep = ee.Projection("not a real epsg")._repr_html_()
assert "Projection object" in rep


def test_full_repr(data_regression):
"""Regression test the full HTML repr (with CSS and JS) of a nested EE object."""
from tests.test_html import get_test_objects

objects = get_test_objects().items()
rendered = _repr_html_(ee.List([obj[1] for obj in objects]))
data_regression.check(rendered)

0 comments on commit 2ece465

Please sign in to comment.