1
1
from collections import ChainMap
2
2
from copy import copy
3
+ from pystac .collection import Collection
4
+ from typing import Any , Dict , List , Optional , Tuple , Union , cast
5
+ from pystac .stac_object import STACObject
3
6
4
7
import pystac
5
8
6
9
7
- def get_cache_key (stac_object ) :
10
+ def get_cache_key (stac_object : STACObject ) -> Tuple [ str , bool ] :
8
11
"""Produce a cache key for the given STAC object.
9
12
10
13
If a self href is set, use that as the cache key.
@@ -20,7 +23,7 @@ def get_cache_key(stac_object):
20
23
if href is not None :
21
24
return (href , True )
22
25
else :
23
- ids = []
26
+ ids : List [ str ] = []
24
27
obj = stac_object
25
28
while obj is not None :
26
29
ids .append (obj .id )
@@ -52,14 +55,17 @@ class ResolvedObjectCache:
52
55
their cached object.
53
56
ids_to_collections (Dict[str, Collection]): Map of collection IDs to collections.
54
57
"""
55
- def __init__ (self , id_keys_to_objects = None , hrefs_to_objects = None , ids_to_collections = None ):
58
+ def __init__ (self ,
59
+ id_keys_to_objects : Optional [Dict [str , STACObject ]] = None ,
60
+ hrefs_to_objects : Optional [Dict [str , STACObject ]] = None ,
61
+ ids_to_collections : Dict [str , Collection ] = None ):
56
62
self .id_keys_to_objects = id_keys_to_objects or {}
57
63
self .hrefs_to_objects = hrefs_to_objects or {}
58
64
self .ids_to_collections = ids_to_collections or {}
59
65
60
66
self ._collection_cache = None
61
67
62
- def get_or_cache (self , obj ) :
68
+ def get_or_cache (self , obj : STACObject ) -> STACObject :
63
69
"""Gets the STACObject that is the cached version of the given STACObject; or, if
64
70
none exists, sets the cached object to the given object.
65
71
@@ -85,7 +91,7 @@ def get_or_cache(self, obj):
85
91
self .cache (obj )
86
92
return obj
87
93
88
- def get (self , obj ) :
94
+ def get (self , obj : STACObject ) -> Optional [ STACObject ] :
89
95
"""Get the cached object that has the same cache key as the given object.
90
96
91
97
Args:
@@ -101,7 +107,7 @@ def get(self, obj):
101
107
else :
102
108
return self .id_keys_to_objects .get (key )
103
109
104
- def get_by_href (self , href ) :
110
+ def get_by_href (self , href : str ) -> Optional [ STACObject ] :
105
111
"""Gets the cached object at href.
106
112
107
113
Args:
@@ -112,7 +118,7 @@ def get_by_href(self, href):
112
118
"""
113
119
return self .hrefs_to_objects .get (href )
114
120
115
- def get_collection_by_id (self , id ) :
121
+ def get_collection_by_id (self , id : str ) -> Optional [ Collection ] :
116
122
"""Retrieved a cached Collection by its ID.
117
123
118
124
Args:
@@ -124,7 +130,7 @@ def get_collection_by_id(self, id):
124
130
"""
125
131
return self .ids_to_collections .get (id )
126
132
127
- def cache (self , obj ) :
133
+ def cache (self , obj : STACObject ) -> None :
128
134
"""Set the given object into the cache.
129
135
130
136
Args:
@@ -136,10 +142,10 @@ def cache(self, obj):
136
142
else :
137
143
self .id_keys_to_objects [key ] = obj
138
144
139
- if obj . STAC_OBJECT_TYPE == pystac . STACObjectType . COLLECTION :
145
+ if isinstance ( obj , Collection ) :
140
146
self .ids_to_collections [obj .id ] = obj
141
147
142
- def remove (self , obj ) :
148
+ def remove (self , obj : STACObject ) -> None :
143
149
"""Removes any cached object that matches the given object's cache key.
144
150
145
151
Args:
@@ -155,21 +161,21 @@ def remove(self, obj):
155
161
if obj .STAC_OBJECT_TYPE == pystac .STACObjectType .COLLECTION :
156
162
self .id_keys_to_objects .pop (obj .id , None )
157
163
158
- def __contains__ (self , obj ) :
164
+ def __contains__ (self , obj : STACObject ) -> bool :
159
165
key , is_href = get_cache_key (obj )
160
166
return key in self .hrefs_to_objects if is_href else key in self .id_keys_to_objects
161
167
162
- def contains_collection_id (self , collection_id ) :
168
+ def contains_collection_id (self , collection_id : str ) -> bool :
163
169
"""Returns True if there is a collection with given collection ID is cached."""
164
170
return collection_id in self .ids_to_collections
165
171
166
- def as_collection_cache (self ):
172
+ def as_collection_cache (self ) -> "CollectionCache" :
167
173
if self ._collection_cache is None :
168
174
self ._collection_cache = ResolvedObjectCollectionCache (self )
169
175
return self ._collection_cache
170
176
171
177
@staticmethod
172
- def merge (first , second ) :
178
+ def merge (first : "ResolvedObjectCache" , second : "ResolvedObjectCache" ) -> "ResolvedObjectCache" :
173
179
"""Merges two ResolvedObjectCache.
174
180
175
181
The merged cache will give preference to the first argument; that is, if there
@@ -206,55 +212,65 @@ class CollectionCache:
206
212
The CollectionCache will contain collections as either as dicts or PySTAC Collections,
207
213
and will set Collection JSON that it reads in order to merge in common properties.
208
214
"""
209
- def __init__ (self , cached_ids = None , cached_hrefs = None ):
215
+ def __init__ (self ,
216
+ cached_ids : Dict [str , Union [Collection , Dict [str , Any ]]] = None ,
217
+ cached_hrefs : Dict [str , Union [Collection , Dict [str , Any ]]] = None ):
210
218
self .cached_ids = cached_ids or {}
211
219
self .cached_hrefs = cached_hrefs or {}
212
220
213
- def get_by_id (self , collection_id ) :
221
+ def get_by_id (self , collection_id : str ) -> Optional [ Union [ Collection , Dict [ str , Any ]]] :
214
222
return self .cached_ids .get (collection_id )
215
223
216
- def get_by_href (self , href ) :
224
+ def get_by_href (self , href : str ) -> Optional [ Union [ Collection , Dict [ str , Any ]]] :
217
225
return self .cached_hrefs .get (href )
218
226
219
- def contains_id (self , collection_id ) :
227
+ def contains_id (self , collection_id : str ) -> bool :
220
228
return collection_id in self .cached_ids
221
229
222
- def cache (self , collection , href = None ):
230
+ def cache (self , collection : Union [ Collection , Dict [ str , Any ]], href : Optional [ str ] = None ) -> None :
223
231
"""Caches a collection JSON."""
224
- self .cached_ids [collection ['id' ]] = collection
232
+ if isinstance (collection , Collection ):
233
+ self .cached_ids [collection .id ] = collection
234
+ else :
235
+ self .cached_ids [collection ['id' ]] = collection
225
236
226
237
if href is not None :
227
238
self .cached_hrefs [href ] = collection
228
239
229
240
230
241
class ResolvedObjectCollectionCache (CollectionCache ):
231
- def __init__ (self , resolved_object_cache , cached_ids = None , cached_hrefs = None ):
242
+ def __init__ (self ,
243
+ resolved_object_cache : ResolvedObjectCache ,
244
+ cached_ids : Dict [str , Union [Collection , Dict [str , Any ]]] = None ,
245
+ cached_hrefs : Dict [str , Union [Collection , Dict [str , Any ]]] = None ):
232
246
super ().__init__ (cached_ids , cached_hrefs )
233
247
self .resolved_object_cache = resolved_object_cache
234
248
235
- def get_by_id (self , collection_id ) :
249
+ def get_by_id (self , collection_id : str ) -> Optional [ Union [ Collection , Dict [ str , Any ]]] :
236
250
result = self .resolved_object_cache .get_collection_by_id (collection_id )
237
251
if result is None :
238
252
return super ().get_by_id (collection_id )
239
253
else :
240
254
return result
241
255
242
- def get_by_href (self , href ) :
256
+ def get_by_href (self , href : str ) -> Optional [ Union [ Collection , Dict [ str , Any ]]] :
243
257
result = self .resolved_object_cache .get_by_href (href )
244
258
if result is None :
245
259
return super ().get_by_href (href )
246
260
else :
247
- return result
261
+ return cast ( Collection , result )
248
262
249
- def contains_id (self , collection_id ) :
263
+ def contains_id (self , collection_id : str ) -> bool :
250
264
return (self .resolved_object_cache .contains_collection_id (collection_id )
251
265
or super ().contains_id (collection_id ))
252
266
253
- def cache (self , collection , href = None ):
267
+ def cache (self , collection : Dict [ str , Any ], href : Optional [ str ] = None ) -> None :
254
268
super ().cache (collection , href )
255
269
256
270
@staticmethod
257
- def merge (resolved_object_cache , first , second ):
271
+ def merge (resolved_object_cache : ResolvedObjectCache ,
272
+ first : Optional ["ResolvedObjectCollectionCache" ],
273
+ second : Optional ["ResolvedObjectCollectionCache" ]) -> "ResolvedObjectCollectionCache" :
258
274
first_cached_ids = {}
259
275
if first is not None :
260
276
first_cached_ids = copy (first .cached_ids )
0 commit comments