Skip to content

Commit

Permalink
feat: pretty total rewrite
Browse files Browse the repository at this point in the history
Includes:
- pydantic under the hood
- move handler out of task into payload
- STAC models
  • Loading branch information
gadomski committed Sep 19, 2023
1 parent 06f22f5 commit 5706d92
Show file tree
Hide file tree
Showing 40 changed files with 994 additions and 1,269 deletions.
42 changes: 20 additions & 22 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,35 @@ on:
pull_request:

jobs:
standard:
name: Standard
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
defaults:
run:
shell: bash -l {0}
steps:
- uses: actions/checkout@v4
- name: Set up pip cache
uses: actions/cache@v3.3.2
- uses: actions/setup-python@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.py', '**/requirements-dev.txt') }}
restore-keys: ${{ runner.os }}-pip-
python-version: ${{ matrix.python-version }}
cache: pip
- name: Install
run: pip install .[dev]
- name: Pre-commit
run: pre-commit run --all-files
- name: Test
run: ./scripts/cibuild
codecov:
name: Codecov
needs:
- standard
run: pytest
example:
name: Example
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Execute linters and test suites
run: ./scripts/cibuild
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
- uses: actions/setup-python@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
fail_ci_if_error: false
python-version: "3.11"
cache: pip
- name: Install
run: pip install .[example]
- name: Run example
run: scripts/run-example

5 changes: 1 addition & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"

python-version: "3.11"
- name: Install release dependencies
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Build and publish package
env:
TWINE_USERNAME: ${{ secrets.PYPI_STACUTILS_USERNAME }}
Expand Down
24 changes: 10 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@
# Please run `pre-commit run --all-files` when adding or changing entries.

repos:
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
hooks:
- id: codespell
args: [--ignore-words=.codespellignore]
types_or: [jupyter, markdown, python, shell]
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: isort
args: ["--profile", "black"]
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.0
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
- click
- pydantic
- pytest
- types-setuptools == 65.7.0.3
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.289
hooks:
- id: ruff
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include stactask/py.typed
include src/stac_task/py.typed
170 changes: 1 addition & 169 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,169 +1 @@
# STAC Task (stactask)

This Python library consists of the Task class, which is used to create custom tasks based
on a "STAC In, STAC Out" approach. The Task class acts as wrapper around custom code and provides
several convenience methods for modifying STAC Items, creating derived Items, and providing a CLI.

