@@ -174,6 +174,10 @@ def pointer(self, pointer: str, resolver: Resolver[D]) -> Resolved[D]:
174
174
return Resolved (contents = contents , resolver = resolver ) # type: ignore[reportUnknownArgumentType] # noqa: E501
175
175
176
176
177
+ def _fail_to_retrieve (uri : URI ):
178
+ raise exceptions .NoSuchResource (ref = uri )
179
+
180
+
177
181
@frozen
178
182
class Registry (Mapping [URI , Resource [D ]]):
179
183
r"""
@@ -190,17 +194,32 @@ class Registry(Mapping[URI, Resource[D]]):
190
194
191
195
Registries are immutable, and their methods return new instances of the
192
196
registry with the additional resources added to them.
197
+
198
+ The ``retrieve`` argument can be used to configure retrieval of resources
199
+ dynamically, either over the network, from a database, or the like.
200
+ Pass it a callable which will be called if any URI not present in the
201
+ registry is accessed. It must either return a `Resource` or else raise an
202
+ exception indicating that the resource is not retrievable.
193
203
"""
194
204
195
205
_resources : PMap [URI , Resource [D ]] = field (default = m (), converter = pmap ) # type: ignore[reportUnknownArgumentType] # noqa: E501
196
206
_anchors : PMap [tuple [URI , str ], AnchorType [D ]] = field (default = m ()) # type: ignore[reportUnknownArgumentType] # noqa: E501
197
207
_uncrawled : PSet [URI ] = field (default = s ()) # type: ignore[reportUnknownArgumentType] # noqa: E501
208
+ _retrieve : Callable [[URI ], Resource [D ]] = field (default = _fail_to_retrieve )
198
209
199
210
def __getitem__ (self , uri : URI ) -> Resource [D ]:
200
211
"""
201
212
Return the `Resource` identified by the given URI.
202
213
"""
203
- return self ._resources [uri ]
214
+ try :
215
+ return self ._resources [uri ]
216
+ except LookupError :
217
+ try :
218
+ return self ._retrieve (uri )
219
+ except exceptions .NoSuchResource :
220
+ raise
221
+ except Exception :
222
+ raise exceptions .NoSuchResource (ref = uri )
204
223
205
224
def __iter__ (self ) -> Iterator [URI ]:
206
225
"""
@@ -393,7 +412,7 @@ def lookup(self, ref: URI) -> Resolved[D]:
393
412
registry = registry .crawl ()
394
413
try :
395
414
resource = registry [uri ]
396
- except KeyError :
415
+ except exceptions . NoSuchResource :
397
416
raise exceptions .Unresolvable (ref = ref ) from None
398
417
399
418
if fragment .startswith ("/" ):
0 commit comments