Skip to content

Add support for the version extension. #193

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

Merged
merged 15 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 6 additions & 4 deletions pystac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ class STACError(Exception):
from pystac import extensions
import pystac.extensions.eo
import pystac.extensions.label
import pystac.extensions.projection
import pystac.extensions.pointcloud
import pystac.extensions.view
import pystac.extensions.projection
import pystac.extensions.single_file_stac
import pystac.extensions.timestamps
import pystac.extensions.version
import pystac.extensions.view

STAC_EXTENSIONS = extensions.base.RegisteredSTACExtensions([
extensions.eo.EO_EXTENSION_DEFINITION, extensions.label.LABEL_EXTENSION_DEFINITION,
extensions.pointcloud.POINTCLOUD_EXTENSION_DEFINITION,
extensions.projection.PROJECTION_EXTENSION_DEFINITION,
extensions.view.VIEW_EXTENSION_DEFINITION, extensions.single_file_stac.SFS_EXTENSION_DEFINITION,
extensions.timestamps.TIMESTAMPS_EXTENSION_DEFINITION
extensions.single_file_stac.SFS_EXTENSION_DEFINITION,
extensions.timestamps.TIMESTAMPS_EXTENSION_DEFINITION,
extensions.version.VERSION_EXTENSION_DEFINITION, extensions.view.VIEW_EXTENSION_DEFINITION
])


Expand Down
170 changes: 170 additions & 0 deletions pystac/extensions/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
"""Implement the version extension.

https://github.com/radiantearth/stac-spec/tree/dev/extensions/version
"""

import pystac
from pystac import collection
from pystac import Extensions
from pystac import item
from pystac import link
from pystac.extensions import base

LATEST_VERSION = 'latest-version'
PREDECESSOR_VERSION = 'predecessor-version'
SUCCESSOR_VERSION = 'successor-version'

# Media type for links.
MEDIA_TYPE = 'application/json'


class VersionItemExt(base.ItemExtension):
"""Add an asset version string to a STAC Item."""
def __init__(self, an_item):
self.item = an_item

def apply(self, version, deprecated=None, latest=None, predecessor=None, successor=None):
self.version = version
if deprecated is not None:
self.deprecated = deprecated
if latest:
self.latest_link = latest
if predecessor:
self.predecessor_link = predecessor
if successor:
self.successor_link = successor

@property
def version(self):
return self.item.properties.get('version')

@version.setter
def version(self, v):
self.item.properties['version'] = v

@property
def deprecated(self):
return bool(self.item.properties.get('deprecated'))

@deprecated.setter
def deprecated(self, v):
if not isinstance(v, bool):
raise pystac.STACError('deprecated must be a bool')
self.item.properties['deprecated'] = v

@property
def latest_link(self):
links = self.item.get_links(LATEST_VERSION)
if links:
return links[0]

@latest_link.setter
def latest_link(self, source_item):
self.item.add_link(link.Link(LATEST_VERSION, source_item, MEDIA_TYPE))

@property
def predecessor_link(self):
links = self.item.get_links(PREDECESSOR_VERSION)
if links:
return links[0]

@predecessor_link.setter
def predecessor_link(self, source_item):
self.item.add_link(link.Link(PREDECESSOR_VERSION, source_item, MEDIA_TYPE))

@property
def successor_link(self):
links = self.item.get_links(SUCCESSOR_VERSION)
if links:
return links[0]

@successor_link.setter
def successor_link(self, source_item):
self.item.add_link(link.Link(SUCCESSOR_VERSION, source_item, MEDIA_TYPE))

@classmethod
def from_item(cls, an_item):
return cls(an_item)

@classmethod
def _object_links(cls):
return [] # TODO(schwehr): What should this return?
Copy link
Member

Choose a reason for hiding this comment

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

This method returns any STAC Objects that are linked to by the extension properties. That way, PySTAC knows that if can resolve links and should check if a link to a referenced object exists in the resolved object cache.

In this case, this should return any of the successor, predecessor, and latest_version links, e.g.

Suggested change
return [] # TODO(schwehr): What should this return?
link_types = [SUCCESSOR_VERSION, PREDECESSOR_VERSION, LATEST_VERSION]
return [link for link in self.item.get_links() if link.rel in link_types]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What call triggers this? This already returns the linked item

print('objects latest',
          [x for x in self.item.get_stac_objects(version.LATEST_VERSION)])

Your example has self.item, but this is a @classmethod with cls as the arg. I'm not following.

Copy link
Member

Choose a reason for hiding this comment

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

My bad, forgot this was a classmethod. It should return the rel types that point to stac objects, not the actual links.

So return [SUCCESSOR_VERSION, PREDECESSOR_VERSION, LATEST_VERSION] should work for this case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there a reasonably direct way to test this? I switched the order the be alphabetical. If there was a specific reason for the order you listed, I will change it.

I'm going to look at the catalog copy test now.

Copy link
Member

Choose a reason for hiding this comment

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

No reason for the ordering, so alphabetical is great!

Not a very direct test; the full_copy logic is really what this is used for so the more complicated test is what will really ensure its working.



class VersionCollectionExt(base.CollectionExtension):
"""Add an asset version string to a STAC Collection."""
def __init__(self, a_collection):
self.collection = a_collection

@property
def version(self):
return self.collection.extra_fields.get('version')

@version.setter
def version(self, v):
self.collection.extra_fields['version'] = v

@property
def deprecated(self):
return bool(self.collection.extra_fields.get('deprecated'))

@deprecated.setter
def deprecated(self, v):
if not isinstance(v, bool):
raise pystac.STACError('deprecated must be a bool')
self.collection.extra_fields['deprecated'] = v

@property
def latest_link(self):
links = self.collection.get_links(LATEST_VERSION)
if links:
return links[0]

@latest_link.setter
def latest_link(self, source_collection):
self.collection.add_link(link.Link(LATEST_VERSION, source_collection, MEDIA_TYPE))

@property
def predecessor_link(self):
links = self.collection.get_links(PREDECESSOR_VERSION)
if links:
return links[0]

@predecessor_link.setter
def predecessor_link(self, source_collection):
self.collection.add_link(link.Link(PREDECESSOR_VERSION, source_collection, MEDIA_TYPE))

@property
def successor_link(self):
links = self.collection.get_links(SUCCESSOR_VERSION)
if links:
return links[0]

@successor_link.setter
def successor_link(self, source_collection):
self.collection.add_link(link.Link(SUCCESSOR_VERSION, source_collection, MEDIA_TYPE))

@classmethod
def from_collection(cls, a_collection):
return cls(a_collection)

@classmethod
def _object_links(cls):
return [] # TODO(schwehr): What should this return?

def apply(self, version, deprecated=None, latest=None, predecessor=None, successor=None):
self.version = version
if deprecated is not None:
self.deprecated = deprecated
if latest:
self.latest_link = latest
if predecessor:
self.predecessor_link = predecessor
if successor:
self.successor_link = successor


VERSION_EXTENSION_DEFINITION = base.ExtensionDefinition(Extensions.VERSION, [
base.ExtendedObject(item.Item, VersionItemExt),
base.ExtendedObject(collection.Collection, VersionCollectionExt)
])
Loading