-
Notifications
You must be signed in to change notification settings - Fork 123
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
Changes from 5 commits
67a3109
36b4183
8753317
4db4048
a5590e9
91cf8e8
c34eeb1
beac9dd
566c200
b35cd64
8ea0f91
d408d2e
56e9033
974dac8
fbd7abd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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? | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What call triggers this? This already returns the linked item
Your example has self.item, but this is a @classmethod with cls as the arg. I'm not following. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, forgot this was a classmethod. It should return the So There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||
|
||||||||
|
||||||||
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') | ||||||||
schwehr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
@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) | ||||||||
]) |
Uh oh!
There was an error while loading. Please reload this page.