Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stac-validator and stac-check optional #468

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install with extra_requires
run: pip install .[s3,dev]
run: pip install .[s3,validate,dev]
- name: Test
run: ./scripts/test
minimum-versions:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Make computation of statistics and histogram optional for `core.add_raster.add_raster_to_item` ([#467](https://github.com/stac-utils/stactools/pull/467))
- Make **stac-validator** and **stac-check** optional dependencies ([#468](https://github.com/stac-utils/stactools/pull/468))

## [0.5.2] - 2023-09-20

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ If your system GDAL is older than version 3.1, consider using [Docker](#using-do

### Optional dependencies

`stactools` includes one optional dependency:
`stactools` includes two optional dependency:

- `s3`: Enables s3 hrefs via `fsspec` and `s3fs`
- `validate`: Enables `stac validate` and `stac lint`

To install the single optional dependency:
To install optional dependencies:

```sh
pip install stactools[s3]
pip install 'stactools[s3]'
pip install 'stactools[validate]'
```

### Docker
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ dependencies = [
"pystac[validation]>=1.8.2",
"rasterio>=1.3.2",
"shapely>=2.0.1",
"stac-check>=1.3.2",
"stac-validator>=3.1.0",
]
dynamic = ["version"]

Expand Down Expand Up @@ -73,6 +71,7 @@ docs = [
"sphinxcontrib-fulltoc~=1.2",
]
s3 = ["s3fs>=2021.7"]
validate = ["stac-check>=1.3.2", "stac-validator>=3.1.0"]

[build-system]
requires = ["setuptools", "wheel"]
Expand Down
29 changes: 25 additions & 4 deletions src/stactools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

import stactools.core

try:
import stac_validator as _
except ImportError:
HAS_STAC_VALIDATOR = False
else:
HAS_STAC_VALIDATOR = True

try:
import stac_check as _
except ImportError:
HAS_STAC_CHECK = False
else:
HAS_STAC_CHECK = True


stactools.core.use_fsspec()


Expand All @@ -16,13 +31,11 @@ def register_plugin(registry: "Registry") -> None:
create,
info,
layout,
lint,
merge,
migrate,
summary,
update_extent,
update_geometry,
validate,
version,
)

Expand All @@ -35,15 +48,23 @@ def register_plugin(registry: "Registry") -> None:
registry.register_subcommand(info.create_info_command)
registry.register_subcommand(info.create_describe_command)
registry.register_subcommand(layout.create_layout_command)
registry.register_subcommand(lint.create_lint_command)
registry.register_subcommand(merge.create_merge_command)
registry.register_subcommand(migrate.create_migrate_command)
registry.register_subcommand(summary.create_summary_command)
registry.register_subcommand(validate.create_validate_command)
registry.register_subcommand(version.create_version_command)
registry.register_subcommand(update_extent.create_update_extent_command)
registry.register_subcommand(update_geometry.create_update_geometry_command)

if HAS_STAC_VALIDATOR:
from stactools.cli.commands import validate

registry.register_subcommand(validate.create_validate_command)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my only question is what happens if a user tries to run stac validate and they don't have the deps? Ideally that would result in a nice error message that tells them what to do. So maybe that would lend itself to always registering and then have the error raised within the actual call to do validation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's a good point. I can make a follow-on that makes the UX a bit nicer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see people writing import_optional helpers a lot. Ala https://github.com/stac-utils/xpystac/blob/main/xpystac/utils.py#L5-L10


if HAS_STAC_CHECK:
from stactools.cli.commands import lint

registry.register_subcommand(lint.create_lint_command)


from stactools.cli.registry import Registry

Expand Down
3 changes: 3 additions & 0 deletions tests/cli/commands/test_lint.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import pytest
from click.testing import CliRunner
from stactools.cli.cli import cli

from tests import test_data

pytest.importorskip("stac_check")


def test_valid_item() -> None:
path = test_data.get_path("data-files/linting/20201211_223832_cs2.json")
Expand Down
2 changes: 2 additions & 0 deletions tests/cli/commands/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from tests import test_data

pytest.importorskip("stac_validator")


def test_valid_item() -> None:
path = test_data.get_path(
Expand Down
Loading