47
47
48
48
49
49
class LoadingOptions :
50
-
51
50
idx : IdxType
52
51
fileuri : Optional [str ]
53
52
baseuri : str
@@ -59,6 +58,8 @@ class LoadingOptions:
59
58
vocab : Dict [str , str ]
60
59
rvocab : Dict [str , str ]
61
60
cache : CacheType
61
+ imports : List [str ]
62
+ includes : List [str ]
62
63
63
64
def __init__ (
64
65
self ,
@@ -71,9 +72,10 @@ def __init__(
71
72
addl_metadata : Optional [Dict [str , str ]] = None ,
72
73
baseuri : Optional [str ] = None ,
73
74
idx : Optional [IdxType ] = None ,
75
+ imports : Optional [List [str ]] = None ,
76
+ includes : Optional [List [str ]] = None ,
74
77
) -> None :
75
78
"""Create a LoadingOptions object."""
76
-
77
79
self .original_doc = original_doc
78
80
79
81
if idx is not None :
@@ -106,6 +108,16 @@ def __init__(
106
108
else :
107
109
self .addl_metadata = copyfrom .addl_metadata if copyfrom is not None else {}
108
110
111
+ if imports is not None :
112
+ self .imports = imports
113
+ else :
114
+ self .imports = copyfrom .imports if copyfrom is not None else []
115
+
116
+ if includes is not None :
117
+ self .includes = includes
118
+ else :
119
+ self .includes = copyfrom .includes if copyfrom is not None else []
120
+
109
121
if fetcher is not None :
110
122
self .fetcher = fetcher
111
123
elif copyfrom is not None :
@@ -151,24 +163,29 @@ def graph(self) -> Graph:
151
163
if self .fileuri is not None
152
164
else pathlib .Path (schema ).resolve ().as_uri ()
153
165
)
154
- try :
155
- if fetchurl not in self . cache or self . cache [ fetchurl ] is True :
156
- _logger . debug ( "Getting external schema %s" , fetchurl )
166
+ if fetchurl not in self . cache or self . cache [ fetchurl ] is True :
167
+ _logger . debug ( "Getting external schema %s" , fetchurl )
168
+ try :
157
169
content = self .fetcher .fetch_text (fetchurl )
158
- self .cache [fetchurl ] = newGraph = Graph ()
159
- for fmt in ["xml" , "turtle" ]:
160
- try :
161
- newGraph .parse (
162
- data = content , format = fmt , publicID = str (fetchurl )
163
- )
164
- break
165
- except (xml .sax .SAXParseException , TypeError , BadSyntax ):
166
- pass
167
- graph += self .cache [fetchurl ]
168
- except Exception as e :
169
- _logger .warning (
170
- "Could not load extension schema %s: %s" , fetchurl , str (e )
171
- )
170
+ except Exception as e :
171
+ _logger .warning (
172
+ "Could not load extension schema %s: %s" , fetchurl , str (e )
173
+ )
174
+ continue
175
+ newGraph = Graph ()
176
+ err_msg = "unknown error"
177
+ for fmt in ["xml" , "turtle" ]:
178
+ try :
179
+ newGraph .parse (data = content , format = fmt , publicID = str (fetchurl ))
180
+ self .cache [fetchurl ] = newGraph
181
+ graph += newGraph
182
+ break
183
+ except (xml .sax .SAXParseException , TypeError , BadSyntax ) as e :
184
+ err_msg = str (e )
185
+ else :
186
+ _logger .warning (
187
+ "Could not load extension schema %s: %s" , fetchurl , err_msg
188
+ )
172
189
self .cache [key ] = graph
173
190
return graph
174
191
@@ -200,18 +217,22 @@ def load_field(val, fieldtype, baseuri, loadingOptions):
200
217
if "$import" in val :
201
218
if loadingOptions .fileuri is None :
202
219
raise SchemaSaladException ("Cannot load $import without fileuri" )
220
+ url = loadingOptions .fetcher .urljoin (loadingOptions .fileuri , val ["$import" ])
203
221
result , metadata = _document_load_by_url (
204
222
fieldtype ,
205
- loadingOptions . fetcher . urljoin ( loadingOptions . fileuri , val [ "$import" ]) ,
223
+ url ,
206
224
loadingOptions ,
207
225
)
226
+ loadingOptions .imports .append (url )
208
227
return result
209
228
elif "$include" in val :
210
229
if loadingOptions .fileuri is None :
211
230
raise SchemaSaladException ("Cannot load $import without fileuri" )
212
- val = loadingOptions .fetcher .fetch_text (
213
- loadingOptions .fetcher . urljoin ( loadingOptions . fileuri , val ["$include" ])
231
+ url = loadingOptions .fetcher .urljoin (
232
+ loadingOptions .fileuri , val ["$include" ]
214
233
)
234
+ val = loadingOptions .fetcher .fetch_text (url )
235
+ loadingOptions .includes .append (url )
215
236
return fieldtype .load (val , baseuri , loadingOptions )
216
237
217
238
@@ -296,7 +317,10 @@ def expand_url(
296
317
split = urlsplit (url )
297
318
298
319
if (
299
- (bool (split .scheme ) and split .scheme in ["http" , "https" , "file" ])
320
+ (
321
+ bool (split .scheme )
322
+ and split .scheme in loadingOptions .fetcher .supported_schemes ()
323
+ )
300
324
or url .startswith ("$(" )
301
325
or url .startswith ("${" )
302
326
):
@@ -382,7 +406,7 @@ def __init__(self, items):
382
406
def load (self , doc , baseuri , loadingOptions , docRoot = None ):
383
407
# type: (Any, str, LoadingOptions, Optional[str]) -> Any
384
408
if not isinstance (doc , MutableSequence ):
385
- raise ValidationException ("Expected a list, was {}" . format ( type (doc )) )
409
+ raise ValidationException (f "Expected a list, was { type (doc )} " )
386
410
r = [] # type: List[Any]
387
411
errors = [] # type: List[SchemaSaladException]
388
412
for i in range (0 , len (doc )):
@@ -504,7 +528,7 @@ def __init__(self, classtype):
504
528
def load (self , doc , baseuri , loadingOptions , docRoot = None ):
505
529
# type: (Any, str, LoadingOptions, Optional[str]) -> Any
506
530
if not isinstance (doc , MutableMapping ):
507
- raise ValidationException ("Expected a dict, was {}" . format ( type (doc )) )
531
+ raise ValidationException (f "Expected a dict, was { type (doc )} " )
508
532
return self .classtype .fromDoc (doc , baseuri , loadingOptions , docRoot = docRoot )
509
533
510
534
def __repr__ (self ): # type: () -> str
@@ -518,7 +542,7 @@ def __init__(self, items: Type[str]) -> None:
518
542
def load (self , doc , baseuri , loadingOptions , docRoot = None ):
519
543
# type: (Any, str, LoadingOptions, Optional[str]) -> Any
520
544
if not isinstance (doc , str ):
521
- raise ValidationException ("Expected a str, was {}" . format ( type (doc )) )
545
+ raise ValidationException (f "Expected a str, was { type (doc )} " )
522
546
return doc
523
547
524
548
0 commit comments