Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/schema_salad/metaschema.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 # | |
2 # This file was autogenerated using schema-salad-tool --codegen=python | |
3 # The code itself is released under the Apache 2.0 license and the help text is | |
4 # subject to the license of the original schema. | |
5 import copy | |
6 import os | |
7 import pathlib | |
8 import re | |
9 import tempfile | |
10 import uuid as _uuid__ # pylint: disable=unused-import # noqa: F401 | |
11 from io import StringIO | |
12 from typing import ( | |
13 Any, | |
14 Dict, | |
15 List, | |
16 MutableMapping, | |
17 MutableSequence, | |
18 Optional, | |
19 Sequence, | |
20 Tuple, | |
21 Type, | |
22 Union, | |
23 ) | |
24 from urllib.parse import quote, urlsplit, urlunsplit | |
25 from urllib.request import pathname2url | |
26 | |
27 from ruamel import yaml | |
28 from ruamel.yaml.comments import CommentedMap | |
29 | |
30 from schema_salad.exceptions import SchemaSaladException, ValidationException | |
31 from schema_salad.fetcher import DefaultFetcher, Fetcher | |
32 from schema_salad.sourceline import SourceLine, add_lc_filename | |
33 | |
34 _vocab = {} # type: Dict[str, str] | |
35 _rvocab = {} # type: Dict[str, str] | |
36 | |
37 | |
38 class Savable: | |
39 @classmethod | |
40 def fromDoc(cls, _doc, baseuri, loadingOptions, docRoot=None): | |
41 # type: (Any, str, LoadingOptions, Optional[str]) -> Savable | |
42 pass | |
43 | |
44 def save(self, top=False, base_url="", relative_uris=True): | |
45 # type: (bool, str, bool) -> Dict[str, str] | |
46 pass | |
47 | |
48 | |
49 class LoadingOptions: | |
50 def __init__( | |
51 self, | |
52 fetcher=None, # type: Optional[Fetcher] | |
53 namespaces=None, # type: Optional[Dict[str, str]] | |
54 schemas=None, # type: Optional[Dict[str, str]] | |
55 fileuri=None, # type: Optional[str] | |
56 copyfrom=None, # type: Optional[LoadingOptions] | |
57 original_doc=None, # type: Optional[Any] | |
58 ): # type: (...) -> None | |
59 self.idx = {} # type: Dict[str, Dict[str, Any]] | |
60 self.fileuri = fileuri # type: Optional[str] | |
61 self.namespaces = namespaces | |
62 self.schemas = schemas | |
63 self.original_doc = original_doc | |
64 if copyfrom is not None: | |
65 self.idx = copyfrom.idx | |
66 if fetcher is None: | |
67 fetcher = copyfrom.fetcher | |
68 if fileuri is None: | |
69 self.fileuri = copyfrom.fileuri | |
70 if namespaces is None: | |
71 self.namespaces = copyfrom.namespaces | |
72 if schemas is None: | |
73 self.schemas = copyfrom.schemas | |
74 | |
75 if fetcher is None: | |
76 import requests | |
77 from cachecontrol.caches import FileCache | |
78 from cachecontrol.wrapper import CacheControl | |
79 | |
80 root = pathlib.Path(os.environ.get("HOME", tempfile.gettempdir())) | |
81 session = CacheControl( | |
82 requests.Session(), | |
83 cache=FileCache(root / ".cache" / "salad"), | |
84 ) | |
85 self.fetcher: Fetcher = DefaultFetcher({}, session) | |
86 else: | |
87 self.fetcher = fetcher | |
88 | |
89 self.vocab = _vocab | |
90 self.rvocab = _rvocab | |
91 | |
92 if namespaces is not None: | |
93 self.vocab = self.vocab.copy() | |
94 self.rvocab = self.rvocab.copy() | |
95 for k, v in namespaces.items(): | |
96 self.vocab[k] = v | |
97 self.rvocab[v] = k | |
98 | |
99 | |
100 def load_field(val, fieldtype, baseuri, loadingOptions): | |
101 # type: (Union[str, Dict[str, str]], _Loader, str, LoadingOptions) -> Any | |
102 if isinstance(val, MutableMapping): | |
103 if "$import" in val: | |
104 if loadingOptions.fileuri is None: | |
105 raise SchemaSaladException("Cannot load $import without fileuri") | |
106 return _document_load_by_url( | |
107 fieldtype, | |
108 loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$import"]), | |
109 loadingOptions, | |
110 ) | |
111 elif "$include" in val: | |
112 if loadingOptions.fileuri is None: | |
113 raise SchemaSaladException("Cannot load $import without fileuri") | |
114 val = loadingOptions.fetcher.fetch_text( | |
115 loadingOptions.fetcher.urljoin(loadingOptions.fileuri, val["$include"]) | |
116 ) | |
117 return fieldtype.load(val, baseuri, loadingOptions) | |
118 | |
119 | |
120 save_type = Union[Dict[str, str], List[Union[Dict[str, str], List[Any], None]], None] | |
121 | |
122 | |
123 def save( | |
124 val, # type: Optional[Union[Savable, MutableSequence[Savable]]] | |
125 top=True, # type: bool | |
126 base_url="", # type: str | |
127 relative_uris=True, # type: bool | |
128 ): # type: (...) -> save_type | |
129 | |
130 if isinstance(val, Savable): | |
131 return val.save(top=top, base_url=base_url, relative_uris=relative_uris) | |
132 if isinstance(val, MutableSequence): | |
133 return [ | |
134 save(v, top=False, base_url=base_url, relative_uris=relative_uris) | |
135 for v in val | |
136 ] | |
137 if isinstance(val, MutableMapping): | |
138 newdict = {} | |
139 for key in val: | |
140 newdict[key] = save( | |
141 val[key], top=False, base_url=base_url, relative_uris=relative_uris | |
142 ) | |
143 return newdict | |
144 return val | |
145 | |
146 | |
147 def expand_url( | |
148 url, # type: str | |
149 base_url, # type: str | |
150 loadingOptions, # type: LoadingOptions | |
151 scoped_id=False, # type: bool | |
152 vocab_term=False, # type: bool | |
153 scoped_ref=None, # type: Optional[int] | |
154 ): | |
155 # type: (...) -> str | |
156 if url in ("@id", "@type"): | |
157 return url | |
158 | |
159 if vocab_term and url in loadingOptions.vocab: | |
160 return url | |
161 | |
162 if bool(loadingOptions.vocab) and ":" in url: | |
163 prefix = url.split(":")[0] | |
164 if prefix in loadingOptions.vocab: | |
165 url = loadingOptions.vocab[prefix] + url[len(prefix) + 1 :] | |
166 | |
167 split = urlsplit(url) | |
168 | |
169 if ( | |
170 (bool(split.scheme) and split.scheme in ["http", "https", "file"]) | |
171 or url.startswith("$(") | |
172 or url.startswith("${") | |
173 ): | |
174 pass | |
175 elif scoped_id and not bool(split.fragment): | |
176 splitbase = urlsplit(base_url) | |
177 frg = "" | |
178 if bool(splitbase.fragment): | |
179 frg = splitbase.fragment + "/" + split.path | |
180 else: | |
181 frg = split.path | |
182 pt = splitbase.path if splitbase.path != "" else "/" | |
183 url = urlunsplit((splitbase.scheme, splitbase.netloc, pt, splitbase.query, frg)) | |
184 elif scoped_ref is not None and not bool(split.fragment): | |
185 splitbase = urlsplit(base_url) | |
186 sp = splitbase.fragment.split("/") | |
187 n = scoped_ref | |
188 while n > 0 and len(sp) > 0: | |
189 sp.pop() | |
190 n -= 1 | |
191 sp.append(url) | |
192 url = urlunsplit( | |
193 ( | |
194 splitbase.scheme, | |
195 splitbase.netloc, | |
196 splitbase.path, | |
197 splitbase.query, | |
198 "/".join(sp), | |
199 ) | |
200 ) | |
201 else: | |
202 url = loadingOptions.fetcher.urljoin(base_url, url) | |
203 | |
204 if vocab_term: | |
205 split = urlsplit(url) | |
206 if bool(split.scheme): | |
207 if url in loadingOptions.rvocab: | |
208 return loadingOptions.rvocab[url] | |
209 else: | |
210 raise ValidationException(f"Term '{url}' not in vocabulary") | |
211 | |
212 return url | |
213 | |
214 | |
215 class _Loader: | |
216 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
217 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
218 pass | |
219 | |
220 | |
221 class _AnyLoader(_Loader): | |
222 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
223 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
224 if doc is not None: | |
225 return doc | |
226 raise ValidationException("Expected non-null") | |
227 | |
228 | |
229 class _PrimitiveLoader(_Loader): | |
230 def __init__(self, tp): | |
231 # type: (Union[type, Tuple[Type[str], Type[str]]]) -> None | |
232 self.tp = tp | |
233 | |
234 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
235 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
236 if not isinstance(doc, self.tp): | |
237 raise ValidationException( | |
238 "Expected a {} but got {}".format( | |
239 self.tp.__class__.__name__, doc.__class__.__name__ | |
240 ) | |
241 ) | |
242 return doc | |
243 | |
244 def __repr__(self): # type: () -> str | |
245 return str(self.tp) | |
246 | |
247 | |
248 class _ArrayLoader(_Loader): | |
249 def __init__(self, items): | |
250 # type: (_Loader) -> None | |
251 self.items = items | |
252 | |
253 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
254 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
255 if not isinstance(doc, MutableSequence): | |
256 raise ValidationException("Expected a list") | |
257 r = [] # type: List[Any] | |
258 errors = [] # type: List[SchemaSaladException] | |
259 for i in range(0, len(doc)): | |
260 try: | |
261 lf = load_field( | |
262 doc[i], _UnionLoader((self, self.items)), baseuri, loadingOptions | |
263 ) | |
264 if isinstance(lf, MutableSequence): | |
265 r.extend(lf) | |
266 else: | |
267 r.append(lf) | |
268 except ValidationException as e: | |
269 errors.append(e.with_sourceline(SourceLine(doc, i, str))) | |
270 if errors: | |
271 raise ValidationException("", None, errors) | |
272 return r | |
273 | |
274 def __repr__(self): # type: () -> str | |
275 return f"array<{self.items}>" | |
276 | |
277 | |
278 class _EnumLoader(_Loader): | |
279 def __init__(self, symbols): | |
280 # type: (Sequence[str]) -> None | |
281 self.symbols = symbols | |
282 | |
283 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
284 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
285 if doc in self.symbols: | |
286 return doc | |
287 else: | |
288 raise ValidationException(f"Expected one of {self.symbols}") | |
289 | |
290 | |
291 class _SecondaryDSLLoader(_Loader): | |
292 def __init__(self, inner): | |
293 # type: (_Loader) -> None | |
294 self.inner = inner | |
295 | |
296 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
297 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
298 r: List[Dict[str, Any]] = [] | |
299 if isinstance(doc, MutableSequence): | |
300 for d in doc: | |
301 if isinstance(d, str): | |
302 if d.endswith("?"): | |
303 r.append({"pattern": d[:-1], "required": False}) | |
304 else: | |
305 r.append({"pattern": d}) | |
306 elif isinstance(d, dict): | |
307 new_dict: Dict[str, Any] = {} | |
308 if "pattern" in d: | |
309 new_dict["pattern"] = d.pop("pattern") | |
310 else: | |
311 raise ValidationException( | |
312 "Missing pattern in secondaryFiles specification entry: {}".format( | |
313 d | |
314 ) | |
315 ) | |
316 new_dict["required"] = ( | |
317 d.pop("required") if "required" in d else None | |
318 ) | |
319 | |
320 if len(d): | |
321 raise ValidationException( | |
322 "Unallowed values in secondaryFiles specification entry: {}".format( | |
323 d | |
324 ) | |
325 ) | |
326 | |
327 else: | |
328 raise ValidationException( | |
329 "Expected a string or sequence of (strings or mappings)." | |
330 ) | |
331 elif isinstance(doc, str): | |
332 if doc.endswith("?"): | |
333 r.append({"pattern": doc[:-1], "required": False}) | |
334 else: | |
335 r.append({"pattern": doc}) | |
336 else: | |
337 raise ValidationException("Expected str or sequence of str") | |
338 return self.inner.load(r, baseuri, loadingOptions, docRoot) | |
339 | |
340 | |
341 class _RecordLoader(_Loader): | |
342 def __init__(self, classtype): | |
343 # type: (Type[Savable]) -> None | |
344 self.classtype = classtype | |
345 | |
346 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
347 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
348 if not isinstance(doc, MutableMapping): | |
349 raise ValidationException("Expected a dict") | |
350 return self.classtype.fromDoc(doc, baseuri, loadingOptions, docRoot=docRoot) | |
351 | |
352 def __repr__(self): # type: () -> str | |
353 return str(self.classtype) | |
354 | |
355 | |
356 class _ExpressionLoader(_Loader): | |
357 def __init__(self, items: Type[str]) -> None: | |
358 self.items = items | |
359 | |
360 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
361 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
362 if not isinstance(doc, str): | |
363 raise ValidationException("Expected a str") | |
364 return doc | |
365 | |
366 | |
367 class _UnionLoader(_Loader): | |
368 def __init__(self, alternates): | |
369 # type: (Sequence[_Loader]) -> None | |
370 self.alternates = alternates | |
371 | |
372 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
373 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
374 errors = [] | |
375 for t in self.alternates: | |
376 try: | |
377 return t.load(doc, baseuri, loadingOptions, docRoot=docRoot) | |
378 except ValidationException as e: | |
379 errors.append( | |
380 ValidationException(f"tried {t.__class__.__name__} but", None, [e]) | |
381 ) | |
382 raise ValidationException("", None, errors, "-") | |
383 | |
384 def __repr__(self): # type: () -> str | |
385 return " | ".join(str(a) for a in self.alternates) | |
386 | |
387 | |
388 class _URILoader(_Loader): | |
389 def __init__(self, inner, scoped_id, vocab_term, scoped_ref): | |
390 # type: (_Loader, bool, bool, Union[int, None]) -> None | |
391 self.inner = inner | |
392 self.scoped_id = scoped_id | |
393 self.vocab_term = vocab_term | |
394 self.scoped_ref = scoped_ref | |
395 | |
396 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
397 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
398 if isinstance(doc, MutableSequence): | |
399 newdoc = [] | |
400 for i in doc: | |
401 if isinstance(i, str): | |
402 newdoc.append( | |
403 expand_url( | |
404 i, | |
405 baseuri, | |
406 loadingOptions, | |
407 self.scoped_id, | |
408 self.vocab_term, | |
409 self.scoped_ref, | |
410 ) | |
411 ) | |
412 else: | |
413 newdoc.append(i) | |
414 doc = newdoc | |
415 elif isinstance(doc, str): | |
416 doc = expand_url( | |
417 doc, | |
418 baseuri, | |
419 loadingOptions, | |
420 self.scoped_id, | |
421 self.vocab_term, | |
422 self.scoped_ref, | |
423 ) | |
424 return self.inner.load(doc, baseuri, loadingOptions) | |
425 | |
426 | |
427 class _TypeDSLLoader(_Loader): | |
428 typeDSLregex = re.compile(r"^([^[?]+)(\[\])?(\?)?$") | |
429 | |
430 def __init__(self, inner, refScope): | |
431 # type: (_Loader, Union[int, None]) -> None | |
432 self.inner = inner | |
433 self.refScope = refScope | |
434 | |
435 def resolve( | |
436 self, | |
437 doc, # type: str | |
438 baseuri, # type: str | |
439 loadingOptions, # type: LoadingOptions | |
440 ): | |
441 # type: (...) -> Union[List[Union[Dict[str, str], str]], Dict[str, str], str] | |
442 m = self.typeDSLregex.match(doc) | |
443 if m: | |
444 group1 = m.group(1) | |
445 assert group1 is not None # nosec | |
446 first = expand_url( | |
447 group1, baseuri, loadingOptions, False, True, self.refScope | |
448 ) | |
449 second = third = None | |
450 if bool(m.group(2)): | |
451 second = {"type": "array", "items": first} | |
452 # second = CommentedMap((("type", "array"), | |
453 # ("items", first))) | |
454 # second.lc.add_kv_line_col("type", lc) | |
455 # second.lc.add_kv_line_col("items", lc) | |
456 # second.lc.filename = filename | |
457 if bool(m.group(3)): | |
458 third = ["null", second or first] | |
459 # third = CommentedSeq(["null", second or first]) | |
460 # third.lc.add_kv_line_col(0, lc) | |
461 # third.lc.add_kv_line_col(1, lc) | |
462 # third.lc.filename = filename | |
463 return third or second or first | |
464 return doc | |
465 | |
466 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
467 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
468 if isinstance(doc, MutableSequence): | |
469 r = [] # type: List[Any] | |
470 for d in doc: | |
471 if isinstance(d, str): | |
472 resolved = self.resolve(d, baseuri, loadingOptions) | |
473 if isinstance(resolved, MutableSequence): | |
474 for i in resolved: | |
475 if i not in r: | |
476 r.append(i) | |
477 else: | |
478 if resolved not in r: | |
479 r.append(resolved) | |
480 else: | |
481 r.append(d) | |
482 doc = r | |
483 elif isinstance(doc, str): | |
484 doc = self.resolve(doc, baseuri, loadingOptions) | |
485 | |
486 return self.inner.load(doc, baseuri, loadingOptions) | |
487 | |
488 | |
489 class _IdMapLoader(_Loader): | |
490 def __init__(self, inner, mapSubject, mapPredicate): | |
491 # type: (_Loader, str, Union[str, None]) -> None | |
492 self.inner = inner | |
493 self.mapSubject = mapSubject | |
494 self.mapPredicate = mapPredicate | |
495 | |
496 def load(self, doc, baseuri, loadingOptions, docRoot=None): | |
497 # type: (Any, str, LoadingOptions, Optional[str]) -> Any | |
498 if isinstance(doc, MutableMapping): | |
499 r = [] # type: List[Any] | |
500 for k in sorted(doc.keys()): | |
501 val = doc[k] | |
502 if isinstance(val, CommentedMap): | |
503 v = copy.copy(val) | |
504 v.lc.data = val.lc.data | |
505 v.lc.filename = val.lc.filename | |
506 v[self.mapSubject] = k | |
507 r.append(v) | |
508 elif isinstance(val, MutableMapping): | |
509 v2 = copy.copy(val) | |
510 v2[self.mapSubject] = k | |
511 r.append(v2) | |
512 else: | |
513 if self.mapPredicate: | |
514 v3 = {self.mapPredicate: val} | |
515 v3[self.mapSubject] = k | |
516 r.append(v3) | |
517 else: | |
518 raise ValidationException("No mapPredicate") | |
519 doc = r | |
520 return self.inner.load(doc, baseuri, loadingOptions) | |
521 | |
522 | |
523 def _document_load(loader, doc, baseuri, loadingOptions): | |
524 # type: (_Loader, Any, str, LoadingOptions) -> Any | |
525 if isinstance(doc, str): | |
526 return _document_load_by_url( | |
527 loader, loadingOptions.fetcher.urljoin(baseuri, doc), loadingOptions | |
528 ) | |
529 | |
530 if isinstance(doc, MutableMapping): | |
531 if "$namespaces" in doc or "$schemas" in doc: | |
532 loadingOptions = LoadingOptions( | |
533 copyfrom=loadingOptions, | |
534 namespaces=doc.get("$namespaces", None), | |
535 schemas=doc.get("$schemas", None), | |
536 ) | |
537 doc = {k: v for k, v in doc.items() if k not in ["$namespaces", "$schemas"]} | |
538 | |
539 if "$base" in doc: | |
540 baseuri = doc["$base"] | |
541 | |
542 if "$graph" in doc: | |
543 return loader.load(doc["$graph"], baseuri, loadingOptions) | |
544 else: | |
545 return loader.load(doc, baseuri, loadingOptions, docRoot=baseuri) | |
546 | |
547 if isinstance(doc, MutableSequence): | |
548 return loader.load(doc, baseuri, loadingOptions) | |
549 | |
550 raise ValidationException("Oops, we shouldn't be here!") | |
551 | |
552 | |
553 def _document_load_by_url(loader, url, loadingOptions): | |
554 # type: (_Loader, str, LoadingOptions) -> Any | |
555 if url in loadingOptions.idx: | |
556 return _document_load(loader, loadingOptions.idx[url], url, loadingOptions) | |
557 | |
558 text = loadingOptions.fetcher.fetch_text(url) | |
559 if isinstance(text, bytes): | |
560 textIO = StringIO(text.decode("utf-8")) | |
561 else: | |
562 textIO = StringIO(text) | |
563 textIO.name = str(url) | |
564 result = yaml.main.round_trip_load(textIO, preserve_quotes=True) | |
565 add_lc_filename(result, url) | |
566 | |
567 loadingOptions.idx[url] = result | |
568 | |
569 loadingOptions = LoadingOptions(copyfrom=loadingOptions, fileuri=url) | |
570 | |
571 return _document_load(loader, result, url, loadingOptions) | |
572 | |
573 | |
574 def file_uri(path, split_frag=False): # type: (str, bool) -> str | |
575 if path.startswith("file://"): | |
576 return path | |
577 if split_frag: | |
578 pathsp = path.split("#", 2) | |
579 frag = "#" + quote(str(pathsp[1])) if len(pathsp) == 2 else "" | |
580 urlpath = pathname2url(str(pathsp[0])) | |
581 else: | |
582 urlpath = pathname2url(path) | |
583 frag = "" | |
584 if urlpath.startswith("//"): | |
585 return f"file:{urlpath}{frag}" | |
586 else: | |
587 return f"file://{urlpath}{frag}" | |
588 | |
589 | |
590 def prefix_url(url, namespaces): # type: (str, Dict[str, str]) -> str | |
591 for k, v in namespaces.items(): | |
592 if url.startswith(v): | |
593 return k + ":" + url[len(v) :] | |
594 return url | |
595 | |
596 | |
597 def save_relative_uri(uri, base_url, scoped_id, ref_scope, relative_uris): | |
598 # type: (str, str, bool, Optional[int], bool) -> Union[str, List[str]] | |
599 if not relative_uris or uri == base_url: | |
600 return uri | |
601 if isinstance(uri, MutableSequence): | |
602 return [ | |
603 save_relative_uri(u, base_url, scoped_id, ref_scope, relative_uris) | |
604 for u in uri | |
605 ] | |
606 elif isinstance(uri, str): | |
607 urisplit = urlsplit(uri) | |
608 basesplit = urlsplit(base_url) | |
609 if urisplit.scheme == basesplit.scheme and urisplit.netloc == basesplit.netloc: | |
610 if urisplit.path != basesplit.path: | |
611 p = os.path.relpath(urisplit.path, os.path.dirname(basesplit.path)) | |
612 if urisplit.fragment: | |
613 p = p + "#" + urisplit.fragment | |
614 return p | |
615 | |
616 basefrag = basesplit.fragment + "/" | |
617 if ref_scope: | |
618 sp = basefrag.split("/") | |
619 i = 0 | |
620 while i < ref_scope: | |
621 sp.pop() | |
622 i += 1 | |
623 basefrag = "/".join(sp) | |
624 | |
625 if urisplit.fragment.startswith(basefrag): | |
626 return urisplit.fragment[len(basefrag) :] | |
627 else: | |
628 return urisplit.fragment | |
629 return uri | |
630 else: | |
631 return save(uri, top=False, base_url=base_url) | |
632 | |
633 | |
634 class Documented(Savable): | |
635 pass | |
636 | |
637 | |
638 class RecordField(Documented): | |
639 """ | |
640 A field of a record. | |
641 """ | |
642 def __init__( | |
643 self, | |
644 name, # type: Any | |
645 type, # type: Any | |
646 doc=None, # type: Any | |
647 extension_fields=None, # type: Optional[Dict[str, Any]] | |
648 loadingOptions=None # type: Optional[LoadingOptions] | |
649 ): # type: (...) -> None | |
650 | |
651 if extension_fields: | |
652 self.extension_fields = extension_fields | |
653 else: | |
654 self.extension_fields = yaml.comments.CommentedMap() | |
655 if loadingOptions: | |
656 self.loadingOptions = loadingOptions | |
657 else: | |
658 self.loadingOptions = LoadingOptions() | |
659 self.doc = doc | |
660 self.name = name | |
661 self.type = type | |
662 | |
663 @classmethod | |
664 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
665 # type: (Any, str, LoadingOptions, Optional[str]) -> RecordField | |
666 | |
667 _doc = copy.copy(doc) | |
668 if hasattr(doc, 'lc'): | |
669 _doc.lc.data = doc.lc.data | |
670 _doc.lc.filename = doc.lc.filename | |
671 _errors__ = [] | |
672 if 'name' in _doc: | |
673 try: | |
674 name = load_field(_doc.get( | |
675 'name'), uri_strtype_True_False_None, baseuri, loadingOptions) | |
676 except ValidationException as e: | |
677 _errors__.append( | |
678 ValidationException( | |
679 "the `name` field is not valid because:", | |
680 SourceLine(_doc, 'name', str), | |
681 [e] | |
682 ) | |
683 ) | |
684 else: | |
685 name = None | |
686 | |
687 if name is None: | |
688 if docRoot is not None: | |
689 name = docRoot | |
690 else: | |
691 raise ValidationException("Missing name") | |
692 baseuri = name | |
693 if 'doc' in _doc: | |
694 try: | |
695 doc = load_field(_doc.get( | |
696 'doc'), union_of_None_type_or_strtype_or_array_of_strtype, baseuri, loadingOptions) | |
697 except ValidationException as e: | |
698 _errors__.append( | |
699 ValidationException( | |
700 "the `doc` field is not valid because:", | |
701 SourceLine(_doc, 'doc', str), | |
702 [e] | |
703 ) | |
704 ) | |
705 else: | |
706 doc = None | |
707 try: | |
708 type = load_field(_doc.get( | |
709 'type'), typedsl_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_2, baseuri, loadingOptions) | |
710 except ValidationException as e: | |
711 _errors__.append( | |
712 ValidationException( | |
713 "the `type` field is not valid because:", | |
714 SourceLine(_doc, 'type', str), | |
715 [e] | |
716 ) | |
717 ) | |
718 | |
719 extension_fields = yaml.comments.CommentedMap() | |
720 for k in _doc.keys(): | |
721 if k not in cls.attrs: | |
722 if ":" in k: | |
723 ex = expand_url(k, | |
724 "", | |
725 loadingOptions, | |
726 scoped_id=False, | |
727 vocab_term=False) | |
728 extension_fields[ex] = _doc[k] | |
729 else: | |
730 _errors__.append( | |
731 ValidationException( | |
732 "invalid field `{}`, expected one of: `doc`, `name`, `type`".format(k), | |
733 SourceLine(_doc, k, str) | |
734 ) | |
735 ) | |
736 break | |
737 | |
738 if _errors__: | |
739 raise ValidationException("Trying 'RecordField'", None, _errors__) | |
740 return cls(doc=doc, name=name, type=type, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
741 | |
742 def save(self, top=False, base_url="", relative_uris=True): | |
743 # type: (bool, str, bool) -> Dict[str, Any] | |
744 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
745 for ef in self.extension_fields: | |
746 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
747 | |
748 if self.name is not None: | |
749 u = save_relative_uri( | |
750 self.name, | |
751 base_url, | |
752 True, | |
753 None, | |
754 relative_uris) | |
755 if u: | |
756 r['name'] = u | |
757 | |
758 if self.doc is not None: | |
759 r['doc'] = save( | |
760 self.doc, | |
761 top=False, | |
762 base_url=self.name, | |
763 relative_uris=relative_uris) | |
764 | |
765 if self.type is not None: | |
766 r['type'] = save( | |
767 self.type, | |
768 top=False, | |
769 base_url=self.name, | |
770 relative_uris=relative_uris) | |
771 | |
772 # top refers to the directory level | |
773 if top: | |
774 if self.loadingOptions.namespaces: | |
775 r["$namespaces"] = self.loadingOptions.namespaces | |
776 if self.loadingOptions.schemas: | |
777 r["$schemas"] = self.loadingOptions.schemas | |
778 return r | |
779 | |
780 attrs = frozenset(['doc', 'name', 'type']) | |
781 | |
782 | |
783 class RecordSchema(Savable): | |
784 def __init__( | |
785 self, | |
786 type, # type: Any | |
787 fields=None, # type: Any | |
788 extension_fields=None, # type: Optional[Dict[str, Any]] | |
789 loadingOptions=None # type: Optional[LoadingOptions] | |
790 ): # type: (...) -> None | |
791 | |
792 if extension_fields: | |
793 self.extension_fields = extension_fields | |
794 else: | |
795 self.extension_fields = yaml.comments.CommentedMap() | |
796 if loadingOptions: | |
797 self.loadingOptions = loadingOptions | |
798 else: | |
799 self.loadingOptions = LoadingOptions() | |
800 self.fields = fields | |
801 self.type = type | |
802 | |
803 @classmethod | |
804 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
805 # type: (Any, str, LoadingOptions, Optional[str]) -> RecordSchema | |
806 | |
807 _doc = copy.copy(doc) | |
808 if hasattr(doc, 'lc'): | |
809 _doc.lc.data = doc.lc.data | |
810 _doc.lc.filename = doc.lc.filename | |
811 _errors__ = [] | |
812 if 'fields' in _doc: | |
813 try: | |
814 fields = load_field(_doc.get( | |
815 'fields'), idmap_fields_union_of_None_type_or_array_of_RecordFieldLoader, baseuri, loadingOptions) | |
816 except ValidationException as e: | |
817 _errors__.append( | |
818 ValidationException( | |
819 "the `fields` field is not valid because:", | |
820 SourceLine(_doc, 'fields', str), | |
821 [e] | |
822 ) | |
823 ) | |
824 else: | |
825 fields = None | |
826 try: | |
827 type = load_field(_doc.get( | |
828 'type'), typedsl_enum_d9cba076fca539106791a4f46d198c7fcfbdb779Loader_2, baseuri, loadingOptions) | |
829 except ValidationException as e: | |
830 _errors__.append( | |
831 ValidationException( | |
832 "the `type` field is not valid because:", | |
833 SourceLine(_doc, 'type', str), | |
834 [e] | |
835 ) | |
836 ) | |
837 | |
838 extension_fields = yaml.comments.CommentedMap() | |
839 for k in _doc.keys(): | |
840 if k not in cls.attrs: | |
841 if ":" in k: | |
842 ex = expand_url(k, | |
843 "", | |
844 loadingOptions, | |
845 scoped_id=False, | |
846 vocab_term=False) | |
847 extension_fields[ex] = _doc[k] | |
848 else: | |
849 _errors__.append( | |
850 ValidationException( | |
851 "invalid field `{}`, expected one of: `fields`, `type`".format(k), | |
852 SourceLine(_doc, k, str) | |
853 ) | |
854 ) | |
855 break | |
856 | |
857 if _errors__: | |
858 raise ValidationException("Trying 'RecordSchema'", None, _errors__) | |
859 return cls(fields=fields, type=type, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
860 | |
861 def save(self, top=False, base_url="", relative_uris=True): | |
862 # type: (bool, str, bool) -> Dict[str, Any] | |
863 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
864 for ef in self.extension_fields: | |
865 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
866 | |
867 if self.fields is not None: | |
868 r['fields'] = save( | |
869 self.fields, | |
870 top=False, | |
871 base_url=base_url, | |
872 relative_uris=relative_uris) | |
873 | |
874 if self.type is not None: | |
875 r['type'] = save( | |
876 self.type, | |
877 top=False, | |
878 base_url=base_url, | |
879 relative_uris=relative_uris) | |
880 | |
881 # top refers to the directory level | |
882 if top: | |
883 if self.loadingOptions.namespaces: | |
884 r["$namespaces"] = self.loadingOptions.namespaces | |
885 if self.loadingOptions.schemas: | |
886 r["$schemas"] = self.loadingOptions.schemas | |
887 return r | |
888 | |
889 attrs = frozenset(['fields', 'type']) | |
890 | |
891 | |
892 class EnumSchema(Savable): | |
893 """ | |
894 Define an enumerated type. | |
895 | |
896 """ | |
897 def __init__( | |
898 self, | |
899 symbols, # type: Any | |
900 type, # type: Any | |
901 extension_fields=None, # type: Optional[Dict[str, Any]] | |
902 loadingOptions=None # type: Optional[LoadingOptions] | |
903 ): # type: (...) -> None | |
904 | |
905 if extension_fields: | |
906 self.extension_fields = extension_fields | |
907 else: | |
908 self.extension_fields = yaml.comments.CommentedMap() | |
909 if loadingOptions: | |
910 self.loadingOptions = loadingOptions | |
911 else: | |
912 self.loadingOptions = LoadingOptions() | |
913 self.symbols = symbols | |
914 self.type = type | |
915 | |
916 @classmethod | |
917 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
918 # type: (Any, str, LoadingOptions, Optional[str]) -> EnumSchema | |
919 | |
920 _doc = copy.copy(doc) | |
921 if hasattr(doc, 'lc'): | |
922 _doc.lc.data = doc.lc.data | |
923 _doc.lc.filename = doc.lc.filename | |
924 _errors__ = [] | |
925 try: | |
926 symbols = load_field(_doc.get( | |
927 'symbols'), uri_array_of_strtype_True_False_None, baseuri, loadingOptions) | |
928 except ValidationException as e: | |
929 _errors__.append( | |
930 ValidationException( | |
931 "the `symbols` field is not valid because:", | |
932 SourceLine(_doc, 'symbols', str), | |
933 [e] | |
934 ) | |
935 ) | |
936 try: | |
937 type = load_field(_doc.get( | |
938 'type'), typedsl_enum_d961d79c225752b9fadb617367615ab176b47d77Loader_2, baseuri, loadingOptions) | |
939 except ValidationException as e: | |
940 _errors__.append( | |
941 ValidationException( | |
942 "the `type` field is not valid because:", | |
943 SourceLine(_doc, 'type', str), | |
944 [e] | |
945 ) | |
946 ) | |
947 | |
948 extension_fields = yaml.comments.CommentedMap() | |
949 for k in _doc.keys(): | |
950 if k not in cls.attrs: | |
951 if ":" in k: | |
952 ex = expand_url(k, | |
953 "", | |
954 loadingOptions, | |
955 scoped_id=False, | |
956 vocab_term=False) | |
957 extension_fields[ex] = _doc[k] | |
958 else: | |
959 _errors__.append( | |
960 ValidationException( | |
961 "invalid field `{}`, expected one of: `symbols`, `type`".format(k), | |
962 SourceLine(_doc, k, str) | |
963 ) | |
964 ) | |
965 break | |
966 | |
967 if _errors__: | |
968 raise ValidationException("Trying 'EnumSchema'", None, _errors__) | |
969 return cls(symbols=symbols, type=type, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
970 | |
971 def save(self, top=False, base_url="", relative_uris=True): | |
972 # type: (bool, str, bool) -> Dict[str, Any] | |
973 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
974 for ef in self.extension_fields: | |
975 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
976 | |
977 if self.symbols is not None: | |
978 u = save_relative_uri( | |
979 self.symbols, | |
980 base_url, | |
981 True, | |
982 None, | |
983 relative_uris) | |
984 if u: | |
985 r['symbols'] = u | |
986 | |
987 if self.type is not None: | |
988 r['type'] = save( | |
989 self.type, | |
990 top=False, | |
991 base_url=base_url, | |
992 relative_uris=relative_uris) | |
993 | |
994 # top refers to the directory level | |
995 if top: | |
996 if self.loadingOptions.namespaces: | |
997 r["$namespaces"] = self.loadingOptions.namespaces | |
998 if self.loadingOptions.schemas: | |
999 r["$schemas"] = self.loadingOptions.schemas | |
1000 return r | |
1001 | |
1002 attrs = frozenset(['symbols', 'type']) | |
1003 | |
1004 | |
1005 class ArraySchema(Savable): | |
1006 def __init__( | |
1007 self, | |
1008 items, # type: Any | |
1009 type, # type: Any | |
1010 extension_fields=None, # type: Optional[Dict[str, Any]] | |
1011 loadingOptions=None # type: Optional[LoadingOptions] | |
1012 ): # type: (...) -> None | |
1013 | |
1014 if extension_fields: | |
1015 self.extension_fields = extension_fields | |
1016 else: | |
1017 self.extension_fields = yaml.comments.CommentedMap() | |
1018 if loadingOptions: | |
1019 self.loadingOptions = loadingOptions | |
1020 else: | |
1021 self.loadingOptions = LoadingOptions() | |
1022 self.items = items | |
1023 self.type = type | |
1024 | |
1025 @classmethod | |
1026 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
1027 # type: (Any, str, LoadingOptions, Optional[str]) -> ArraySchema | |
1028 | |
1029 _doc = copy.copy(doc) | |
1030 if hasattr(doc, 'lc'): | |
1031 _doc.lc.data = doc.lc.data | |
1032 _doc.lc.filename = doc.lc.filename | |
1033 _errors__ = [] | |
1034 try: | |
1035 items = load_field(_doc.get( | |
1036 'items'), uri_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_False_True_2, baseuri, loadingOptions) | |
1037 except ValidationException as e: | |
1038 _errors__.append( | |
1039 ValidationException( | |
1040 "the `items` field is not valid because:", | |
1041 SourceLine(_doc, 'items', str), | |
1042 [e] | |
1043 ) | |
1044 ) | |
1045 try: | |
1046 type = load_field(_doc.get( | |
1047 'type'), typedsl_enum_d062602be0b4b8fd33e69e29a841317b6ab665bcLoader_2, baseuri, loadingOptions) | |
1048 except ValidationException as e: | |
1049 _errors__.append( | |
1050 ValidationException( | |
1051 "the `type` field is not valid because:", | |
1052 SourceLine(_doc, 'type', str), | |
1053 [e] | |
1054 ) | |
1055 ) | |
1056 | |
1057 extension_fields = yaml.comments.CommentedMap() | |
1058 for k in _doc.keys(): | |
1059 if k not in cls.attrs: | |
1060 if ":" in k: | |
1061 ex = expand_url(k, | |
1062 "", | |
1063 loadingOptions, | |
1064 scoped_id=False, | |
1065 vocab_term=False) | |
1066 extension_fields[ex] = _doc[k] | |
1067 else: | |
1068 _errors__.append( | |
1069 ValidationException( | |
1070 "invalid field `{}`, expected one of: `items`, `type`".format(k), | |
1071 SourceLine(_doc, k, str) | |
1072 ) | |
1073 ) | |
1074 break | |
1075 | |
1076 if _errors__: | |
1077 raise ValidationException("Trying 'ArraySchema'", None, _errors__) | |
1078 return cls(items=items, type=type, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
1079 | |
1080 def save(self, top=False, base_url="", relative_uris=True): | |
1081 # type: (bool, str, bool) -> Dict[str, Any] | |
1082 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
1083 for ef in self.extension_fields: | |
1084 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
1085 | |
1086 if self.items is not None: | |
1087 u = save_relative_uri( | |
1088 self.items, | |
1089 base_url, | |
1090 False, | |
1091 2, | |
1092 relative_uris) | |
1093 if u: | |
1094 r['items'] = u | |
1095 | |
1096 if self.type is not None: | |
1097 r['type'] = save( | |
1098 self.type, | |
1099 top=False, | |
1100 base_url=base_url, | |
1101 relative_uris=relative_uris) | |
1102 | |
1103 # top refers to the directory level | |
1104 if top: | |
1105 if self.loadingOptions.namespaces: | |
1106 r["$namespaces"] = self.loadingOptions.namespaces | |
1107 if self.loadingOptions.schemas: | |
1108 r["$schemas"] = self.loadingOptions.schemas | |
1109 return r | |
1110 | |
1111 attrs = frozenset(['items', 'type']) | |
1112 | |
1113 | |
1114 class JsonldPredicate(Savable): | |
1115 """ | |
1116 Attached to a record field to define how the parent record field is handled for | |
1117 URI resolution and JSON-LD context generation. | |
1118 | |
1119 """ | |
1120 def __init__( | |
1121 self, | |
1122 _id=None, # type: Any | |
1123 _type=None, # type: Any | |
1124 _container=None, # type: Any | |
1125 identity=None, # type: Any | |
1126 noLinkCheck=None, # type: Any | |
1127 mapSubject=None, # type: Any | |
1128 mapPredicate=None, # type: Any | |
1129 refScope=None, # type: Any | |
1130 typeDSL=None, # type: Any | |
1131 secondaryFilesDSL=None, # type: Any | |
1132 subscope=None, # type: Any | |
1133 extension_fields=None, # type: Optional[Dict[str, Any]] | |
1134 loadingOptions=None # type: Optional[LoadingOptions] | |
1135 ): # type: (...) -> None | |
1136 | |
1137 if extension_fields: | |
1138 self.extension_fields = extension_fields | |
1139 else: | |
1140 self.extension_fields = yaml.comments.CommentedMap() | |
1141 if loadingOptions: | |
1142 self.loadingOptions = loadingOptions | |
1143 else: | |
1144 self.loadingOptions = LoadingOptions() | |
1145 self._id = _id | |
1146 self._type = _type | |
1147 self._container = _container | |
1148 self.identity = identity | |
1149 self.noLinkCheck = noLinkCheck | |
1150 self.mapSubject = mapSubject | |
1151 self.mapPredicate = mapPredicate | |
1152 self.refScope = refScope | |
1153 self.typeDSL = typeDSL | |
1154 self.secondaryFilesDSL = secondaryFilesDSL | |
1155 self.subscope = subscope | |
1156 | |
1157 @classmethod | |
1158 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
1159 # type: (Any, str, LoadingOptions, Optional[str]) -> JsonldPredicate | |
1160 | |
1161 _doc = copy.copy(doc) | |
1162 if hasattr(doc, 'lc'): | |
1163 _doc.lc.data = doc.lc.data | |
1164 _doc.lc.filename = doc.lc.filename | |
1165 _errors__ = [] | |
1166 if '_id' in _doc: | |
1167 try: | |
1168 _id = load_field(_doc.get( | |
1169 '_id'), uri_union_of_None_type_or_strtype_True_False_None, baseuri, loadingOptions) | |
1170 except ValidationException as e: | |
1171 _errors__.append( | |
1172 ValidationException( | |
1173 "the `_id` field is not valid because:", | |
1174 SourceLine(_doc, '_id', str), | |
1175 [e] | |
1176 ) | |
1177 ) | |
1178 else: | |
1179 _id = None | |
1180 if '_type' in _doc: | |
1181 try: | |
1182 _type = load_field(_doc.get( | |
1183 '_type'), union_of_None_type_or_strtype, baseuri, loadingOptions) | |
1184 except ValidationException as e: | |
1185 _errors__.append( | |
1186 ValidationException( | |
1187 "the `_type` field is not valid because:", | |
1188 SourceLine(_doc, '_type', str), | |
1189 [e] | |
1190 ) | |
1191 ) | |
1192 else: | |
1193 _type = None | |
1194 if '_container' in _doc: | |
1195 try: | |
1196 _container = load_field(_doc.get( | |
1197 '_container'), union_of_None_type_or_strtype, baseuri, loadingOptions) | |
1198 except ValidationException as e: | |
1199 _errors__.append( | |
1200 ValidationException( | |
1201 "the `_container` field is not valid because:", | |
1202 SourceLine(_doc, '_container', str), | |
1203 [e] | |
1204 ) | |
1205 ) | |
1206 else: | |
1207 _container = None | |
1208 if 'identity' in _doc: | |
1209 try: | |
1210 identity = load_field(_doc.get( | |
1211 'identity'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1212 except ValidationException as e: | |
1213 _errors__.append( | |
1214 ValidationException( | |
1215 "the `identity` field is not valid because:", | |
1216 SourceLine(_doc, 'identity', str), | |
1217 [e] | |
1218 ) | |
1219 ) | |
1220 else: | |
1221 identity = None | |
1222 if 'noLinkCheck' in _doc: | |
1223 try: | |
1224 noLinkCheck = load_field(_doc.get( | |
1225 'noLinkCheck'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1226 except ValidationException as e: | |
1227 _errors__.append( | |
1228 ValidationException( | |
1229 "the `noLinkCheck` field is not valid because:", | |
1230 SourceLine(_doc, 'noLinkCheck', str), | |
1231 [e] | |
1232 ) | |
1233 ) | |
1234 else: | |
1235 noLinkCheck = None | |
1236 if 'mapSubject' in _doc: | |
1237 try: | |
1238 mapSubject = load_field(_doc.get( | |
1239 'mapSubject'), union_of_None_type_or_strtype, baseuri, loadingOptions) | |
1240 except ValidationException as e: | |
1241 _errors__.append( | |
1242 ValidationException( | |
1243 "the `mapSubject` field is not valid because:", | |
1244 SourceLine(_doc, 'mapSubject', str), | |
1245 [e] | |
1246 ) | |
1247 ) | |
1248 else: | |
1249 mapSubject = None | |
1250 if 'mapPredicate' in _doc: | |
1251 try: | |
1252 mapPredicate = load_field(_doc.get( | |
1253 'mapPredicate'), union_of_None_type_or_strtype, baseuri, loadingOptions) | |
1254 except ValidationException as e: | |
1255 _errors__.append( | |
1256 ValidationException( | |
1257 "the `mapPredicate` field is not valid because:", | |
1258 SourceLine(_doc, 'mapPredicate', str), | |
1259 [e] | |
1260 ) | |
1261 ) | |
1262 else: | |
1263 mapPredicate = None | |
1264 if 'refScope' in _doc: | |
1265 try: | |
1266 refScope = load_field(_doc.get( | |
1267 'refScope'), union_of_None_type_or_inttype, baseuri, loadingOptions) | |
1268 except ValidationException as e: | |
1269 _errors__.append( | |
1270 ValidationException( | |
1271 "the `refScope` field is not valid because:", | |
1272 SourceLine(_doc, 'refScope', str), | |
1273 [e] | |
1274 ) | |
1275 ) | |
1276 else: | |
1277 refScope = None | |
1278 if 'typeDSL' in _doc: | |
1279 try: | |
1280 typeDSL = load_field(_doc.get( | |
1281 'typeDSL'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1282 except ValidationException as e: | |
1283 _errors__.append( | |
1284 ValidationException( | |
1285 "the `typeDSL` field is not valid because:", | |
1286 SourceLine(_doc, 'typeDSL', str), | |
1287 [e] | |
1288 ) | |
1289 ) | |
1290 else: | |
1291 typeDSL = None | |
1292 if 'secondaryFilesDSL' in _doc: | |
1293 try: | |
1294 secondaryFilesDSL = load_field(_doc.get( | |
1295 'secondaryFilesDSL'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1296 except ValidationException as e: | |
1297 _errors__.append( | |
1298 ValidationException( | |
1299 "the `secondaryFilesDSL` field is not valid because:", | |
1300 SourceLine(_doc, 'secondaryFilesDSL', str), | |
1301 [e] | |
1302 ) | |
1303 ) | |
1304 else: | |
1305 secondaryFilesDSL = None | |
1306 if 'subscope' in _doc: | |
1307 try: | |
1308 subscope = load_field(_doc.get( | |
1309 'subscope'), union_of_None_type_or_strtype, baseuri, loadingOptions) | |
1310 except ValidationException as e: | |
1311 _errors__.append( | |
1312 ValidationException( | |
1313 "the `subscope` field is not valid because:", | |
1314 SourceLine(_doc, 'subscope', str), | |
1315 [e] | |
1316 ) | |
1317 ) | |
1318 else: | |
1319 subscope = None | |
1320 | |
1321 extension_fields = yaml.comments.CommentedMap() | |
1322 for k in _doc.keys(): | |
1323 if k not in cls.attrs: | |
1324 if ":" in k: | |
1325 ex = expand_url(k, | |
1326 "", | |
1327 loadingOptions, | |
1328 scoped_id=False, | |
1329 vocab_term=False) | |
1330 extension_fields[ex] = _doc[k] | |
1331 else: | |
1332 _errors__.append( | |
1333 ValidationException( | |
1334 "invalid field `{}`, expected one of: `_id`, `_type`, `_container`, `identity`, `noLinkCheck`, `mapSubject`, `mapPredicate`, `refScope`, `typeDSL`, `secondaryFilesDSL`, `subscope`".format(k), | |
1335 SourceLine(_doc, k, str) | |
1336 ) | |
1337 ) | |
1338 break | |
1339 | |
1340 if _errors__: | |
1341 raise ValidationException("Trying 'JsonldPredicate'", None, _errors__) | |
1342 return cls(_id=_id, _type=_type, _container=_container, identity=identity, noLinkCheck=noLinkCheck, mapSubject=mapSubject, mapPredicate=mapPredicate, refScope=refScope, typeDSL=typeDSL, secondaryFilesDSL=secondaryFilesDSL, subscope=subscope, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
1343 | |
1344 def save(self, top=False, base_url="", relative_uris=True): | |
1345 # type: (bool, str, bool) -> Dict[str, Any] | |
1346 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
1347 for ef in self.extension_fields: | |
1348 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
1349 | |
1350 if self._id is not None: | |
1351 u = save_relative_uri( | |
1352 self._id, | |
1353 base_url, | |
1354 True, | |
1355 None, | |
1356 relative_uris) | |
1357 if u: | |
1358 r['_id'] = u | |
1359 | |
1360 if self._type is not None: | |
1361 r['_type'] = save( | |
1362 self._type, | |
1363 top=False, | |
1364 base_url=base_url, | |
1365 relative_uris=relative_uris) | |
1366 | |
1367 if self._container is not None: | |
1368 r['_container'] = save( | |
1369 self._container, | |
1370 top=False, | |
1371 base_url=base_url, | |
1372 relative_uris=relative_uris) | |
1373 | |
1374 if self.identity is not None: | |
1375 r['identity'] = save( | |
1376 self.identity, | |
1377 top=False, | |
1378 base_url=base_url, | |
1379 relative_uris=relative_uris) | |
1380 | |
1381 if self.noLinkCheck is not None: | |
1382 r['noLinkCheck'] = save( | |
1383 self.noLinkCheck, | |
1384 top=False, | |
1385 base_url=base_url, | |
1386 relative_uris=relative_uris) | |
1387 | |
1388 if self.mapSubject is not None: | |
1389 r['mapSubject'] = save( | |
1390 self.mapSubject, | |
1391 top=False, | |
1392 base_url=base_url, | |
1393 relative_uris=relative_uris) | |
1394 | |
1395 if self.mapPredicate is not None: | |
1396 r['mapPredicate'] = save( | |
1397 self.mapPredicate, | |
1398 top=False, | |
1399 base_url=base_url, | |
1400 relative_uris=relative_uris) | |
1401 | |
1402 if self.refScope is not None: | |
1403 r['refScope'] = save( | |
1404 self.refScope, | |
1405 top=False, | |
1406 base_url=base_url, | |
1407 relative_uris=relative_uris) | |
1408 | |
1409 if self.typeDSL is not None: | |
1410 r['typeDSL'] = save( | |
1411 self.typeDSL, | |
1412 top=False, | |
1413 base_url=base_url, | |
1414 relative_uris=relative_uris) | |
1415 | |
1416 if self.secondaryFilesDSL is not None: | |
1417 r['secondaryFilesDSL'] = save( | |
1418 self.secondaryFilesDSL, | |
1419 top=False, | |
1420 base_url=base_url, | |
1421 relative_uris=relative_uris) | |
1422 | |
1423 if self.subscope is not None: | |
1424 r['subscope'] = save( | |
1425 self.subscope, | |
1426 top=False, | |
1427 base_url=base_url, | |
1428 relative_uris=relative_uris) | |
1429 | |
1430 # top refers to the directory level | |
1431 if top: | |
1432 if self.loadingOptions.namespaces: | |
1433 r["$namespaces"] = self.loadingOptions.namespaces | |
1434 if self.loadingOptions.schemas: | |
1435 r["$schemas"] = self.loadingOptions.schemas | |
1436 return r | |
1437 | |
1438 attrs = frozenset(['_id', '_type', '_container', 'identity', 'noLinkCheck', 'mapSubject', 'mapPredicate', 'refScope', 'typeDSL', 'secondaryFilesDSL', 'subscope']) | |
1439 | |
1440 | |
1441 class SpecializeDef(Savable): | |
1442 def __init__( | |
1443 self, | |
1444 specializeFrom, # type: Any | |
1445 specializeTo, # type: Any | |
1446 extension_fields=None, # type: Optional[Dict[str, Any]] | |
1447 loadingOptions=None # type: Optional[LoadingOptions] | |
1448 ): # type: (...) -> None | |
1449 | |
1450 if extension_fields: | |
1451 self.extension_fields = extension_fields | |
1452 else: | |
1453 self.extension_fields = yaml.comments.CommentedMap() | |
1454 if loadingOptions: | |
1455 self.loadingOptions = loadingOptions | |
1456 else: | |
1457 self.loadingOptions = LoadingOptions() | |
1458 self.specializeFrom = specializeFrom | |
1459 self.specializeTo = specializeTo | |
1460 | |
1461 @classmethod | |
1462 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
1463 # type: (Any, str, LoadingOptions, Optional[str]) -> SpecializeDef | |
1464 | |
1465 _doc = copy.copy(doc) | |
1466 if hasattr(doc, 'lc'): | |
1467 _doc.lc.data = doc.lc.data | |
1468 _doc.lc.filename = doc.lc.filename | |
1469 _errors__ = [] | |
1470 try: | |
1471 specializeFrom = load_field(_doc.get( | |
1472 'specializeFrom'), uri_strtype_False_False_1, baseuri, loadingOptions) | |
1473 except ValidationException as e: | |
1474 _errors__.append( | |
1475 ValidationException( | |
1476 "the `specializeFrom` field is not valid because:", | |
1477 SourceLine(_doc, 'specializeFrom', str), | |
1478 [e] | |
1479 ) | |
1480 ) | |
1481 try: | |
1482 specializeTo = load_field(_doc.get( | |
1483 'specializeTo'), uri_strtype_False_False_1, baseuri, loadingOptions) | |
1484 except ValidationException as e: | |
1485 _errors__.append( | |
1486 ValidationException( | |
1487 "the `specializeTo` field is not valid because:", | |
1488 SourceLine(_doc, 'specializeTo', str), | |
1489 [e] | |
1490 ) | |
1491 ) | |
1492 | |
1493 extension_fields = yaml.comments.CommentedMap() | |
1494 for k in _doc.keys(): | |
1495 if k not in cls.attrs: | |
1496 if ":" in k: | |
1497 ex = expand_url(k, | |
1498 "", | |
1499 loadingOptions, | |
1500 scoped_id=False, | |
1501 vocab_term=False) | |
1502 extension_fields[ex] = _doc[k] | |
1503 else: | |
1504 _errors__.append( | |
1505 ValidationException( | |
1506 "invalid field `{}`, expected one of: `specializeFrom`, `specializeTo`".format(k), | |
1507 SourceLine(_doc, k, str) | |
1508 ) | |
1509 ) | |
1510 break | |
1511 | |
1512 if _errors__: | |
1513 raise ValidationException("Trying 'SpecializeDef'", None, _errors__) | |
1514 return cls(specializeFrom=specializeFrom, specializeTo=specializeTo, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
1515 | |
1516 def save(self, top=False, base_url="", relative_uris=True): | |
1517 # type: (bool, str, bool) -> Dict[str, Any] | |
1518 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
1519 for ef in self.extension_fields: | |
1520 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
1521 | |
1522 if self.specializeFrom is not None: | |
1523 u = save_relative_uri( | |
1524 self.specializeFrom, | |
1525 base_url, | |
1526 False, | |
1527 1, | |
1528 relative_uris) | |
1529 if u: | |
1530 r['specializeFrom'] = u | |
1531 | |
1532 if self.specializeTo is not None: | |
1533 u = save_relative_uri( | |
1534 self.specializeTo, | |
1535 base_url, | |
1536 False, | |
1537 1, | |
1538 relative_uris) | |
1539 if u: | |
1540 r['specializeTo'] = u | |
1541 | |
1542 # top refers to the directory level | |
1543 if top: | |
1544 if self.loadingOptions.namespaces: | |
1545 r["$namespaces"] = self.loadingOptions.namespaces | |
1546 if self.loadingOptions.schemas: | |
1547 r["$schemas"] = self.loadingOptions.schemas | |
1548 return r | |
1549 | |
1550 attrs = frozenset(['specializeFrom', 'specializeTo']) | |
1551 | |
1552 | |
1553 class NamedType(Savable): | |
1554 pass | |
1555 | |
1556 | |
1557 class DocType(Documented): | |
1558 pass | |
1559 | |
1560 | |
1561 class SchemaDefinedType(DocType): | |
1562 """ | |
1563 Abstract base for schema-defined types. | |
1564 | |
1565 """ | |
1566 pass | |
1567 | |
1568 | |
1569 class SaladRecordField(RecordField): | |
1570 """ | |
1571 A field of a record. | |
1572 """ | |
1573 def __init__( | |
1574 self, | |
1575 name, # type: Any | |
1576 type, # type: Any | |
1577 doc=None, # type: Any | |
1578 jsonldPredicate=None, # type: Any | |
1579 default=None, # type: Any | |
1580 extension_fields=None, # type: Optional[Dict[str, Any]] | |
1581 loadingOptions=None # type: Optional[LoadingOptions] | |
1582 ): # type: (...) -> None | |
1583 | |
1584 if extension_fields: | |
1585 self.extension_fields = extension_fields | |
1586 else: | |
1587 self.extension_fields = yaml.comments.CommentedMap() | |
1588 if loadingOptions: | |
1589 self.loadingOptions = loadingOptions | |
1590 else: | |
1591 self.loadingOptions = LoadingOptions() | |
1592 self.doc = doc | |
1593 self.name = name | |
1594 self.type = type | |
1595 self.jsonldPredicate = jsonldPredicate | |
1596 self.default = default | |
1597 | |
1598 @classmethod | |
1599 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
1600 # type: (Any, str, LoadingOptions, Optional[str]) -> SaladRecordField | |
1601 | |
1602 _doc = copy.copy(doc) | |
1603 if hasattr(doc, 'lc'): | |
1604 _doc.lc.data = doc.lc.data | |
1605 _doc.lc.filename = doc.lc.filename | |
1606 _errors__ = [] | |
1607 if 'name' in _doc: | |
1608 try: | |
1609 name = load_field(_doc.get( | |
1610 'name'), uri_strtype_True_False_None, baseuri, loadingOptions) | |
1611 except ValidationException as e: | |
1612 _errors__.append( | |
1613 ValidationException( | |
1614 "the `name` field is not valid because:", | |
1615 SourceLine(_doc, 'name', str), | |
1616 [e] | |
1617 ) | |
1618 ) | |
1619 else: | |
1620 name = None | |
1621 | |
1622 if name is None: | |
1623 if docRoot is not None: | |
1624 name = docRoot | |
1625 else: | |
1626 raise ValidationException("Missing name") | |
1627 baseuri = name | |
1628 if 'doc' in _doc: | |
1629 try: | |
1630 doc = load_field(_doc.get( | |
1631 'doc'), union_of_None_type_or_strtype_or_array_of_strtype, baseuri, loadingOptions) | |
1632 except ValidationException as e: | |
1633 _errors__.append( | |
1634 ValidationException( | |
1635 "the `doc` field is not valid because:", | |
1636 SourceLine(_doc, 'doc', str), | |
1637 [e] | |
1638 ) | |
1639 ) | |
1640 else: | |
1641 doc = None | |
1642 try: | |
1643 type = load_field(_doc.get( | |
1644 'type'), typedsl_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_2, baseuri, loadingOptions) | |
1645 except ValidationException as e: | |
1646 _errors__.append( | |
1647 ValidationException( | |
1648 "the `type` field is not valid because:", | |
1649 SourceLine(_doc, 'type', str), | |
1650 [e] | |
1651 ) | |
1652 ) | |
1653 if 'jsonldPredicate' in _doc: | |
1654 try: | |
1655 jsonldPredicate = load_field(_doc.get( | |
1656 'jsonldPredicate'), union_of_None_type_or_strtype_or_JsonldPredicateLoader, baseuri, loadingOptions) | |
1657 except ValidationException as e: | |
1658 _errors__.append( | |
1659 ValidationException( | |
1660 "the `jsonldPredicate` field is not valid because:", | |
1661 SourceLine(_doc, 'jsonldPredicate', str), | |
1662 [e] | |
1663 ) | |
1664 ) | |
1665 else: | |
1666 jsonldPredicate = None | |
1667 if 'default' in _doc: | |
1668 try: | |
1669 default = load_field(_doc.get( | |
1670 'default'), union_of_None_type_or_Any_type, baseuri, loadingOptions) | |
1671 except ValidationException as e: | |
1672 _errors__.append( | |
1673 ValidationException( | |
1674 "the `default` field is not valid because:", | |
1675 SourceLine(_doc, 'default', str), | |
1676 [e] | |
1677 ) | |
1678 ) | |
1679 else: | |
1680 default = None | |
1681 | |
1682 extension_fields = yaml.comments.CommentedMap() | |
1683 for k in _doc.keys(): | |
1684 if k not in cls.attrs: | |
1685 if ":" in k: | |
1686 ex = expand_url(k, | |
1687 "", | |
1688 loadingOptions, | |
1689 scoped_id=False, | |
1690 vocab_term=False) | |
1691 extension_fields[ex] = _doc[k] | |
1692 else: | |
1693 _errors__.append( | |
1694 ValidationException( | |
1695 "invalid field `{}`, expected one of: `doc`, `name`, `type`, `jsonldPredicate`, `default`".format(k), | |
1696 SourceLine(_doc, k, str) | |
1697 ) | |
1698 ) | |
1699 break | |
1700 | |
1701 if _errors__: | |
1702 raise ValidationException("Trying 'SaladRecordField'", None, _errors__) | |
1703 return cls(doc=doc, name=name, type=type, jsonldPredicate=jsonldPredicate, default=default, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
1704 | |
1705 def save(self, top=False, base_url="", relative_uris=True): | |
1706 # type: (bool, str, bool) -> Dict[str, Any] | |
1707 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
1708 for ef in self.extension_fields: | |
1709 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
1710 | |
1711 if self.name is not None: | |
1712 u = save_relative_uri( | |
1713 self.name, | |
1714 base_url, | |
1715 True, | |
1716 None, | |
1717 relative_uris) | |
1718 if u: | |
1719 r['name'] = u | |
1720 | |
1721 if self.doc is not None: | |
1722 r['doc'] = save( | |
1723 self.doc, | |
1724 top=False, | |
1725 base_url=self.name, | |
1726 relative_uris=relative_uris) | |
1727 | |
1728 if self.type is not None: | |
1729 r['type'] = save( | |
1730 self.type, | |
1731 top=False, | |
1732 base_url=self.name, | |
1733 relative_uris=relative_uris) | |
1734 | |
1735 if self.jsonldPredicate is not None: | |
1736 r['jsonldPredicate'] = save( | |
1737 self.jsonldPredicate, | |
1738 top=False, | |
1739 base_url=self.name, | |
1740 relative_uris=relative_uris) | |
1741 | |
1742 if self.default is not None: | |
1743 r['default'] = save( | |
1744 self.default, | |
1745 top=False, | |
1746 base_url=self.name, | |
1747 relative_uris=relative_uris) | |
1748 | |
1749 # top refers to the directory level | |
1750 if top: | |
1751 if self.loadingOptions.namespaces: | |
1752 r["$namespaces"] = self.loadingOptions.namespaces | |
1753 if self.loadingOptions.schemas: | |
1754 r["$schemas"] = self.loadingOptions.schemas | |
1755 return r | |
1756 | |
1757 attrs = frozenset(['doc', 'name', 'type', 'jsonldPredicate', 'default']) | |
1758 | |
1759 | |
1760 class SaladRecordSchema(NamedType, RecordSchema, SchemaDefinedType): | |
1761 def __init__( | |
1762 self, | |
1763 name, # type: Any | |
1764 type, # type: Any | |
1765 inVocab=None, # type: Any | |
1766 fields=None, # type: Any | |
1767 doc=None, # type: Any | |
1768 docParent=None, # type: Any | |
1769 docChild=None, # type: Any | |
1770 docAfter=None, # type: Any | |
1771 jsonldPredicate=None, # type: Any | |
1772 documentRoot=None, # type: Any | |
1773 abstract=None, # type: Any | |
1774 extends=None, # type: Any | |
1775 specialize=None, # type: Any | |
1776 extension_fields=None, # type: Optional[Dict[str, Any]] | |
1777 loadingOptions=None # type: Optional[LoadingOptions] | |
1778 ): # type: (...) -> None | |
1779 | |
1780 if extension_fields: | |
1781 self.extension_fields = extension_fields | |
1782 else: | |
1783 self.extension_fields = yaml.comments.CommentedMap() | |
1784 if loadingOptions: | |
1785 self.loadingOptions = loadingOptions | |
1786 else: | |
1787 self.loadingOptions = LoadingOptions() | |
1788 self.name = name | |
1789 self.inVocab = inVocab | |
1790 self.fields = fields | |
1791 self.type = type | |
1792 self.doc = doc | |
1793 self.docParent = docParent | |
1794 self.docChild = docChild | |
1795 self.docAfter = docAfter | |
1796 self.jsonldPredicate = jsonldPredicate | |
1797 self.documentRoot = documentRoot | |
1798 self.abstract = abstract | |
1799 self.extends = extends | |
1800 self.specialize = specialize | |
1801 | |
1802 @classmethod | |
1803 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
1804 # type: (Any, str, LoadingOptions, Optional[str]) -> SaladRecordSchema | |
1805 | |
1806 _doc = copy.copy(doc) | |
1807 if hasattr(doc, 'lc'): | |
1808 _doc.lc.data = doc.lc.data | |
1809 _doc.lc.filename = doc.lc.filename | |
1810 _errors__ = [] | |
1811 if 'name' in _doc: | |
1812 try: | |
1813 name = load_field(_doc.get( | |
1814 'name'), uri_strtype_True_False_None, baseuri, loadingOptions) | |
1815 except ValidationException as e: | |
1816 _errors__.append( | |
1817 ValidationException( | |
1818 "the `name` field is not valid because:", | |
1819 SourceLine(_doc, 'name', str), | |
1820 [e] | |
1821 ) | |
1822 ) | |
1823 else: | |
1824 name = None | |
1825 | |
1826 if name is None: | |
1827 if docRoot is not None: | |
1828 name = docRoot | |
1829 else: | |
1830 raise ValidationException("Missing name") | |
1831 baseuri = name | |
1832 if 'inVocab' in _doc: | |
1833 try: | |
1834 inVocab = load_field(_doc.get( | |
1835 'inVocab'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1836 except ValidationException as e: | |
1837 _errors__.append( | |
1838 ValidationException( | |
1839 "the `inVocab` field is not valid because:", | |
1840 SourceLine(_doc, 'inVocab', str), | |
1841 [e] | |
1842 ) | |
1843 ) | |
1844 else: | |
1845 inVocab = None | |
1846 if 'fields' in _doc: | |
1847 try: | |
1848 fields = load_field(_doc.get( | |
1849 'fields'), idmap_fields_union_of_None_type_or_array_of_SaladRecordFieldLoader, baseuri, loadingOptions) | |
1850 except ValidationException as e: | |
1851 _errors__.append( | |
1852 ValidationException( | |
1853 "the `fields` field is not valid because:", | |
1854 SourceLine(_doc, 'fields', str), | |
1855 [e] | |
1856 ) | |
1857 ) | |
1858 else: | |
1859 fields = None | |
1860 try: | |
1861 type = load_field(_doc.get( | |
1862 'type'), typedsl_enum_d9cba076fca539106791a4f46d198c7fcfbdb779Loader_2, baseuri, loadingOptions) | |
1863 except ValidationException as e: | |
1864 _errors__.append( | |
1865 ValidationException( | |
1866 "the `type` field is not valid because:", | |
1867 SourceLine(_doc, 'type', str), | |
1868 [e] | |
1869 ) | |
1870 ) | |
1871 if 'doc' in _doc: | |
1872 try: | |
1873 doc = load_field(_doc.get( | |
1874 'doc'), union_of_None_type_or_strtype_or_array_of_strtype, baseuri, loadingOptions) | |
1875 except ValidationException as e: | |
1876 _errors__.append( | |
1877 ValidationException( | |
1878 "the `doc` field is not valid because:", | |
1879 SourceLine(_doc, 'doc', str), | |
1880 [e] | |
1881 ) | |
1882 ) | |
1883 else: | |
1884 doc = None | |
1885 if 'docParent' in _doc: | |
1886 try: | |
1887 docParent = load_field(_doc.get( | |
1888 'docParent'), uri_union_of_None_type_or_strtype_False_False_None, baseuri, loadingOptions) | |
1889 except ValidationException as e: | |
1890 _errors__.append( | |
1891 ValidationException( | |
1892 "the `docParent` field is not valid because:", | |
1893 SourceLine(_doc, 'docParent', str), | |
1894 [e] | |
1895 ) | |
1896 ) | |
1897 else: | |
1898 docParent = None | |
1899 if 'docChild' in _doc: | |
1900 try: | |
1901 docChild = load_field(_doc.get( | |
1902 'docChild'), uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_None, baseuri, loadingOptions) | |
1903 except ValidationException as e: | |
1904 _errors__.append( | |
1905 ValidationException( | |
1906 "the `docChild` field is not valid because:", | |
1907 SourceLine(_doc, 'docChild', str), | |
1908 [e] | |
1909 ) | |
1910 ) | |
1911 else: | |
1912 docChild = None | |
1913 if 'docAfter' in _doc: | |
1914 try: | |
1915 docAfter = load_field(_doc.get( | |
1916 'docAfter'), uri_union_of_None_type_or_strtype_False_False_None, baseuri, loadingOptions) | |
1917 except ValidationException as e: | |
1918 _errors__.append( | |
1919 ValidationException( | |
1920 "the `docAfter` field is not valid because:", | |
1921 SourceLine(_doc, 'docAfter', str), | |
1922 [e] | |
1923 ) | |
1924 ) | |
1925 else: | |
1926 docAfter = None | |
1927 if 'jsonldPredicate' in _doc: | |
1928 try: | |
1929 jsonldPredicate = load_field(_doc.get( | |
1930 'jsonldPredicate'), union_of_None_type_or_strtype_or_JsonldPredicateLoader, baseuri, loadingOptions) | |
1931 except ValidationException as e: | |
1932 _errors__.append( | |
1933 ValidationException( | |
1934 "the `jsonldPredicate` field is not valid because:", | |
1935 SourceLine(_doc, 'jsonldPredicate', str), | |
1936 [e] | |
1937 ) | |
1938 ) | |
1939 else: | |
1940 jsonldPredicate = None | |
1941 if 'documentRoot' in _doc: | |
1942 try: | |
1943 documentRoot = load_field(_doc.get( | |
1944 'documentRoot'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1945 except ValidationException as e: | |
1946 _errors__.append( | |
1947 ValidationException( | |
1948 "the `documentRoot` field is not valid because:", | |
1949 SourceLine(_doc, 'documentRoot', str), | |
1950 [e] | |
1951 ) | |
1952 ) | |
1953 else: | |
1954 documentRoot = None | |
1955 if 'abstract' in _doc: | |
1956 try: | |
1957 abstract = load_field(_doc.get( | |
1958 'abstract'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
1959 except ValidationException as e: | |
1960 _errors__.append( | |
1961 ValidationException( | |
1962 "the `abstract` field is not valid because:", | |
1963 SourceLine(_doc, 'abstract', str), | |
1964 [e] | |
1965 ) | |
1966 ) | |
1967 else: | |
1968 abstract = None | |
1969 if 'extends' in _doc: | |
1970 try: | |
1971 extends = load_field(_doc.get( | |
1972 'extends'), uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_1, baseuri, loadingOptions) | |
1973 except ValidationException as e: | |
1974 _errors__.append( | |
1975 ValidationException( | |
1976 "the `extends` field is not valid because:", | |
1977 SourceLine(_doc, 'extends', str), | |
1978 [e] | |
1979 ) | |
1980 ) | |
1981 else: | |
1982 extends = None | |
1983 if 'specialize' in _doc: | |
1984 try: | |
1985 specialize = load_field(_doc.get( | |
1986 'specialize'), idmap_specialize_union_of_None_type_or_array_of_SpecializeDefLoader, baseuri, loadingOptions) | |
1987 except ValidationException as e: | |
1988 _errors__.append( | |
1989 ValidationException( | |
1990 "the `specialize` field is not valid because:", | |
1991 SourceLine(_doc, 'specialize', str), | |
1992 [e] | |
1993 ) | |
1994 ) | |
1995 else: | |
1996 specialize = None | |
1997 | |
1998 extension_fields = yaml.comments.CommentedMap() | |
1999 for k in _doc.keys(): | |
2000 if k not in cls.attrs: | |
2001 if ":" in k: | |
2002 ex = expand_url(k, | |
2003 "", | |
2004 loadingOptions, | |
2005 scoped_id=False, | |
2006 vocab_term=False) | |
2007 extension_fields[ex] = _doc[k] | |
2008 else: | |
2009 _errors__.append( | |
2010 ValidationException( | |
2011 "invalid field `{}`, expected one of: `name`, `inVocab`, `fields`, `type`, `doc`, `docParent`, `docChild`, `docAfter`, `jsonldPredicate`, `documentRoot`, `abstract`, `extends`, `specialize`".format(k), | |
2012 SourceLine(_doc, k, str) | |
2013 ) | |
2014 ) | |
2015 break | |
2016 | |
2017 if _errors__: | |
2018 raise ValidationException("Trying 'SaladRecordSchema'", None, _errors__) | |
2019 return cls(name=name, inVocab=inVocab, fields=fields, type=type, doc=doc, docParent=docParent, docChild=docChild, docAfter=docAfter, jsonldPredicate=jsonldPredicate, documentRoot=documentRoot, abstract=abstract, extends=extends, specialize=specialize, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
2020 | |
2021 def save(self, top=False, base_url="", relative_uris=True): | |
2022 # type: (bool, str, bool) -> Dict[str, Any] | |
2023 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
2024 for ef in self.extension_fields: | |
2025 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
2026 | |
2027 if self.name is not None: | |
2028 u = save_relative_uri( | |
2029 self.name, | |
2030 base_url, | |
2031 True, | |
2032 None, | |
2033 relative_uris) | |
2034 if u: | |
2035 r['name'] = u | |
2036 | |
2037 if self.inVocab is not None: | |
2038 r['inVocab'] = save( | |
2039 self.inVocab, | |
2040 top=False, | |
2041 base_url=self.name, | |
2042 relative_uris=relative_uris) | |
2043 | |
2044 if self.fields is not None: | |
2045 r['fields'] = save( | |
2046 self.fields, | |
2047 top=False, | |
2048 base_url=self.name, | |
2049 relative_uris=relative_uris) | |
2050 | |
2051 if self.type is not None: | |
2052 r['type'] = save( | |
2053 self.type, | |
2054 top=False, | |
2055 base_url=self.name, | |
2056 relative_uris=relative_uris) | |
2057 | |
2058 if self.doc is not None: | |
2059 r['doc'] = save( | |
2060 self.doc, | |
2061 top=False, | |
2062 base_url=self.name, | |
2063 relative_uris=relative_uris) | |
2064 | |
2065 if self.docParent is not None: | |
2066 u = save_relative_uri( | |
2067 self.docParent, | |
2068 self.name, | |
2069 False, | |
2070 None, | |
2071 relative_uris) | |
2072 if u: | |
2073 r['docParent'] = u | |
2074 | |
2075 if self.docChild is not None: | |
2076 u = save_relative_uri( | |
2077 self.docChild, | |
2078 self.name, | |
2079 False, | |
2080 None, | |
2081 relative_uris) | |
2082 if u: | |
2083 r['docChild'] = u | |
2084 | |
2085 if self.docAfter is not None: | |
2086 u = save_relative_uri( | |
2087 self.docAfter, | |
2088 self.name, | |
2089 False, | |
2090 None, | |
2091 relative_uris) | |
2092 if u: | |
2093 r['docAfter'] = u | |
2094 | |
2095 if self.jsonldPredicate is not None: | |
2096 r['jsonldPredicate'] = save( | |
2097 self.jsonldPredicate, | |
2098 top=False, | |
2099 base_url=self.name, | |
2100 relative_uris=relative_uris) | |
2101 | |
2102 if self.documentRoot is not None: | |
2103 r['documentRoot'] = save( | |
2104 self.documentRoot, | |
2105 top=False, | |
2106 base_url=self.name, | |
2107 relative_uris=relative_uris) | |
2108 | |
2109 if self.abstract is not None: | |
2110 r['abstract'] = save( | |
2111 self.abstract, | |
2112 top=False, | |
2113 base_url=self.name, | |
2114 relative_uris=relative_uris) | |
2115 | |
2116 if self.extends is not None: | |
2117 u = save_relative_uri( | |
2118 self.extends, | |
2119 self.name, | |
2120 False, | |
2121 1, | |
2122 relative_uris) | |
2123 if u: | |
2124 r['extends'] = u | |
2125 | |
2126 if self.specialize is not None: | |
2127 r['specialize'] = save( | |
2128 self.specialize, | |
2129 top=False, | |
2130 base_url=self.name, | |
2131 relative_uris=relative_uris) | |
2132 | |
2133 # top refers to the directory level | |
2134 if top: | |
2135 if self.loadingOptions.namespaces: | |
2136 r["$namespaces"] = self.loadingOptions.namespaces | |
2137 if self.loadingOptions.schemas: | |
2138 r["$schemas"] = self.loadingOptions.schemas | |
2139 return r | |
2140 | |
2141 attrs = frozenset(['name', 'inVocab', 'fields', 'type', 'doc', 'docParent', 'docChild', 'docAfter', 'jsonldPredicate', 'documentRoot', 'abstract', 'extends', 'specialize']) | |
2142 | |
2143 | |
2144 class SaladEnumSchema(NamedType, EnumSchema, SchemaDefinedType): | |
2145 """ | |
2146 Define an enumerated type. | |
2147 | |
2148 """ | |
2149 def __init__( | |
2150 self, | |
2151 name, # type: Any | |
2152 symbols, # type: Any | |
2153 type, # type: Any | |
2154 inVocab=None, # type: Any | |
2155 doc=None, # type: Any | |
2156 docParent=None, # type: Any | |
2157 docChild=None, # type: Any | |
2158 docAfter=None, # type: Any | |
2159 jsonldPredicate=None, # type: Any | |
2160 documentRoot=None, # type: Any | |
2161 extends=None, # type: Any | |
2162 extension_fields=None, # type: Optional[Dict[str, Any]] | |
2163 loadingOptions=None # type: Optional[LoadingOptions] | |
2164 ): # type: (...) -> None | |
2165 | |
2166 if extension_fields: | |
2167 self.extension_fields = extension_fields | |
2168 else: | |
2169 self.extension_fields = yaml.comments.CommentedMap() | |
2170 if loadingOptions: | |
2171 self.loadingOptions = loadingOptions | |
2172 else: | |
2173 self.loadingOptions = LoadingOptions() | |
2174 self.name = name | |
2175 self.inVocab = inVocab | |
2176 self.symbols = symbols | |
2177 self.type = type | |
2178 self.doc = doc | |
2179 self.docParent = docParent | |
2180 self.docChild = docChild | |
2181 self.docAfter = docAfter | |
2182 self.jsonldPredicate = jsonldPredicate | |
2183 self.documentRoot = documentRoot | |
2184 self.extends = extends | |
2185 | |
2186 @classmethod | |
2187 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
2188 # type: (Any, str, LoadingOptions, Optional[str]) -> SaladEnumSchema | |
2189 | |
2190 _doc = copy.copy(doc) | |
2191 if hasattr(doc, 'lc'): | |
2192 _doc.lc.data = doc.lc.data | |
2193 _doc.lc.filename = doc.lc.filename | |
2194 _errors__ = [] | |
2195 if 'name' in _doc: | |
2196 try: | |
2197 name = load_field(_doc.get( | |
2198 'name'), uri_strtype_True_False_None, baseuri, loadingOptions) | |
2199 except ValidationException as e: | |
2200 _errors__.append( | |
2201 ValidationException( | |
2202 "the `name` field is not valid because:", | |
2203 SourceLine(_doc, 'name', str), | |
2204 [e] | |
2205 ) | |
2206 ) | |
2207 else: | |
2208 name = None | |
2209 | |
2210 if name is None: | |
2211 if docRoot is not None: | |
2212 name = docRoot | |
2213 else: | |
2214 raise ValidationException("Missing name") | |
2215 baseuri = name | |
2216 if 'inVocab' in _doc: | |
2217 try: | |
2218 inVocab = load_field(_doc.get( | |
2219 'inVocab'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
2220 except ValidationException as e: | |
2221 _errors__.append( | |
2222 ValidationException( | |
2223 "the `inVocab` field is not valid because:", | |
2224 SourceLine(_doc, 'inVocab', str), | |
2225 [e] | |
2226 ) | |
2227 ) | |
2228 else: | |
2229 inVocab = None | |
2230 try: | |
2231 symbols = load_field(_doc.get( | |
2232 'symbols'), uri_array_of_strtype_True_False_None, baseuri, loadingOptions) | |
2233 except ValidationException as e: | |
2234 _errors__.append( | |
2235 ValidationException( | |
2236 "the `symbols` field is not valid because:", | |
2237 SourceLine(_doc, 'symbols', str), | |
2238 [e] | |
2239 ) | |
2240 ) | |
2241 try: | |
2242 type = load_field(_doc.get( | |
2243 'type'), typedsl_enum_d961d79c225752b9fadb617367615ab176b47d77Loader_2, baseuri, loadingOptions) | |
2244 except ValidationException as e: | |
2245 _errors__.append( | |
2246 ValidationException( | |
2247 "the `type` field is not valid because:", | |
2248 SourceLine(_doc, 'type', str), | |
2249 [e] | |
2250 ) | |
2251 ) | |
2252 if 'doc' in _doc: | |
2253 try: | |
2254 doc = load_field(_doc.get( | |
2255 'doc'), union_of_None_type_or_strtype_or_array_of_strtype, baseuri, loadingOptions) | |
2256 except ValidationException as e: | |
2257 _errors__.append( | |
2258 ValidationException( | |
2259 "the `doc` field is not valid because:", | |
2260 SourceLine(_doc, 'doc', str), | |
2261 [e] | |
2262 ) | |
2263 ) | |
2264 else: | |
2265 doc = None | |
2266 if 'docParent' in _doc: | |
2267 try: | |
2268 docParent = load_field(_doc.get( | |
2269 'docParent'), uri_union_of_None_type_or_strtype_False_False_None, baseuri, loadingOptions) | |
2270 except ValidationException as e: | |
2271 _errors__.append( | |
2272 ValidationException( | |
2273 "the `docParent` field is not valid because:", | |
2274 SourceLine(_doc, 'docParent', str), | |
2275 [e] | |
2276 ) | |
2277 ) | |
2278 else: | |
2279 docParent = None | |
2280 if 'docChild' in _doc: | |
2281 try: | |
2282 docChild = load_field(_doc.get( | |
2283 'docChild'), uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_None, baseuri, loadingOptions) | |
2284 except ValidationException as e: | |
2285 _errors__.append( | |
2286 ValidationException( | |
2287 "the `docChild` field is not valid because:", | |
2288 SourceLine(_doc, 'docChild', str), | |
2289 [e] | |
2290 ) | |
2291 ) | |
2292 else: | |
2293 docChild = None | |
2294 if 'docAfter' in _doc: | |
2295 try: | |
2296 docAfter = load_field(_doc.get( | |
2297 'docAfter'), uri_union_of_None_type_or_strtype_False_False_None, baseuri, loadingOptions) | |
2298 except ValidationException as e: | |
2299 _errors__.append( | |
2300 ValidationException( | |
2301 "the `docAfter` field is not valid because:", | |
2302 SourceLine(_doc, 'docAfter', str), | |
2303 [e] | |
2304 ) | |
2305 ) | |
2306 else: | |
2307 docAfter = None | |
2308 if 'jsonldPredicate' in _doc: | |
2309 try: | |
2310 jsonldPredicate = load_field(_doc.get( | |
2311 'jsonldPredicate'), union_of_None_type_or_strtype_or_JsonldPredicateLoader, baseuri, loadingOptions) | |
2312 except ValidationException as e: | |
2313 _errors__.append( | |
2314 ValidationException( | |
2315 "the `jsonldPredicate` field is not valid because:", | |
2316 SourceLine(_doc, 'jsonldPredicate', str), | |
2317 [e] | |
2318 ) | |
2319 ) | |
2320 else: | |
2321 jsonldPredicate = None | |
2322 if 'documentRoot' in _doc: | |
2323 try: | |
2324 documentRoot = load_field(_doc.get( | |
2325 'documentRoot'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
2326 except ValidationException as e: | |
2327 _errors__.append( | |
2328 ValidationException( | |
2329 "the `documentRoot` field is not valid because:", | |
2330 SourceLine(_doc, 'documentRoot', str), | |
2331 [e] | |
2332 ) | |
2333 ) | |
2334 else: | |
2335 documentRoot = None | |
2336 if 'extends' in _doc: | |
2337 try: | |
2338 extends = load_field(_doc.get( | |
2339 'extends'), uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_1, baseuri, loadingOptions) | |
2340 except ValidationException as e: | |
2341 _errors__.append( | |
2342 ValidationException( | |
2343 "the `extends` field is not valid because:", | |
2344 SourceLine(_doc, 'extends', str), | |
2345 [e] | |
2346 ) | |
2347 ) | |
2348 else: | |
2349 extends = None | |
2350 | |
2351 extension_fields = yaml.comments.CommentedMap() | |
2352 for k in _doc.keys(): | |
2353 if k not in cls.attrs: | |
2354 if ":" in k: | |
2355 ex = expand_url(k, | |
2356 "", | |
2357 loadingOptions, | |
2358 scoped_id=False, | |
2359 vocab_term=False) | |
2360 extension_fields[ex] = _doc[k] | |
2361 else: | |
2362 _errors__.append( | |
2363 ValidationException( | |
2364 "invalid field `{}`, expected one of: `name`, `inVocab`, `symbols`, `type`, `doc`, `docParent`, `docChild`, `docAfter`, `jsonldPredicate`, `documentRoot`, `extends`".format(k), | |
2365 SourceLine(_doc, k, str) | |
2366 ) | |
2367 ) | |
2368 break | |
2369 | |
2370 if _errors__: | |
2371 raise ValidationException("Trying 'SaladEnumSchema'", None, _errors__) | |
2372 return cls(name=name, inVocab=inVocab, symbols=symbols, type=type, doc=doc, docParent=docParent, docChild=docChild, docAfter=docAfter, jsonldPredicate=jsonldPredicate, documentRoot=documentRoot, extends=extends, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
2373 | |
2374 def save(self, top=False, base_url="", relative_uris=True): | |
2375 # type: (bool, str, bool) -> Dict[str, Any] | |
2376 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
2377 for ef in self.extension_fields: | |
2378 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
2379 | |
2380 if self.name is not None: | |
2381 u = save_relative_uri( | |
2382 self.name, | |
2383 base_url, | |
2384 True, | |
2385 None, | |
2386 relative_uris) | |
2387 if u: | |
2388 r['name'] = u | |
2389 | |
2390 if self.inVocab is not None: | |
2391 r['inVocab'] = save( | |
2392 self.inVocab, | |
2393 top=False, | |
2394 base_url=self.name, | |
2395 relative_uris=relative_uris) | |
2396 | |
2397 if self.symbols is not None: | |
2398 u = save_relative_uri( | |
2399 self.symbols, | |
2400 self.name, | |
2401 True, | |
2402 None, | |
2403 relative_uris) | |
2404 if u: | |
2405 r['symbols'] = u | |
2406 | |
2407 if self.type is not None: | |
2408 r['type'] = save( | |
2409 self.type, | |
2410 top=False, | |
2411 base_url=self.name, | |
2412 relative_uris=relative_uris) | |
2413 | |
2414 if self.doc is not None: | |
2415 r['doc'] = save( | |
2416 self.doc, | |
2417 top=False, | |
2418 base_url=self.name, | |
2419 relative_uris=relative_uris) | |
2420 | |
2421 if self.docParent is not None: | |
2422 u = save_relative_uri( | |
2423 self.docParent, | |
2424 self.name, | |
2425 False, | |
2426 None, | |
2427 relative_uris) | |
2428 if u: | |
2429 r['docParent'] = u | |
2430 | |
2431 if self.docChild is not None: | |
2432 u = save_relative_uri( | |
2433 self.docChild, | |
2434 self.name, | |
2435 False, | |
2436 None, | |
2437 relative_uris) | |
2438 if u: | |
2439 r['docChild'] = u | |
2440 | |
2441 if self.docAfter is not None: | |
2442 u = save_relative_uri( | |
2443 self.docAfter, | |
2444 self.name, | |
2445 False, | |
2446 None, | |
2447 relative_uris) | |
2448 if u: | |
2449 r['docAfter'] = u | |
2450 | |
2451 if self.jsonldPredicate is not None: | |
2452 r['jsonldPredicate'] = save( | |
2453 self.jsonldPredicate, | |
2454 top=False, | |
2455 base_url=self.name, | |
2456 relative_uris=relative_uris) | |
2457 | |
2458 if self.documentRoot is not None: | |
2459 r['documentRoot'] = save( | |
2460 self.documentRoot, | |
2461 top=False, | |
2462 base_url=self.name, | |
2463 relative_uris=relative_uris) | |
2464 | |
2465 if self.extends is not None: | |
2466 u = save_relative_uri( | |
2467 self.extends, | |
2468 self.name, | |
2469 False, | |
2470 1, | |
2471 relative_uris) | |
2472 if u: | |
2473 r['extends'] = u | |
2474 | |
2475 # top refers to the directory level | |
2476 if top: | |
2477 if self.loadingOptions.namespaces: | |
2478 r["$namespaces"] = self.loadingOptions.namespaces | |
2479 if self.loadingOptions.schemas: | |
2480 r["$schemas"] = self.loadingOptions.schemas | |
2481 return r | |
2482 | |
2483 attrs = frozenset(['name', 'inVocab', 'symbols', 'type', 'doc', 'docParent', 'docChild', 'docAfter', 'jsonldPredicate', 'documentRoot', 'extends']) | |
2484 | |
2485 | |
2486 class Documentation(NamedType, DocType): | |
2487 """ | |
2488 A documentation section. This type exists to facilitate self-documenting | |
2489 schemas but has no role in formal validation. | |
2490 | |
2491 """ | |
2492 def __init__( | |
2493 self, | |
2494 name, # type: Any | |
2495 type, # type: Any | |
2496 inVocab=None, # type: Any | |
2497 doc=None, # type: Any | |
2498 docParent=None, # type: Any | |
2499 docChild=None, # type: Any | |
2500 docAfter=None, # type: Any | |
2501 extension_fields=None, # type: Optional[Dict[str, Any]] | |
2502 loadingOptions=None # type: Optional[LoadingOptions] | |
2503 ): # type: (...) -> None | |
2504 | |
2505 if extension_fields: | |
2506 self.extension_fields = extension_fields | |
2507 else: | |
2508 self.extension_fields = yaml.comments.CommentedMap() | |
2509 if loadingOptions: | |
2510 self.loadingOptions = loadingOptions | |
2511 else: | |
2512 self.loadingOptions = LoadingOptions() | |
2513 self.name = name | |
2514 self.inVocab = inVocab | |
2515 self.doc = doc | |
2516 self.docParent = docParent | |
2517 self.docChild = docChild | |
2518 self.docAfter = docAfter | |
2519 self.type = type | |
2520 | |
2521 @classmethod | |
2522 def fromDoc(cls, doc, baseuri, loadingOptions, docRoot=None): | |
2523 # type: (Any, str, LoadingOptions, Optional[str]) -> Documentation | |
2524 | |
2525 _doc = copy.copy(doc) | |
2526 if hasattr(doc, 'lc'): | |
2527 _doc.lc.data = doc.lc.data | |
2528 _doc.lc.filename = doc.lc.filename | |
2529 _errors__ = [] | |
2530 if 'name' in _doc: | |
2531 try: | |
2532 name = load_field(_doc.get( | |
2533 'name'), uri_strtype_True_False_None, baseuri, loadingOptions) | |
2534 except ValidationException as e: | |
2535 _errors__.append( | |
2536 ValidationException( | |
2537 "the `name` field is not valid because:", | |
2538 SourceLine(_doc, 'name', str), | |
2539 [e] | |
2540 ) | |
2541 ) | |
2542 else: | |
2543 name = None | |
2544 | |
2545 if name is None: | |
2546 if docRoot is not None: | |
2547 name = docRoot | |
2548 else: | |
2549 raise ValidationException("Missing name") | |
2550 baseuri = name | |
2551 if 'inVocab' in _doc: | |
2552 try: | |
2553 inVocab = load_field(_doc.get( | |
2554 'inVocab'), union_of_None_type_or_booltype, baseuri, loadingOptions) | |
2555 except ValidationException as e: | |
2556 _errors__.append( | |
2557 ValidationException( | |
2558 "the `inVocab` field is not valid because:", | |
2559 SourceLine(_doc, 'inVocab', str), | |
2560 [e] | |
2561 ) | |
2562 ) | |
2563 else: | |
2564 inVocab = None | |
2565 if 'doc' in _doc: | |
2566 try: | |
2567 doc = load_field(_doc.get( | |
2568 'doc'), union_of_None_type_or_strtype_or_array_of_strtype, baseuri, loadingOptions) | |
2569 except ValidationException as e: | |
2570 _errors__.append( | |
2571 ValidationException( | |
2572 "the `doc` field is not valid because:", | |
2573 SourceLine(_doc, 'doc', str), | |
2574 [e] | |
2575 ) | |
2576 ) | |
2577 else: | |
2578 doc = None | |
2579 if 'docParent' in _doc: | |
2580 try: | |
2581 docParent = load_field(_doc.get( | |
2582 'docParent'), uri_union_of_None_type_or_strtype_False_False_None, baseuri, loadingOptions) | |
2583 except ValidationException as e: | |
2584 _errors__.append( | |
2585 ValidationException( | |
2586 "the `docParent` field is not valid because:", | |
2587 SourceLine(_doc, 'docParent', str), | |
2588 [e] | |
2589 ) | |
2590 ) | |
2591 else: | |
2592 docParent = None | |
2593 if 'docChild' in _doc: | |
2594 try: | |
2595 docChild = load_field(_doc.get( | |
2596 'docChild'), uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_None, baseuri, loadingOptions) | |
2597 except ValidationException as e: | |
2598 _errors__.append( | |
2599 ValidationException( | |
2600 "the `docChild` field is not valid because:", | |
2601 SourceLine(_doc, 'docChild', str), | |
2602 [e] | |
2603 ) | |
2604 ) | |
2605 else: | |
2606 docChild = None | |
2607 if 'docAfter' in _doc: | |
2608 try: | |
2609 docAfter = load_field(_doc.get( | |
2610 'docAfter'), uri_union_of_None_type_or_strtype_False_False_None, baseuri, loadingOptions) | |
2611 except ValidationException as e: | |
2612 _errors__.append( | |
2613 ValidationException( | |
2614 "the `docAfter` field is not valid because:", | |
2615 SourceLine(_doc, 'docAfter', str), | |
2616 [e] | |
2617 ) | |
2618 ) | |
2619 else: | |
2620 docAfter = None | |
2621 try: | |
2622 type = load_field(_doc.get( | |
2623 'type'), typedsl_enum_056429f0e9355680bd9b2411dc96a69c7ff2e76bLoader_2, baseuri, loadingOptions) | |
2624 except ValidationException as e: | |
2625 _errors__.append( | |
2626 ValidationException( | |
2627 "the `type` field is not valid because:", | |
2628 SourceLine(_doc, 'type', str), | |
2629 [e] | |
2630 ) | |
2631 ) | |
2632 | |
2633 extension_fields = yaml.comments.CommentedMap() | |
2634 for k in _doc.keys(): | |
2635 if k not in cls.attrs: | |
2636 if ":" in k: | |
2637 ex = expand_url(k, | |
2638 "", | |
2639 loadingOptions, | |
2640 scoped_id=False, | |
2641 vocab_term=False) | |
2642 extension_fields[ex] = _doc[k] | |
2643 else: | |
2644 _errors__.append( | |
2645 ValidationException( | |
2646 "invalid field `{}`, expected one of: `name`, `inVocab`, `doc`, `docParent`, `docChild`, `docAfter`, `type`".format(k), | |
2647 SourceLine(_doc, k, str) | |
2648 ) | |
2649 ) | |
2650 break | |
2651 | |
2652 if _errors__: | |
2653 raise ValidationException("Trying 'Documentation'", None, _errors__) | |
2654 return cls(name=name, inVocab=inVocab, doc=doc, docParent=docParent, docChild=docChild, docAfter=docAfter, type=type, extension_fields=extension_fields, loadingOptions=loadingOptions) | |
2655 | |
2656 def save(self, top=False, base_url="", relative_uris=True): | |
2657 # type: (bool, str, bool) -> Dict[str, Any] | |
2658 r = yaml.comments.CommentedMap() # type: Dict[str, Any] | |
2659 for ef in self.extension_fields: | |
2660 r[prefix_url(ef, self.loadingOptions.vocab)] = self.extension_fields[ef] | |
2661 | |
2662 if self.name is not None: | |
2663 u = save_relative_uri( | |
2664 self.name, | |
2665 base_url, | |
2666 True, | |
2667 None, | |
2668 relative_uris) | |
2669 if u: | |
2670 r['name'] = u | |
2671 | |
2672 if self.inVocab is not None: | |
2673 r['inVocab'] = save( | |
2674 self.inVocab, | |
2675 top=False, | |
2676 base_url=self.name, | |
2677 relative_uris=relative_uris) | |
2678 | |
2679 if self.doc is not None: | |
2680 r['doc'] = save( | |
2681 self.doc, | |
2682 top=False, | |
2683 base_url=self.name, | |
2684 relative_uris=relative_uris) | |
2685 | |
2686 if self.docParent is not None: | |
2687 u = save_relative_uri( | |
2688 self.docParent, | |
2689 self.name, | |
2690 False, | |
2691 None, | |
2692 relative_uris) | |
2693 if u: | |
2694 r['docParent'] = u | |
2695 | |
2696 if self.docChild is not None: | |
2697 u = save_relative_uri( | |
2698 self.docChild, | |
2699 self.name, | |
2700 False, | |
2701 None, | |
2702 relative_uris) | |
2703 if u: | |
2704 r['docChild'] = u | |
2705 | |
2706 if self.docAfter is not None: | |
2707 u = save_relative_uri( | |
2708 self.docAfter, | |
2709 self.name, | |
2710 False, | |
2711 None, | |
2712 relative_uris) | |
2713 if u: | |
2714 r['docAfter'] = u | |
2715 | |
2716 if self.type is not None: | |
2717 r['type'] = save( | |
2718 self.type, | |
2719 top=False, | |
2720 base_url=self.name, | |
2721 relative_uris=relative_uris) | |
2722 | |
2723 # top refers to the directory level | |
2724 if top: | |
2725 if self.loadingOptions.namespaces: | |
2726 r["$namespaces"] = self.loadingOptions.namespaces | |
2727 if self.loadingOptions.schemas: | |
2728 r["$schemas"] = self.loadingOptions.schemas | |
2729 return r | |
2730 | |
2731 attrs = frozenset(['name', 'inVocab', 'doc', 'docParent', 'docChild', 'docAfter', 'type']) | |
2732 | |
2733 | |
2734 _vocab = { | |
2735 "Any": "https://w3id.org/cwl/salad#Any", | |
2736 "ArraySchema": "https://w3id.org/cwl/salad#ArraySchema", | |
2737 "DocType": "https://w3id.org/cwl/salad#DocType", | |
2738 "Documentation": "https://w3id.org/cwl/salad#Documentation", | |
2739 "Documented": "https://w3id.org/cwl/salad#Documented", | |
2740 "EnumSchema": "https://w3id.org/cwl/salad#EnumSchema", | |
2741 "JsonldPredicate": "https://w3id.org/cwl/salad#JsonldPredicate", | |
2742 "NamedType": "https://w3id.org/cwl/salad#NamedType", | |
2743 "PrimitiveType": "https://w3id.org/cwl/salad#PrimitiveType", | |
2744 "RecordField": "https://w3id.org/cwl/salad#RecordField", | |
2745 "RecordSchema": "https://w3id.org/cwl/salad#RecordSchema", | |
2746 "SaladEnumSchema": "https://w3id.org/cwl/salad#SaladEnumSchema", | |
2747 "SaladRecordField": "https://w3id.org/cwl/salad#SaladRecordField", | |
2748 "SaladRecordSchema": "https://w3id.org/cwl/salad#SaladRecordSchema", | |
2749 "SchemaDefinedType": "https://w3id.org/cwl/salad#SchemaDefinedType", | |
2750 "SpecializeDef": "https://w3id.org/cwl/salad#SpecializeDef", | |
2751 "array": "https://w3id.org/cwl/salad#array", | |
2752 "boolean": "http://www.w3.org/2001/XMLSchema#boolean", | |
2753 "documentation": "https://w3id.org/cwl/salad#documentation", | |
2754 "double": "http://www.w3.org/2001/XMLSchema#double", | |
2755 "enum": "https://w3id.org/cwl/salad#enum", | |
2756 "float": "http://www.w3.org/2001/XMLSchema#float", | |
2757 "int": "http://www.w3.org/2001/XMLSchema#int", | |
2758 "long": "http://www.w3.org/2001/XMLSchema#long", | |
2759 "null": "https://w3id.org/cwl/salad#null", | |
2760 "record": "https://w3id.org/cwl/salad#record", | |
2761 "string": "http://www.w3.org/2001/XMLSchema#string", | |
2762 } | |
2763 _rvocab = { | |
2764 "https://w3id.org/cwl/salad#Any": "Any", | |
2765 "https://w3id.org/cwl/salad#ArraySchema": "ArraySchema", | |
2766 "https://w3id.org/cwl/salad#DocType": "DocType", | |
2767 "https://w3id.org/cwl/salad#Documentation": "Documentation", | |
2768 "https://w3id.org/cwl/salad#Documented": "Documented", | |
2769 "https://w3id.org/cwl/salad#EnumSchema": "EnumSchema", | |
2770 "https://w3id.org/cwl/salad#JsonldPredicate": "JsonldPredicate", | |
2771 "https://w3id.org/cwl/salad#NamedType": "NamedType", | |
2772 "https://w3id.org/cwl/salad#PrimitiveType": "PrimitiveType", | |
2773 "https://w3id.org/cwl/salad#RecordField": "RecordField", | |
2774 "https://w3id.org/cwl/salad#RecordSchema": "RecordSchema", | |
2775 "https://w3id.org/cwl/salad#SaladEnumSchema": "SaladEnumSchema", | |
2776 "https://w3id.org/cwl/salad#SaladRecordField": "SaladRecordField", | |
2777 "https://w3id.org/cwl/salad#SaladRecordSchema": "SaladRecordSchema", | |
2778 "https://w3id.org/cwl/salad#SchemaDefinedType": "SchemaDefinedType", | |
2779 "https://w3id.org/cwl/salad#SpecializeDef": "SpecializeDef", | |
2780 "https://w3id.org/cwl/salad#array": "array", | |
2781 "http://www.w3.org/2001/XMLSchema#boolean": "boolean", | |
2782 "https://w3id.org/cwl/salad#documentation": "documentation", | |
2783 "http://www.w3.org/2001/XMLSchema#double": "double", | |
2784 "https://w3id.org/cwl/salad#enum": "enum", | |
2785 "http://www.w3.org/2001/XMLSchema#float": "float", | |
2786 "http://www.w3.org/2001/XMLSchema#int": "int", | |
2787 "http://www.w3.org/2001/XMLSchema#long": "long", | |
2788 "https://w3id.org/cwl/salad#null": "null", | |
2789 "https://w3id.org/cwl/salad#record": "record", | |
2790 "http://www.w3.org/2001/XMLSchema#string": "string", | |
2791 } | |
2792 | |
2793 strtype = _PrimitiveLoader((str, str)) | |
2794 inttype = _PrimitiveLoader(int) | |
2795 floattype = _PrimitiveLoader(float) | |
2796 booltype = _PrimitiveLoader(bool) | |
2797 None_type = _PrimitiveLoader(type(None)) | |
2798 Any_type = _AnyLoader() | |
2799 DocumentedLoader = _RecordLoader(Documented) | |
2800 PrimitiveTypeLoader = _EnumLoader(("null", "boolean", "int", "long", "float", "double", "string",)) | |
2801 AnyLoader = _EnumLoader(("Any",)) | |
2802 RecordFieldLoader = _RecordLoader(RecordField) | |
2803 RecordSchemaLoader = _RecordLoader(RecordSchema) | |
2804 EnumSchemaLoader = _RecordLoader(EnumSchema) | |
2805 ArraySchemaLoader = _RecordLoader(ArraySchema) | |
2806 JsonldPredicateLoader = _RecordLoader(JsonldPredicate) | |
2807 SpecializeDefLoader = _RecordLoader(SpecializeDef) | |
2808 NamedTypeLoader = _RecordLoader(NamedType) | |
2809 DocTypeLoader = _RecordLoader(DocType) | |
2810 SchemaDefinedTypeLoader = _RecordLoader(SchemaDefinedType) | |
2811 SaladRecordFieldLoader = _RecordLoader(SaladRecordField) | |
2812 SaladRecordSchemaLoader = _RecordLoader(SaladRecordSchema) | |
2813 SaladEnumSchemaLoader = _RecordLoader(SaladEnumSchema) | |
2814 DocumentationLoader = _RecordLoader(Documentation) | |
2815 array_of_strtype = _ArrayLoader(strtype) | |
2816 union_of_None_type_or_strtype_or_array_of_strtype = _UnionLoader((None_type, strtype, array_of_strtype,)) | |
2817 uri_strtype_True_False_None = _URILoader(strtype, True, False, None) | |
2818 union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype = _UnionLoader((PrimitiveTypeLoader, RecordSchemaLoader, EnumSchemaLoader, ArraySchemaLoader, strtype,)) | |
2819 array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype = _ArrayLoader(union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype) | |
2820 union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype = _UnionLoader((PrimitiveTypeLoader, RecordSchemaLoader, EnumSchemaLoader, ArraySchemaLoader, strtype, array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype,)) | |
2821 typedsl_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_2 = _TypeDSLLoader(union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype, 2) | |
2822 array_of_RecordFieldLoader = _ArrayLoader(RecordFieldLoader) | |
2823 union_of_None_type_or_array_of_RecordFieldLoader = _UnionLoader((None_type, array_of_RecordFieldLoader,)) | |
2824 idmap_fields_union_of_None_type_or_array_of_RecordFieldLoader = _IdMapLoader(union_of_None_type_or_array_of_RecordFieldLoader, 'name', 'type') | |
2825 enum_d9cba076fca539106791a4f46d198c7fcfbdb779Loader = _EnumLoader(("record",)) | |
2826 typedsl_enum_d9cba076fca539106791a4f46d198c7fcfbdb779Loader_2 = _TypeDSLLoader(enum_d9cba076fca539106791a4f46d198c7fcfbdb779Loader, 2) | |
2827 uri_array_of_strtype_True_False_None = _URILoader(array_of_strtype, True, False, None) | |
2828 enum_d961d79c225752b9fadb617367615ab176b47d77Loader = _EnumLoader(("enum",)) | |
2829 typedsl_enum_d961d79c225752b9fadb617367615ab176b47d77Loader_2 = _TypeDSLLoader(enum_d961d79c225752b9fadb617367615ab176b47d77Loader, 2) | |
2830 uri_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_False_True_2 = _URILoader(union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype_or_array_of_union_of_PrimitiveTypeLoader_or_RecordSchemaLoader_or_EnumSchemaLoader_or_ArraySchemaLoader_or_strtype, False, True, 2) | |
2831 enum_d062602be0b4b8fd33e69e29a841317b6ab665bcLoader = _EnumLoader(("array",)) | |
2832 typedsl_enum_d062602be0b4b8fd33e69e29a841317b6ab665bcLoader_2 = _TypeDSLLoader(enum_d062602be0b4b8fd33e69e29a841317b6ab665bcLoader, 2) | |
2833 union_of_None_type_or_strtype = _UnionLoader((None_type, strtype,)) | |
2834 uri_union_of_None_type_or_strtype_True_False_None = _URILoader(union_of_None_type_or_strtype, True, False, None) | |
2835 union_of_None_type_or_booltype = _UnionLoader((None_type, booltype,)) | |
2836 union_of_None_type_or_inttype = _UnionLoader((None_type, inttype,)) | |
2837 uri_strtype_False_False_1 = _URILoader(strtype, False, False, 1) | |
2838 uri_union_of_None_type_or_strtype_False_False_None = _URILoader(union_of_None_type_or_strtype, False, False, None) | |
2839 uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_None = _URILoader(union_of_None_type_or_strtype_or_array_of_strtype, False, False, None) | |
2840 union_of_None_type_or_strtype_or_JsonldPredicateLoader = _UnionLoader((None_type, strtype, JsonldPredicateLoader,)) | |
2841 union_of_None_type_or_Any_type = _UnionLoader((None_type, Any_type,)) | |
2842 array_of_SaladRecordFieldLoader = _ArrayLoader(SaladRecordFieldLoader) | |
2843 union_of_None_type_or_array_of_SaladRecordFieldLoader = _UnionLoader((None_type, array_of_SaladRecordFieldLoader,)) | |
2844 idmap_fields_union_of_None_type_or_array_of_SaladRecordFieldLoader = _IdMapLoader(union_of_None_type_or_array_of_SaladRecordFieldLoader, 'name', 'type') | |
2845 uri_union_of_None_type_or_strtype_or_array_of_strtype_False_False_1 = _URILoader(union_of_None_type_or_strtype_or_array_of_strtype, False, False, 1) | |
2846 array_of_SpecializeDefLoader = _ArrayLoader(SpecializeDefLoader) | |
2847 union_of_None_type_or_array_of_SpecializeDefLoader = _UnionLoader((None_type, array_of_SpecializeDefLoader,)) | |
2848 idmap_specialize_union_of_None_type_or_array_of_SpecializeDefLoader = _IdMapLoader(union_of_None_type_or_array_of_SpecializeDefLoader, 'specializeFrom', 'specializeTo') | |
2849 enum_056429f0e9355680bd9b2411dc96a69c7ff2e76bLoader = _EnumLoader(("documentation",)) | |
2850 typedsl_enum_056429f0e9355680bd9b2411dc96a69c7ff2e76bLoader_2 = _TypeDSLLoader(enum_056429f0e9355680bd9b2411dc96a69c7ff2e76bLoader, 2) | |
2851 union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader = _UnionLoader((SaladRecordSchemaLoader, SaladEnumSchemaLoader, DocumentationLoader,)) | |
2852 array_of_union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader = _ArrayLoader(union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader) | |
2853 union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader_or_array_of_union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader = _UnionLoader((SaladRecordSchemaLoader, SaladEnumSchemaLoader, DocumentationLoader, array_of_union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader,)) | |
2854 | |
2855 | |
2856 def load_document(doc, baseuri=None, loadingOptions=None): | |
2857 # type: (Any, Optional[str], Optional[LoadingOptions]) -> Any | |
2858 if baseuri is None: | |
2859 baseuri = file_uri(os.getcwd()) + "/" | |
2860 if loadingOptions is None: | |
2861 loadingOptions = LoadingOptions() | |
2862 return _document_load(union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader_or_array_of_union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader, doc, baseuri, loadingOptions) | |
2863 | |
2864 | |
2865 def load_document_by_string(string, uri, loadingOptions=None): | |
2866 # type: (Any, str, Optional[LoadingOptions]) -> Any | |
2867 result = yaml.main.round_trip_load(string, preserve_quotes=True) | |
2868 add_lc_filename(result, uri) | |
2869 | |
2870 if loadingOptions is None: | |
2871 loadingOptions = LoadingOptions(fileuri=uri) | |
2872 loadingOptions.idx[uri] = result | |
2873 | |
2874 return _document_load(union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader_or_array_of_union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader, result, uri, loadingOptions) | |
2875 | |
2876 | |
2877 def load_document_by_yaml(yaml, uri, loadingOptions=None): | |
2878 # type: (Any, str, Optional[LoadingOptions]) -> Any | |
2879 '''Shortcut to load via a YAML object. | |
2880 yaml: must be from ruamel.yaml.main.round_trip_load with preserve_quotes=True | |
2881 ''' | |
2882 add_lc_filename(yaml, uri) | |
2883 | |
2884 if loadingOptions is None: | |
2885 loadingOptions = LoadingOptions(fileuri=uri) | |
2886 loadingOptions.idx[uri] = yaml | |
2887 | |
2888 return _document_load(union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader_or_array_of_union_of_SaladRecordSchemaLoader_or_SaladEnumSchemaLoader_or_DocumentationLoader, yaml, uri, loadingOptions) |