This library is based on a [branch of cirrus-lib](https://github.com/cirrus-geo/cirrus-lib/tree/features/task-class) except aims to be more generic.

## Quickstart for Creating New Tasks

```python
from typing import Any, Dict, List

from stactask import Task

class MyTask(Task):
name = "my-task"
description = "this task does it all"

def validate(self, payload: Dict[str, Any]) -> bool:
return len(self.items) == 1

def process(self, **kwargs: Any) -> List[Dict[str, Any]]:
item = self.items[0]

# download a datafile
item = self.download_item_assets(item, assets=['data'])

# operate on the local file to create a new asset
item = self.upload_item_assets_to_s3(item)

# this task returns a single item
return [item.to_dict(include_self_link=True, transform_hrefs=False)]
```

## Task Input

| Field Name | Type | Description |
| ------------- | ---- | ----------- |
| type | string | Must be FeatureCollection |
| features | [Item] | A list of STAC `Item` |
| process | ProcessDefinition | A Process Definition |

### ProcessDefinition Object

A STAC task can be provided additional configuration via the 'process' field in the input
ItemCollection.

| Field Name | Type | Description |
| ------------- | ---- | ----------- |
| description | string | Optional description of the process configuration |
| upload_options | UploadOptions | Options used when uploading assets to a remote server |
| tasks | Map<str, Map> | Dictionary of task configurations. A List of [task configurations](#taskconfig-object) is supported for backwards compatibility reasons, but a dictionary should be preferred. |

#### UploadOptions Object

| Field Name | Type | Description |
| ------------- | ---- | ----------- |
| path_template | string | **REQUIRED** A string template for specifying the location of uploaded assets |
| public_assets | [str] | A list of asset keys that should be marked as public when uploaded |
| headers | Map<str, str> | A set of key, value headers to send when uploading data to s3 |
| collections | Map<str, str> | A mapping of output collection name to a JSONPath pattern (for matching Items) |
| s3_urls | bool | Controls if the final published URLs should be an s3 (s3://*bucket*/*key*) or https URL |

##### path_template

The path_template string is a way to control the output location of uploaded assets from a STAC Item using metadata from the Item itself.
The template can contain fixed strings along with variables used for substitution.
See [the PySTAC documentation for `LayoutTemplate`](https://pystac.readthedocs.io/en/stable/api/layout.html#pystac.layout.LayoutTemplate) for a list of supported template variables and their meaning.

##### collections

The collections dictionary provides a collection ID and JSONPath pattern for matching against STAC Items.
At the end of processing, before the final STAC Items are returned, the Task class can be used to assign
all of the Items to specific collection IDs. For each Item the JSONPath pattern for all collections will be
compared. The first match will cause the Item's Collection ID to be set to the provided value.

For example:

```json
"collections": {
"landsat-c2l2": "$[?(@.id =~ 'LC08.*')]"
}
```

In this example, the task will set any STAC Items that have an ID beginning with "LC08" to the `landsat-c2l2` collection.

See [Jayway JsonPath Evaluator](https://jsonpath.herokuapp.com/) to experiment with JSONpath and [regex101](https://regex101.com/) to experiment with regex.

#### tasks

The tasks field is a dictionary with an optional key for each task. If present, it contains
a dictionary that is converted to a set of keywords and passed to the Task's `process` function.
The documentation for each task will provide the list of available parameters.

```json
{
"tasks": {
"task-a": {
"param1": "value1"
},
"task-c": {
"param2": "value2"
}
}
}
```

In the example above a task named `task-a` would have the `param1=value1` passed as a keyword, while `task-c`
would have `param2=value2` passed. If there were a `task-b` to be run it would not be passed any keywords.

#### TaskConfig Object

**DEPRECATED**: `tasks` should be a dictionary of parameters, with task names as keys. See [tasks](#tasks) for more information.

A Task Configuration contains information for running a specific task.

| Field Name | Type | Description |
| ------------- | ---- | ----------- |
| name | str | **REQUIRED** Name of the task |
| parameters | Map<str, str> | Dictionary of keyword parameters that will be passed to the Tasks `process` function |

## Full Process Definition Example

Process definitions are sometimes called "Payloads":

```json
{
"description": "My process configuration",
"collections": {
"landsat-c2l2": "$[?(@.id =~ 'LC08.*')]"
},
"upload_options": {
"path_template": "s3://my-bucket/${collection}/${year}/${month}/${day}/${id}"
},
"tasks": {
"task-name": {
"param": "value"
}
}
}
```

## Development

Clone, install in editable mode with development requirements, and install the **pre-commit** hooks:

```shell
git clone https://github.com/stac-utils/stac-task
cd stac-task
pip install -e '.[dev]'
pre-commit install
```

To run the complete test and linting suite:

```shell
./scripts/test
```

To just run the tests:

```shell
pytest
```

## Contributing

Use Github [issues](https://github.com/stac-utils/stac-task/issues) and [pull requests](https://github.com/stac-utils/stac-task/pulls).
# stac-task
8 changes: 0 additions & 8 deletions codecov.yml

This file was deleted.

Binary file added examples/dataset_geo.tif
Binary file not shown.
13 changes: 13 additions & 0 deletions examples/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "FeatureCollection",
"features": [
{
"href": "./dataset_geo.tif"
}
],
"process": {
"tasks": {
"rio-stac": {}
}
}
}
12 changes: 12 additions & 0 deletions examples/rio_stac_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import rio_stac.stac
import stac_task
from pystac import Item
from stac_task import HrefTask


class RioStacTask(HrefTask):
def process_href(self, href: str) -> Item:
return rio_stac.stac.create_stac_item(href)


stac_task.register_task("rio-stac", RioStacTask)
Loading

0 comments on commit 5706d92

Please sign in to comment.