4
4
from urllib .parse import unquote , urldefrag , urljoin
5
5
6
6
from attrs import evolve , field
7
- from pyrsistent import m , plist , s
7
+ from pyrsistent import m , plist , pmap , s
8
8
from pyrsistent .typing import PList , PMap , PSet
9
9
10
10
from referencing ._attrs import define , frozen
@@ -178,16 +178,6 @@ def with_identified_resources(
178
178
uncrawled = uncrawled .persistent (),
179
179
)
180
180
181
- def _with_anchors (
182
- self ,
183
- uri : str ,
184
- anchors : Iterable [AnchorType ],
185
- ) -> Registry :
186
- assert uri .endswith ("#" ) or "#" not in uri , uri
187
- resource , old = self ._contents [uri ]
188
- new = old .update ({anchor .name : anchor for anchor in anchors })
189
- return evolve (self , contents = self ._contents .set (uri , (resource , new )))
190
-
191
181
def resource_at (
192
182
self ,
193
183
uri : str ,
@@ -199,26 +189,25 @@ def resource_at(
199
189
return * at_uri , self
200
190
201
191
def _crawl (self ) -> Registry :
202
- registry = self
203
- resources = [(uri , self . _contents [uri ][0 ]) for uri in self ._uncrawled ]
192
+ contents = self . _contents . evolver ()
193
+ resources = [(uri , contents [uri ][0 ]) for uri in self ._uncrawled ]
204
194
while resources :
205
195
base_uri , resource = resources .pop ()
196
+ anchors = pmap ((each .name , each ) for each in resource .anchors ())
197
+
206
198
id = resource .id ()
207
199
if id is None :
208
200
uri = base_uri
201
+
202
+ if anchors :
203
+ old , old_anchors = contents [uri ]
204
+ contents [uri ] = old , old_anchors .update (anchors )
209
205
else :
210
206
uri = urljoin (base_uri , id )
211
- registry = registry .with_identified_resource (
212
- uri = uri ,
213
- resource = resource ,
214
- )
215
-
216
- anchors = resource .anchors ()
217
- if anchors :
218
- registry = registry ._with_anchors (uri , anchors )
207
+ contents [uri ] = resource , anchors
219
208
220
209
resources .extend ((uri , each ) for each in resource .subresources ())
221
- return evolve (registry , uncrawled = s ())
210
+ return evolve (self , contents = contents . persistent () , uncrawled = s ())
222
211
223
212
def resolver (self , root : Schema , specification : Specification ) -> Resolver :
224
213
uri = specification .id_of (root ) or ""
0 commit comments