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