-
Notifications
You must be signed in to change notification settings - Fork 113
/
timestamps.py
288 lines (231 loc) · 10.4 KB
/
timestamps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""Implements the :stac-ext:`Timestamps Extension <timestamps>`."""
from datetime import datetime as datetime
from pystac.summaries import RangeSummary
from typing import Dict, Any, Iterable, Generic, Optional, TypeVar, Union, cast
import pystac
from pystac.extensions.base import (
ExtensionManagementMixin,
PropertiesExtension,
SummariesExtension,
)
from pystac.extensions.hooks import ExtensionHooks
from pystac.utils import datetime_to_str, map_opt, str_to_datetime
T = TypeVar("T", pystac.Item, pystac.Asset)
SCHEMA_URI = "https://stac-extensions.github.io/timestamps/v1.0.0/schema.json"
PUBLISHED_PROP = "published"
EXPIRES_PROP = "expires"
UNPUBLISHED_PROP = "unpublished"
class TimestampsExtension(
Generic[T],
PropertiesExtension,
ExtensionManagementMixin[Union[pystac.Item, pystac.Collection]],
):
"""An abstract class that can be used to extend the properties of an
:class:`~pystac.Item` or :class:`~pystac.Asset` with properties from the
:stac-ext:`Timestamps Extension <timestamps>`. This class is generic over the type
of STAC Object to be extended (e.g. :class:`~pystac.Item`, :class:`~pystac.Asset`).
To create a concrete instance of :class:`TimestampsExtension`, use the
:meth:`TimestampsExtension.ext` method. For example:
.. code-block:: python
>>> item: pystac.Item = ...
>>> ts_ext = TimestampsExtension.ext(item)
"""
def apply(
self,
published: Optional[datetime] = None,
expires: Optional[datetime] = None,
unpublished: Optional[datetime] = None,
) -> None:
"""Applies timestamps extension properties to the extended Item.
Args:
published : Date and time the corresponding data
was published the first time.
expires : Date and time the corresponding data
expires (is not valid any longer).
unpublished : Date and time the corresponding data
was unpublished.
"""
self.published = published
self.expires = expires
self.unpublished = unpublished
@property
def published(self) -> Optional[datetime]:
"""Gets or sets a datetime object that represents the date and time that the
corresponding data was published the first time.
'Published' has a different meaning depending on where it is used. If available
in the asset properties, it refers to the timestamps valid for the actual data
linked to the Asset Object. If it comes from the Item properties, it refers to
timestamp valid for the metadata.
"""
return map_opt(str_to_datetime, self._get_property(PUBLISHED_PROP, str))
@published.setter
def published(self, v: Optional[datetime]) -> None:
self._set_property(PUBLISHED_PROP, map_opt(datetime_to_str, v))
@property
def expires(self) -> Optional[datetime]:
"""Gets or sets a datetime object that represents the date and time the
corresponding data expires (is not valid any longer).
'Unpublished' has a different meaning depending on where it is used. If
available in the asset properties, it refers to the timestamps valid for the
actual data linked to the Asset Object. If it comes from the Item properties,
it refers to to the timestamp valid for the metadata.
"""
return map_opt(str_to_datetime, self._get_property(EXPIRES_PROP, str))
@expires.setter
def expires(self, v: Optional[datetime]) -> None:
self._set_property(EXPIRES_PROP, map_opt(datetime_to_str, v))
@property
def unpublished(self) -> Optional[datetime]:
"""Gets or sets a datetime object that represents the date and time the
corresponding data was unpublished.
'Unpublished' has a different meaning depending on where it is used. If
available in the asset properties, it refers to the timestamps valid for the
actual data linked to the Asset Object. If it comes from the Item properties,
it's referencing to the timestamp valid for the metadata.
"""
return map_opt(str_to_datetime, self._get_property(UNPUBLISHED_PROP, str))
@unpublished.setter
def unpublished(self, v: Optional[datetime]) -> None:
self._set_property(UNPUBLISHED_PROP, map_opt(datetime_to_str, v))
@classmethod
def get_schema_uri(cls) -> str:
return SCHEMA_URI
@classmethod
def ext(cls, obj: T, add_if_missing: bool = False) -> "TimestampsExtension[T]":
"""Extends the given STAC Object with properties from the :stac-ext:`Timestamps
Extension <timestamps>`.
This extension can be applied to instances of :class:`~pystac.Item` or
:class:`~pystac.Asset`.
Raises:
pystac.ExtensionTypeError : If an invalid object type is passed.
"""
if isinstance(obj, pystac.Item):
cls.validate_has_extension(obj, add_if_missing)
return cast(TimestampsExtension[T], ItemTimestampsExtension(obj))
elif isinstance(obj, pystac.Asset):
cls.validate_owner_has_extension(obj, add_if_missing)
return cast(TimestampsExtension[T], AssetTimestampsExtension(obj))
else:
raise pystac.ExtensionTypeError(
f"Timestamps extension does not apply to type '{type(obj).__name__}'"
)
@classmethod
def summaries(
cls, obj: pystac.Collection, add_if_missing: bool = False
) -> "SummariesTimestampsExtension":
"""Returns the extended summaries object for the given collection."""
cls.validate_has_extension(obj, add_if_missing)
return SummariesTimestampsExtension(obj)
class ItemTimestampsExtension(TimestampsExtension[pystac.Item]):
"""A concrete implementation of :class:`TimestampsExtension` on an
:class:`~pystac.Item` that extends the properties of the Item to include properties
defined in the :stac-ext:`Timestamps Extension <timestamps>`.
This class should generally not be instantiated directly. Instead, call
:meth:`TimestampsExtension.ext` on an :class:`~pystac.Item` to extend it.
"""
item: pystac.Item
"""The :class:`~pystac.Item` being extended."""
properties: Dict[str, Any]
"""The :class:`~pystac.Item` properties, including extension properties."""
def __init__(self, item: pystac.Item):
self.item = item
self.properties = item.properties
def __repr__(self) -> str:
return "<ItemTimestampsExtension Item id={}>".format(self.item.id)
class AssetTimestampsExtension(TimestampsExtension[pystac.Asset]):
"""A concrete implementation of :class:`TimestampsExtension` on an
:class:`~pystac.Asset` that extends the Asset fields to include properties
defined in the :stac-ext:`Timestamps Extension <timestamps>`.
This class should generally not be instantiated directly. Instead, call
:meth:`TimestampsExtension.ext` on an :class:`~pystac.Asset` to extend it.
"""
asset_href: str
"""The ``href`` value of the :class:`~pystac.Asset` being extended."""
properties: Dict[str, Any]
"""The :class:`~pystac.Asset` fields, including extension properties."""
additional_read_properties: Optional[Iterable[Dict[str, Any]]] = None
"""If present, this will be a list containing 1 dictionary representing the
properties of the owning :class:`~pystac.Item`."""
def __init__(self, asset: pystac.Asset):
self.asset_href = asset.href
self.properties = asset.extra_fields
if asset.owner and isinstance(asset.owner, pystac.Item):
self.additional_read_properties = [asset.owner.properties]
def __repr__(self) -> str:
return "<AssetTimestampsExtension Asset href={}>".format(self.asset_href)
class SummariesTimestampsExtension(SummariesExtension):
"""A concrete implementation of :class:`~SummariesExtension` that extends
the ``summaries`` field of a :class:`~pystac.Collection` to include properties
defined in the :stac-ext:`Timestamps Extension <timestamps>`.
"""
@property
def published(self) -> Optional[RangeSummary[datetime]]:
"""Get or sets the summary of :attr:`TimestampsExtension.published` values
for this Collection.
"""
return map_opt(
lambda s: RangeSummary(
str_to_datetime(s.minimum), str_to_datetime(s.maximum)
),
self.summaries.get_range(PUBLISHED_PROP),
)
@published.setter
def published(self, v: Optional[RangeSummary[datetime]]) -> None:
self._set_summary(
PUBLISHED_PROP,
map_opt(
lambda s: RangeSummary(
datetime_to_str(s.minimum), datetime_to_str(s.maximum)
),
v,
),
)
@property
def expires(self) -> Optional[RangeSummary[datetime]]:
"""Get or sets the summary of :attr:`TimestampsExtension.expires` values
for this Collection.
"""
return map_opt(
lambda s: RangeSummary(
str_to_datetime(s.minimum), str_to_datetime(s.maximum)
),
self.summaries.get_range(EXPIRES_PROP),
)
@expires.setter
def expires(self, v: Optional[RangeSummary[datetime]]) -> None:
self._set_summary(
EXPIRES_PROP,
map_opt(
lambda s: RangeSummary(
datetime_to_str(s.minimum), datetime_to_str(s.maximum)
),
v,
),
)
@property
def unpublished(self) -> Optional[RangeSummary[datetime]]:
"""Get or sets the summary of :attr:`TimestampsExtension.unpublished` values
for this Collection.
"""
return map_opt(
lambda s: RangeSummary(
str_to_datetime(s.minimum), str_to_datetime(s.maximum)
),
self.summaries.get_range(UNPUBLISHED_PROP),
)
@unpublished.setter
def unpublished(self, v: Optional[RangeSummary[datetime]]) -> None:
self._set_summary(
UNPUBLISHED_PROP,
map_opt(
lambda s: RangeSummary(
datetime_to_str(s.minimum), datetime_to_str(s.maximum)
),
v,
),
)
class TimestampsExtensionHooks(ExtensionHooks):
schema_uri: str = SCHEMA_URI
prev_extension_ids = {"timestamps"}
stac_object_types = {pystac.STACObjectType.ITEM}
TIMESTAMPS_EXTENSION_HOOKS: ExtensionHooks = TimestampsExtensionHooks()