annotate_schema
This module offers functionality to semantically enrich JSON Schemas
by using x-jsonld-context
annotations pointing to JSON-LD context documents,
and also to build ready-to-use JSON-LD contexts from annotated JSON Schemas.
An example of an annotated JSON schema:
"$schema": https://json-schema.org/draft/2020-12/schema
"x-jsonld-context": observation.context.jsonld
title: Observation
type: object
required:
- featureOfInterest
- hasResult
- resultTime
properties:
featureOfInterest:
type: string
hasResult:
type: object
resultTime:
type: string
format: date-time
observationCollection:
type: string
... and its linked x-jsonld-context
:
{
"@context": {
"@version": 1.1,
"sosa": "http://www.w3.org/ns/sosa/",
"featureOfInterest": "sosa:featureOfInterest",
"hasResult": "sosa:hasResult",
"resultTime": "sosa:resultTime"
}
}
A SchemaAnnotator
instance would then generate the following annotated JSON schema:
$schema: https://json-schema.org/draft/2020-12/schema
title: Observation
type: object
required:
- featureOfInterest
- hasResult
- observationTime
properties:
featureOfInterest:
'x-jsonld-id': http://www.w3.org/ns/sosa/featureOfInterest
type: string
hasResult:
'x-jsonld-id': http://www.w3.org/ns/sosa/hasResult
type: object
observationCollection:
type: string
observationTime:
'x-jsonld-id': http://www.w3.org/ns/sosa/resultTime
format: date-time
type: string
This schema can then be referenced from other entities that follow it (e.g., by using FG-JSON "definedby" links).
A client can then build a full JSON-LD @context
(by using a ContextBuilder
instance)
and use it when parsing plain-JSON entities:
{
"@context": {
"featureOfInterest": "http://www.w3.org/ns/sosa/featureOfInterest",
"hasResult": "http://www.w3.org/ns/sosa/hasResult",
"observationTime": "http://www.w3.org/ns/sosa/resultTime"
}
}
A JSON schema can be in YAML or JSON format (the annotated schema will use the same format as the input one).
JSON schemas need to follow some rules to work with this tool:
- No nested
properties
are allowed. If they are needed, they should be put in a different schema, and a$ref
to it used inside the appropriate property definition. allOf
/someOf
root properties can be used to import other schemas (as long as they contain$ref
s to them).
This module can be run as a script, both for schema annotation and for context generation.
To annotate a schema (that already contains a x-jsonld-context
to a JSON-LD context resource):
python -m ogc.na.annotate_schema --file path/to/schema.file.yaml
This will generate a new annotated
directory replicating the layout of the input file
path (/annotated/path/to/schema.file.yaml
in this example).
JSON-LD contexts can be built by adding a -c
flag:
python -m ogc.na.annotate_schema -c --file annotated/path/to/schema.file.yaml
The resulting context will be printed to the standard output.
ContextBuilder
Builds a JSON-LD context from a set of annotated JSON schemas.
Source code in ogc/na/annotate_schema.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
|
__init__(location, compact=True, schema_resolver=None, contents=None, version=1.1)
:ref_mapper: an optional function to map JSON $ref
's before resolving them
Parameters:
Name | Type | Description | Default |
---|---|---|---|
location
|
Path | str
|
file or URL load the annotated schema from |
required |
compact
|
bool
|
whether to compact the resulting context (remove redundancies, compact CURIEs) |
True
|
Source code in ogc/na/annotate_schema.py
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
|
SchemaAnnotator
Builds a set of annotated JSON schemas from a collection of input schemas
that have x-jsonld-context
s to JSON-LD context documents.
The results will be stored in the schemas
property (a dictionary of
schema-path-or-url -> AnnotatedSchema mappings).
Source code in ogc/na/annotate_schema.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
|
__init__(schema_resolver=None, ref_mapper=None, ignore_existing=False)
:schema_resolver: an optional SchemaResolver to resolve references
:ref_mapper: an optional function to map JSON $ref
's before resolving them
Source code in ogc/na/annotate_schema.py
497 498 499 500 501 502 503 504 505 506 |
|
SchemaResolver
Source code in ogc/na/annotate_schema.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
load_contents(s)
Load the contents of a schema. Can be overriden by subclasses to alter the loading process.
Source code in ogc/na/annotate_schema.py
219 220 221 222 223 224 225 226 227 228 229 230 |
|
dump_annotated_schema(schema, subdir='annotated', root_dir=None, output_fn_transform=None)
Creates a "mirror" directory (named annotated
by default) with the resulting
schemas annotated by a SchemaAnnotator
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
AnnotatedSchema
|
the |
required |
subdir
|
Path | str
|
a name for the mirror directory |
'annotated'
|
root_dir
|
Path | str | None
|
root directory for computing relative paths to schemas |
None
|
output_fn_transform
|
Callable[[Path], Path] | None
|
optional callable to transform the output path |
None
|
Source code in ogc/na/annotate_schema.py
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 |
|
load_json_yaml(contents)
Loads either a JSON or a YAML file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
contents
|
str | bytes
|
contents to load |
required |
Returns:
Type | Description |
---|---|
tuple[Any, bool]
|
a tuple with the loaded document, and whether the detected format was JSON (True) or YAML (False) |
Source code in ogc/na/annotate_schema.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
read_contents(location)
Reads contents from a file or URL
@param location: filename or URL to load @return: a tuple with the loaded data (str or bytes) and the base URL, if any
Source code in ogc/na/annotate_schema.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
resolve_ref(ref, fn_from=None, url_from=None, base_url=None)
Resolves a $ref
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ref
|
str
|
the |
required |
fn_from
|
str | Path | None
|
original name of the file containing the |
None
|
url_from
|
str | None
|
original URL of the document containing the |
None
|
base_url
|
str | None
|
base URL of the document containing the |
None
|
Returns:
Type | Description |
---|---|
tuple[Path | None, str | None]
|
a tuple of (Path, str) with only one None entry (the Path if the resolved reference is a file, or the str if it is a URL) |
Source code in ogc/na/annotate_schema.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|