Skip to content

Commit

Permalink
Merge pull request #490 from duckontheweb/remove/deprecated-stac-io
Browse files Browse the repository at this point in the history
Remove deprecated STAC_IO class
  • Loading branch information
Jon Duckworth authored Jun 29, 2021
2 parents 365aa60 + c0a2c9b commit 93d7720
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 228 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

### Removed

- `STAC_IO` class in favor of `StacIO`. This was deprecated in v1.0.0-beta.1 and has
been removed in this release. ([#490](https://github.com/stac-utils/pystac/pull/490))

### Deprecated

## [v1.0.0-rc.2]
Expand Down Expand Up @@ -133,7 +136,7 @@
- Removed `LinkMixin`, and implemented those methods on `STACObject` directly. STACObject was the only class using LinkMixin and this should not effect users ([#309](https://github.com/stac-utils/pystac/pull/309)
- Removed `single-file-stac` extension; this extension is being removed in favor of ItemCollection usage ([#309](https://github.com/stac-utils/pystac/pull/309)

# Deprecated
### Deprecated

- Deprecated `STAC_IO` in favor of new `StacIO` class. `STAC_IO` will be removed in
v1.0.0. ([#309](https://github.com/stac-utils/pystac/pull/309))
Expand Down
10 changes: 0 additions & 10 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,6 @@ DuplicateKeyReportingMixin
:members:
:show-inheritance:

STAC_IO
~~~~~~~

.. deprecated:: 1.0.0-beta.1
Use :class:`pystac.StacIO` instead. This class will be removed in v1.0.0.

.. autoclass:: pystac.stac_io.STAC_IO
:members:
:undoc-members:

Layout
------

Expand Down
167 changes: 0 additions & 167 deletions pystac/stac_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Type,
Union,
)
import warnings

from urllib.request import urlopen
from urllib.error import HTTPError
Expand Down Expand Up @@ -388,169 +387,3 @@ def _report_duplicate_object_names(
else:
result[key] = value
return result


class STAC_IO:
"""DEPRECATED: Methods used to read and save STAC json.
Allows users of the library to set their own methods
(e.g. for reading and writing from cloud storage)
Note: The static methods of this class are deprecated. Move to using
instance methods of a specific instance of StacIO.
"""

@staticmethod
def issue_deprecation_warning() -> None:
warnings.warn(
"STAC_IO is deprecated and will be removed in v1.0.0. "
"Please use instances of StacIO (e.g. StacIO.default()) instead.",
DeprecationWarning,
)

def __init__(self) -> None:
STAC_IO.issue_deprecation_warning()

def __init_subclass__(cls) -> None:
STAC_IO.issue_deprecation_warning()

@staticmethod
def read_text_method(uri: str) -> str:
STAC_IO.issue_deprecation_warning()
return StacIO.default().read_text(uri)

@staticmethod
def write_text_method(uri: str, txt: str) -> None:
"""Default method for writing text."""
STAC_IO.issue_deprecation_warning()
return StacIO.default().write_text(uri, txt)

@staticmethod
def stac_object_from_dict(
d: Dict[str, Any],
href: Optional[str] = None,
root: Optional["Catalog_Type"] = None,
) -> "STACObject_Type":
STAC_IO.issue_deprecation_warning()
if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
collection_cache = None
if root is not None:
collection_cache = root._resolved_objects.as_collection_cache()

# Merge common properties in case this is an older STAC object.
merge_common_properties(
d, json_href=href, collection_cache=collection_cache
)

info = identify_stac_object(d)

d = migrate_to_latest(d, info)

if info.object_type == pystac.STACObjectType.CATALOG:
return pystac.Catalog.from_dict(d, href=href, root=root, migrate=False)

if info.object_type == pystac.STACObjectType.COLLECTION:
return pystac.Collection.from_dict(d, href=href, root=root, migrate=False)

if info.object_type == pystac.STACObjectType.ITEM:
return pystac.Item.from_dict(d, href=href, root=root, migrate=False)

raise ValueError(f"Unknown STAC object type {info.object_type}")

# This is set in __init__.py
_STAC_OBJECT_CLASSES = None

@classmethod
def read_text(cls, uri: str) -> str:
"""Read text from the given URI.
Args:
uri : The URI from which to read text.
Returns:
str: The text contained in the file at the location specified by the uri.
Note:
This method uses the :func:`STAC_IO.read_text_method
<STAC_IO.read_text_method>`. If you want to modify the behavior of
STAC_IO in order to enable additional URI types, replace that member
with your own implementation.
"""
return cls.read_text_method(uri)

@classmethod
def write_text(cls, uri: str, txt: str) -> None:
"""Write the given text to a file at the given URI.
Args:
uri : The URI of the file to write the text to.
txt : The text to write.
Note:
This method uses the :func:`STAC_IO.write_text_method
<STAC_IO.write_text_method>`. If you want to modify the behavior of
STAC_IO in order to enable additional URI types, replace that member
with your own implementation.
"""
cls.write_text_method(uri, txt)

@classmethod
def read_json(cls, uri: str) -> Dict[str, Any]:
"""Read a dict from the given URI.
Args:
uri : The URI from which to read.
Returns:
dict: A dict representation of the JSON contained in the file at the
given uri.
Note:
This method uses the :func:`STAC_IO.read_text_method
<STAC_IO.read_text_method>`. If you want to modify the behavior of
STAC_IO in order to enable additional URI types, replace that member
with your own implementation.
"""
STAC_IO.issue_deprecation_warning()
result: Dict[str, Any] = json.loads(STAC_IO.read_text(uri))
return result

@classmethod
def read_stac_object(
cls, uri: str, root: Optional["Catalog_Type"] = None
) -> "STACObject_Type":
"""Read a STACObject from a JSON file at the given URI.
Args:
uri : The URI from which to read.
root : Optional root of the catalog for this object.
If provided, the root's resolved object cache can be used to search for
previously resolved instances of the STAC object.
Returns:
STACObject: The deserialized STACObject from the serialized JSON
contained in the file at the given uri.
Note:
This method uses the :func:`STAC_IO.read_text_method
<STAC_IO.read_text_method>`. If you want to modify the behavior of
STAC_IO in order to enable additional URI types, replace that member
with your own implementation.
"""
d = cls.read_json(uri)
return cls.stac_object_from_dict(d, uri, root)

@classmethod
def save_json(cls, uri: str, json_dict: Dict[str, Any]) -> None:
"""Write a dict to the given URI as JSON.
Args:
uri : The URI of the file to write the text to.
json_dict : The JSON dict to write.
Note:
This method uses the :func:`STAC_IO.write_text_method
<STAC_IO.write_text_method>`. If you want to modify the behavior of
STAC_IO in order to enable additional URI types, replace that member
with your own implementation.
"""
STAC_IO.write_text(uri, json.dumps(json_dict, indent=4))
49 changes: 1 addition & 48 deletions tests/test_stac_io.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,16 @@
import os
import unittest
import warnings
import tempfile

import pystac
from pystac.stac_io import STAC_IO, StacIO, DefaultStacIO, DuplicateKeyReportingMixin
from pystac.stac_io import StacIO, DefaultStacIO, DuplicateKeyReportingMixin
from tests.utils import TestCases


class StacIOTest(unittest.TestCase):
def setUp(self) -> None:
self.stac_io = StacIO.default()

def test_stac_io_issues_warnings(self) -> None:
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger a warning.
STAC_IO.read_text(
TestCases.get_path("data-files/collections/multi-extent.json")
)

# Verify some things
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))

with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger instantiation warning.
_ = STAC_IO()

self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))

with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")

class CustomSTAC_IO(STAC_IO):
pass

self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, DeprecationWarning))

with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")

d = STAC_IO.read_json(
TestCases.get_path("data-files/item/sample-item.json")
)
_ = STAC_IO.stac_object_from_dict(d)

self.assertEqual(len(w), 3)
self.assertTrue(
all(issubclass(wrn.category, DeprecationWarning) for wrn in w)
)

def test_read_write_collection(self) -> None:
collection = pystac.read_file(
TestCases.get_path("data-files/collections/multi-extent.json")
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/stac_io_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


class MockStacIO(pystac.StacIO):
"""Creates a mock that records STAC_IO calls for testing and allows
clients to replace STAC_IO functionality, all within a context scope.
"""Creates a mock that records StacIO calls for testing and allows
clients to replace StacIO functionality, all within a context scope.
"""

def __init__(self) -> None:
Expand Down

0 comments on commit 93d7720

Please sign in to comment.