Skip to content

Commit

Permalink
Repeated type annotations in docstrings removed; data store and data …
Browse files Browse the repository at this point in the history
…opener split in two seperated files
  • Loading branch information
konstntokas committed Apr 26, 2024
1 parent dd54b8f commit 916d671
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 167 deletions.
53 changes: 53 additions & 0 deletions test/test_opener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# The MIT License (MIT)
# Copyright (c) 2024 by the xcube development team and contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import unittest

from xcube.util.jsonschema import JsonObjectSchema
from xcube_stac.store import StacDataOpener
from xcube_stac.stac import Stac


class StacDataOpenerTest(unittest.TestCase):

def setUp(self) -> None:
stac_instance = Stac("url")
self.opener = StacDataOpener(stac_instance)

def test_get_open_data_params_schema(self):
schema = self.opener.get_open_data_params_schema()
self.assertIsInstance(schema, JsonObjectSchema)

def test_open_data(self):
with self.assertRaises(NotImplementedError) as cm:
self.opener.open_data("data_id1")
self.assertEqual(
"open_data() operation is not supported yet",
f"{cm.exception}",
)

def test_describe_data(self):
with self.assertRaises(NotImplementedError) as cm:
self.opener.describe_data("data_id1")
self.assertEqual(
"describe_data() operation is not supported yet",
f"{cm.exception}",
)
29 changes: 0 additions & 29 deletions test/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,7 @@
import unittest

from xcube.util.jsonschema import JsonObjectSchema
from xcube_stac.store import StacDataOpener
from xcube_stac.store import StacDataStore
from xcube_stac.stac import Stac


class StacDataOpenerTest(unittest.TestCase):

def setUp(self) -> None:
stac_instance = Stac("url")
self.opener = StacDataOpener(stac_instance)

def test_get_open_data_params_schema(self):
schema = self.opener.get_open_data_params_schema()
self.assertIsInstance(schema, JsonObjectSchema)

def test_open_data(self):
with self.assertRaises(NotImplementedError) as cm:
self.opener.open_data("data_id1")
self.assertEqual(
"open_data() operation is not supported yet",
f"{cm.exception}",
)

def test_describe_data(self):
with self.assertRaises(NotImplementedError) as cm:
self.opener.describe_data("data_id1")
self.assertEqual(
"describe_data() operation is not supported yet",
f"{cm.exception}",
)


class StacDataStoreTest(unittest.TestCase):
Expand Down
99 changes: 99 additions & 0 deletions xcube_stac/opener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# The MIT License (MIT)
# Copyright (c) 2024 by the xcube development team and contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import logging
import xarray as xr

from xcube.util.jsonschema import JsonObjectSchema
from xcube.core.store import (
DataOpener,
DatasetDescriptor
)
from .stac import Stac

_LOG = logging.getLogger("xcube")


class StacDataOpener(DataOpener):
""" STAC implementation of the data opener.
Attributes:
stac: Common operations on STAC catalogs
"""

def __init__(self, stac: Stac):
self.stac = stac

def get_open_data_params_schema(self, data_id: str = None) -> JsonObjectSchema:
""" Get the schema for the parameters passed as *open_params* to
:meth:`open_data`.
Args:
data_id: An identifier of data that is provided by this
store. Defaults to None.
Returns:
Schema containing the parameters used by the data opener
to open data.
"""
# ToDo: to be adjusted
open_parms = {}
stac_schema = JsonObjectSchema(
properties=dict(**open_parms),
required=[],
additional_properties=False
)
return stac_schema

def open_data(self, data_id: str, **open_params) -> xr.Dataset:
""" Open the data given by the data resource identifier *data_id*
using the supplied *open_params*.
Args:
data_id: An identifier of data that is provided by this
store.
Returns:
An in-memory representation of the data resources
identified by *data_id* and *open_params*.
"""
stac_schema = self.get_open_data_params_schema()
stac_schema.validate_instance(open_params)
return self.stac.open_data(data_id, **open_params)

def describe_data(
self, data_id: str, **open_params
) -> DatasetDescriptor:
""" Get the descriptor for the data resource given by *data_id*.
Args:
data_id: An identifier of data that is provided by this
store.
Raises:
NotImplementedError: Not implemented yet.
Returns:
Data descriptor containing meta data of
the data resources identified by *data_id*
"""
# ToDo: implement describe_data method.
raise NotImplementedError("describe_data() operation is not supported yet")
28 changes: 14 additions & 14 deletions xcube_stac/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@


class Stac:
""" Class containing methods handling STAC catalogs
""" Common operations on STAC catalogs.
Attributes:
url: URL to STAC catalog
collection_prefix: Path of collection used as
entry point. Defaults to None.
data_id_delimiter: Delimiter used to separate
collections, items and assets from each other.
Defaults to "/".
"""

def __init__(
self, url: str,
self,
url: str,
collection_prefix: str = None,
data_id_delimiter: str = "/"
):
"""
Args:
url (str): URL to STAC catalog
collection_prefix (str, optional): Path of collection used as
entry point. Defaults to None.
data_id_delimiter (str, optional): Delimiter used to separate
collections, items and assets from each other.
Defaults to "/".
"""
self._url = url
self._collection_prefix = collection_prefix
self._data_id_delimiter = data_id_delimiter
Expand All @@ -51,15 +51,15 @@ def open_data(self, data_id: str, **open_params) -> xr.Dataset:
using the supplied *open_params*.
Args:
data_id (str): An identifier of data that is provided by this
data_id: An identifier of data that is provided by this
store.
Raises:
NotImplementedError: Not implemented yet.
Returns:
xr.Dataset: An in-memory representation of the data resources
identified by *data_id* and *open_params*.
An in-memory representation of the data resources
identified by *data_id* and *open_params*.
"""
# ToDo: implement this method using data store "file", see __init__()
raise NotImplementedError("open_data() operation is not supported yet")
Loading

0 comments on commit 916d671

Please sign in to comment.