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
+ from typing import Any , Dict , List , Optional , TYPE_CHECKING , Tuple , Union , cast
6
4
7
- import pystac
5
+ import pystac as ps
8
6
7
+ if TYPE_CHECKING :
8
+ from pystac .stac_object import STACObject
9
+ from pystac .collection import Collection
9
10
10
- def get_cache_key (stac_object : STACObject ) -> Tuple [str , bool ]:
11
+
12
+ def get_cache_key (stac_object : "STACObject" ) -> Tuple [str , bool ]:
11
13
"""Produce a cache key for the given STAC object.
12
14
13
15
If a self href is set, use that as the cache key.
@@ -56,16 +58,16 @@ class ResolvedObjectCache:
56
58
ids_to_collections (Dict[str, Collection]): Map of collection IDs to collections.
57
59
"""
58
60
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 ):
61
+ id_keys_to_objects : Optional [Dict [str , " STACObject" ]] = None ,
62
+ hrefs_to_objects : Optional [Dict [str , " STACObject" ]] = None ,
63
+ ids_to_collections : Dict [str , " Collection" ] = None ):
62
64
self .id_keys_to_objects = id_keys_to_objects or {}
63
65
self .hrefs_to_objects = hrefs_to_objects or {}
64
66
self .ids_to_collections = ids_to_collections or {}
65
67
66
68
self ._collection_cache = None
67
69
68
- def get_or_cache (self , obj : STACObject ) -> STACObject :
70
+ def get_or_cache (self , obj : " STACObject" ) -> " STACObject" :
69
71
"""Gets the STACObject that is the cached version of the given STACObject; or, if
70
72
none exists, sets the cached object to the given object.
71
73
@@ -91,7 +93,7 @@ def get_or_cache(self, obj: STACObject) -> STACObject:
91
93
self .cache (obj )
92
94
return obj
93
95
94
- def get (self , obj : STACObject ) -> Optional [STACObject ]:
96
+ def get (self , obj : " STACObject" ) -> Optional [" STACObject" ]:
95
97
"""Get the cached object that has the same cache key as the given object.
96
98
97
99
Args:
@@ -107,7 +109,7 @@ def get(self, obj: STACObject) -> Optional[STACObject]:
107
109
else :
108
110
return self .id_keys_to_objects .get (key )
109
111
110
- def get_by_href (self , href : str ) -> Optional [STACObject ]:
112
+ def get_by_href (self , href : str ) -> Optional [" STACObject" ]:
111
113
"""Gets the cached object at href.
112
114
113
115
Args:
@@ -118,7 +120,7 @@ def get_by_href(self, href: str) -> Optional[STACObject]:
118
120
"""
119
121
return self .hrefs_to_objects .get (href )
120
122
121
- def get_collection_by_id (self , id : str ) -> Optional [Collection ]:
123
+ def get_collection_by_id (self , id : str ) -> Optional [" Collection" ]:
122
124
"""Retrieved a cached Collection by its ID.
123
125
124
126
Args:
@@ -130,7 +132,7 @@ def get_collection_by_id(self, id: str) -> Optional[Collection]:
130
132
"""
131
133
return self .ids_to_collections .get (id )
132
134
133
- def cache (self , obj : STACObject ) -> None :
135
+ def cache (self , obj : " STACObject" ) -> None :
134
136
"""Set the given object into the cache.
135
137
136
138
Args:
@@ -142,10 +144,10 @@ def cache(self, obj: STACObject) -> None:
142
144
else :
143
145
self .id_keys_to_objects [key ] = obj
144
146
145
- if isinstance (obj , Collection ):
147
+ if isinstance (obj , ps . Collection ):
146
148
self .ids_to_collections [obj .id ] = obj
147
149
148
- def remove (self , obj : STACObject ) -> None :
150
+ def remove (self , obj : " STACObject" ) -> None :
149
151
"""Removes any cached object that matches the given object's cache key.
150
152
151
153
Args:
@@ -158,10 +160,10 @@ def remove(self, obj: STACObject) -> None:
158
160
else :
159
161
self .id_keys_to_objects .pop (key , None )
160
162
161
- if obj .STAC_OBJECT_TYPE == pystac .STACObjectType .COLLECTION :
163
+ if obj .STAC_OBJECT_TYPE == ps .STACObjectType .COLLECTION :
162
164
self .id_keys_to_objects .pop (obj .id , None )
163
165
164
- def __contains__ (self , obj : STACObject ) -> bool :
166
+ def __contains__ (self , obj : " STACObject" ) -> bool :
165
167
key , is_href = get_cache_key (obj )
166
168
return key in self .hrefs_to_objects if is_href else key in self .id_keys_to_objects
167
169
@@ -213,23 +215,25 @@ class CollectionCache:
213
215
and will set Collection JSON that it reads in order to merge in common properties.
214
216
"""
215
217
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 ):
218
+ cached_ids : Dict [str , Union [" Collection" , Dict [str , Any ]]] = None ,
219
+ cached_hrefs : Dict [str , Union [" Collection" , Dict [str , Any ]]] = None ):
218
220
self .cached_ids = cached_ids or {}
219
221
self .cached_hrefs = cached_hrefs or {}
220
222
221
- def get_by_id (self , collection_id : str ) -> Optional [Union [Collection , Dict [str , Any ]]]:
223
+ def get_by_id (self , collection_id : str ) -> Optional [Union [" Collection" , Dict [str , Any ]]]:
222
224
return self .cached_ids .get (collection_id )
223
225
224
- def get_by_href (self , href : str ) -> Optional [Union [Collection , Dict [str , Any ]]]:
226
+ def get_by_href (self , href : str ) -> Optional [Union [" Collection" , Dict [str , Any ]]]:
225
227
return self .cached_hrefs .get (href )
226
228
227
229
def contains_id (self , collection_id : str ) -> bool :
228
230
return collection_id in self .cached_ids
229
231
230
- def cache (self , collection : Union [Collection , Dict [str , Any ]], href : Optional [str ] = None ) -> None :
232
+ def cache (self ,
233
+ collection : Union ["Collection" , Dict [str , Any ]],
234
+ href : Optional [str ] = None ) -> None :
231
235
"""Caches a collection JSON."""
232
- if isinstance (collection , Collection ):
236
+ if isinstance (collection , ps . Collection ):
233
237
self .cached_ids [collection .id ] = collection
234
238
else :
235
239
self .cached_ids [collection ['id' ]] = collection
@@ -241,24 +245,24 @@ def cache(self, collection: Union[Collection, Dict[str, Any]], href: Optional[st
241
245
class ResolvedObjectCollectionCache (CollectionCache ):
242
246
def __init__ (self ,
243
247
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 ):
248
+ cached_ids : Dict [str , Union [" Collection" , Dict [str , Any ]]] = None ,
249
+ cached_hrefs : Dict [str , Union [" Collection" , Dict [str , Any ]]] = None ):
246
250
super ().__init__ (cached_ids , cached_hrefs )
247
251
self .resolved_object_cache = resolved_object_cache
248
252
249
- def get_by_id (self , collection_id : str ) -> Optional [Union [Collection , Dict [str , Any ]]]:
253
+ def get_by_id (self , collection_id : str ) -> Optional [Union [" Collection" , Dict [str , Any ]]]:
250
254
result = self .resolved_object_cache .get_collection_by_id (collection_id )
251
255
if result is None :
252
256
return super ().get_by_id (collection_id )
253
257
else :
254
258
return result
255
259
256
- def get_by_href (self , href : str ) -> Optional [Union [Collection , Dict [str , Any ]]]:
260
+ def get_by_href (self , href : str ) -> Optional [Union [" Collection" , Dict [str , Any ]]]:
257
261
result = self .resolved_object_cache .get_by_href (href )
258
262
if result is None :
259
263
return super ().get_by_href (href )
260
264
else :
261
- return cast (Collection , result )
265
+ return cast (ps . Collection , result )
262
266
263
267
def contains_id (self , collection_id : str ) -> bool :
264
268
return (self .resolved_object_cache .contains_collection_id (collection_id )
0 commit comments