Skip to content

Commit 6acd62e

Browse files
committed
Add a way to drop resources from a registry.
Will be needed if/when #19 is done.
1 parent c1a9ce8 commit 6acd62e

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

referencing/_core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ def __repr__(self) -> str:
246246
summary = f"{pluralized}"
247247
return f"<Registry ({size} {summary})>"
248248

249+
def remove(self, uri: URI):
250+
"""
251+
Return a registry with the resource identified by a given URI removed.
252+
"""
253+
if uri not in self._resources:
254+
raise exceptions.NoSuchResource(ref=uri)
255+
256+
return evolve(
257+
self,
258+
resources=self._resources.remove(uri),
259+
uncrawled=self._uncrawled.discard(uri),
260+
anchors=pmap(
261+
(k, v) for k, v in self._anchors.items() if k[0] != uri
262+
),
263+
)
264+
249265
def anchor(self, uri: URI, name: str):
250266
"""
251267
Retrieve the given anchor, which must already have been found.

referencing/tests/test_core.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,48 @@ def bar_retrieve(uri):
338338
with pytest.raises(Exception, match="conflict.*retriev"): # noqa: B017
339339
first.combine(second, third)
340340

341+
def test_remove(self):
342+
one = Resource.opaque(contents={})
343+
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
344+
registry = Registry({"urn:foo": one, "urn:bar": two})
345+
assert registry.remove("urn:foo") == Registry({"urn:bar": two})
346+
347+
def test_remove_uncrawled(self):
348+
one = Resource.opaque(contents={})
349+
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})
350+
registry = Registry().with_resources(
351+
[("urn:foo", one), ("urn:bar", two)],
352+
)
353+
assert registry.remove("urn:foo") == Registry().with_resource(
354+
"urn:bar",
355+
two,
356+
)
357+
358+
def test_remove_with_anchors(self):
359+
one = Resource.opaque(contents={})
360+
two = ID_AND_CHILDREN.create_resource({"anchors": {"foo": "bar"}})
361+
registry = (
362+
Registry()
363+
.with_resources(
364+
[("urn:foo", one), ("urn:bar", two)],
365+
)
366+
.crawl()
367+
)
368+
assert (
369+
registry.remove("urn:bar")
370+
== Registry()
371+
.with_resource(
372+
"urn:foo",
373+
one,
374+
)
375+
.crawl()
376+
)
377+
378+
def test_remove_nonexistent_uri(self):
379+
with pytest.raises(exceptions.NoSuchResource) as e:
380+
Registry().remove("urn:doesNotExist")
381+
assert e.value == exceptions.NoSuchResource(ref="urn:doesNotExist")
382+
341383
def test_repr(self):
342384
one = Resource.opaque(contents={})
343385
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})

0 commit comments

Comments
 (0)