Skip to content

Commit 3794b11

Browse files
Support latest protobuf version
1 parent d22c2bf commit 3794b11

3 files changed

Lines changed: 89 additions & 11 deletions

File tree

tilebox-datasets/tilebox/datasets/protobuf_conversion/protobuf_xarray.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,19 +374,16 @@ def _create_field_converter(field: FieldDescriptor) -> _FieldConverter:
374374
"""
375375
# special handling for enums:
376376
if field.type == FieldDescriptor.TYPE_ENUM:
377-
if field.label == FieldDescriptor.LABEL_REPEATED:
377+
if field.is_repeated:
378378
raise NotImplementedError("Repeated enum fields are not supported")
379379

380380
return _EnumFieldConverter(field.name, enum_mapping_from_field_descriptor(field))
381381

382382
field_type = infer_field_type(field)
383-
if field.label == FieldDescriptor.LABEL_OPTIONAL: # simple fields (in proto3 every simple field is optional)
384-
return _SimpleFieldConverter(field.name, field_type)
385-
386-
if field.label == FieldDescriptor.LABEL_REPEATED:
383+
if field.is_repeated:
387384
return _ArrayFieldConverter(field.name, field_type)
388385

389-
raise ValueError(f"Unsupported field type with label {field.label} and type {field.type}")
386+
return _SimpleFieldConverter(field.name, field_type)
390387

391388

392389
def _combine_dimension_names(array_dimensions: dict[str, int]) -> dict[str, tuple[str, int]]:

tilebox-datasets/tilebox/datasets/protobuf_conversion/to_protobuf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import pandas as pd
88
import xarray as xr
9-
from google.protobuf.descriptor import FieldDescriptor
109
from google.protobuf.message import Message
1110

1211
from tilebox.datasets.protobuf_conversion.field_types import (
@@ -80,7 +79,7 @@ def to_messages( # noqa: C901, PLR0912
8079
descriptor = field_descriptors_by_name[field_name]
8180
field_type = infer_field_type(descriptor)
8281

83-
if descriptor.label == FieldDescriptor.LABEL_REPEATED:
82+
if descriptor.is_repeated:
8483
values = convert_repeated_values_to_proto(values, field_type)
8584
else:
8685
values = convert_values_to_proto(values, field_type, filter_none=False)

tilebox-storage/tests/test_storage_client.py

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@
99
from obstore.store import LocalStore
1010
from pytest_httpx import HTTPXMock, IteratorStream
1111

12-
from tests.storage_data import ers_granules, s5p_granules, umbra_granules
13-
from tilebox.storage.aio import ASFStorageClient, CopernicusStorageClient, UmbraStorageClient, _HttpClient
14-
from tilebox.storage.granule import ASFStorageGranule, CopernicusStorageGranule, UmbraStorageGranule
12+
from tests.storage_data import ers_granules, landsat_granules, s5p_granules, umbra_granules
13+
from tilebox.storage.aio import (
14+
ASFStorageClient,
15+
CopernicusStorageClient,
16+
UmbraStorageClient,
17+
USGSLandsatStorageClient,
18+
_HttpClient,
19+
)
20+
from tilebox.storage.granule import (
21+
ASFStorageGranule,
22+
CopernicusStorageGranule,
23+
UmbraStorageGranule,
24+
USGSLandsatStorageGranule,
25+
)
1526

1627

1728
@pytest.mark.asyncio
@@ -277,3 +288,74 @@ async def test_copernicus_storage_client_download_objects(granule: CopernicusSto
277288
assert folder.exists()
278289
assert (folder / granule.granule_name).read_bytes() == b"content1"
279290
assert not (folder / f"other_product_{granule.granule_name}").exists()
291+
292+
293+
@pytest.mark.asyncio
294+
@given(landsat_granules())
295+
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
296+
async def test_landsat_storage_client_download(granule: USGSLandsatStorageGranule) -> None:
297+
with TemporaryDirectory(delete=True) as tmp_path:
298+
store_path = Path(tmp_path) / "store"
299+
store_path.mkdir(exist_ok=True, parents=True)
300+
store = LocalStore(store_path)
301+
302+
await store.put_async(
303+
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}", b"content1"
304+
)
305+
with patch("tilebox.storage.aio.S3Store") as store_mock:
306+
store_mock.return_value = store
307+
landsat = USGSLandsatStorageClient(cache_directory=Path(tmp_path))
308+
309+
folder = await landsat.download(granule, show_progress=False)
310+
assert folder.exists()
311+
assert (folder / granule.granule_name).read_bytes() == b"content1"
312+
313+
314+
@pytest.mark.asyncio
315+
@given(landsat_granules())
316+
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
317+
async def test_landsat_storage_client_list_objects(granule: USGSLandsatStorageGranule) -> None:
318+
with TemporaryDirectory(delete=True) as tmp_path:
319+
store_path = Path(tmp_path) / "store"
320+
store_path.mkdir(exist_ok=True, parents=True)
321+
store = LocalStore(store_path)
322+
323+
await store.put_async(
324+
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}", b"content1"
325+
)
326+
await store.put_async(
327+
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}_thumb_small.jpeg",
328+
b"content2",
329+
)
330+
with patch("tilebox.storage.aio.S3Store") as store_mock:
331+
store_mock.return_value = store
332+
landsat = USGSLandsatStorageClient(cache_directory=Path(tmp_path))
333+
334+
objects = await landsat.list_objects(granule)
335+
assert len(objects) == 2
336+
assert sorted(objects) == sorted([granule.granule_name, f"{granule.granule_name}_thumb_small.jpeg"])
337+
338+
339+
@pytest.mark.asyncio
340+
@given(landsat_granules())
341+
@settings(max_examples=1, deadline=timedelta(milliseconds=100))
342+
async def test_landsat_storage_client_download_objects(granule: USGSLandsatStorageGranule) -> None:
343+
with TemporaryDirectory(delete=True) as tmp_path:
344+
store_path = Path(tmp_path) / "store"
345+
store_path.mkdir(exist_ok=True, parents=True)
346+
store = LocalStore(store_path)
347+
348+
await store.put_async(
349+
f"{granule.location.removeprefix('s3://usgs-landsat/')}/{granule.granule_name}", b"content1"
350+
)
351+
await store.put_async(
352+
f"{granule.location.removeprefix('s3://usgs-landsat/')}/other_product_{granule.granule_name}", b"content2"
353+
)
354+
with patch("tilebox.storage.aio.S3Store") as store_mock:
355+
store_mock.return_value = store
356+
landsat = USGSLandsatStorageClient(cache_directory=Path(tmp_path))
357+
358+
folder = await landsat.download_objects(granule, [granule.granule_name], show_progress=False)
359+
assert folder.exists()
360+
assert (folder / granule.granule_name).read_bytes() == b"content1"
361+
assert not (folder / f"other_product_{granule.granule_name}").exists()

0 commit comments

Comments
 (0)