Profiles API
Overview
The profiles module provides YAML-first entity profiles that drive validation, form schema generation, and profile-aware data workflows.
Current implementations: YAML loading, JSON Schema validation, Pydantic model generation, form schema generation, Wikidata validation
Future implementations: profile registry, code generation, broader datatype support
Quick Start
from gkc.profiles import ProfileLoader, ProfileValidator
profile = ProfileLoader().load_from_file(
"/path/to/SpiritSafe/profiles/TribalGovernmentUS/profile.yaml"
)
validator = ProfileValidator(profile)
result = validator.validate_item(item_json, policy="lenient")
Classes
ProfileLoader
Load YAML profile definitions into ProfileDefinition objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_path
|
Optional[Path]
|
Optional path to the JSON schema used for validation. |
None
|
Side effects
Reads the JSON schema from disk during initialization.
Example
loader = ProfileLoader() profile = loader.load_from_file("profiles/TribalGovernmentUS.yaml")
Plain meaning: Read a YAML profile and validate its structure.
Source code in gkc/profiles/loaders/yaml_loader.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
load_from_dict(data)
Load a profile from a Python dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Profile data dictionary. |
required |
Returns:
| Type | Description |
|---|---|
ProfileDefinition
|
Parsed ProfileDefinition instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the profile fails schema validation. |
Side effects
None.
Example
profile = loader.load_from_dict({"name": "Demo", "statements": []})
Plain meaning: Validate and convert profile data to a typed object.
Source code in gkc/profiles/loaders/yaml_loader.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
load_from_file(path)
Load a YAML profile from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to the YAML profile file. |
required |
Returns:
| Type | Description |
|---|---|
ProfileDefinition
|
Parsed ProfileDefinition instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the profile fails schema validation. |
FileNotFoundError
|
If the file does not exist. |
YAMLError
|
If the YAML cannot be parsed. |
Side effects
Reads a YAML file from disk.
Example
profile = loader.load_from_file(".dev/TribalGovernmentUS.yaml")
Plain meaning: Read and validate a profile file.
Source code in gkc/profiles/loaders/yaml_loader.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
load_from_text(text)
Load a YAML profile from text.
Args:
text: YAML text contents.
Returns:
Parsed ProfileDefinition instance.
Raises:
ValueError: If the profile fails schema validation.
yaml.YAMLError: If the YAML cannot be parsed.
Side effects:
None.
Example:
>>> profile = loader.load_from_text("name: Example
statements: []")
Plain meaning: Parse YAML content into a profile object.
Source code in gkc/profiles/loaders/yaml_loader.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
validate_data(data)
Validate profile data against the JSON schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Profile data dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[str]
|
Iterable of error messages (empty if valid). |
Side effects
None.
Example
errors = list(loader.validate_data({"name": "Demo"}))
Plain meaning: Check if the profile matches the required structure.
Source code in gkc/profiles/loaders/yaml_loader.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
ProfileDefinition
Bases: BaseModel
Define a YAML profile and its statements.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Profile name. |
description |
str
|
Profile description. |
labels |
dict[str, MetadataDefinition]
|
Per-language label definitions. |
descriptions |
dict[str, MetadataDefinition]
|
Per-language description definitions. |
aliases |
dict[str, MetadataDefinition]
|
Per-language alias definitions. |
sitelinks |
Optional[SitelinksDefinition]
|
Sitelink definitions for wiki projects. |
statements |
List[ProfileFieldDefinition]
|
List of statement definitions. |
Example
ProfileDefinition(name="Example", description="Demo", statements=[])
Plain meaning: The complete YAML profile definition.
Source code in gkc/profiles/models.py
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 701 702 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 | |
fields
property
Backward-compatible alias for statements.
field_by_id(field_id)
Backward-compatible alias for statement_by_id.
Source code in gkc/profiles/models.py
741 742 743 | |
field_by_property(property_id)
Backward-compatible alias for statement_by_property.
Source code in gkc/profiles/models.py
745 746 747 | |
get_link_definition(target_profile)
Get linkage metadata for a specific target profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_profile
|
str
|
Name of the target profile to find linkage for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[StatementLinkage]
|
StatementLinkage instance or None if no linkage to that profile. |
Side effects
None.
Example
linkage = profile.get_link_definition("OfficeHeldByHeadOfState") linkage.cardinality.max 1
Plain meaning: Get the linkage rules for a specific connected profile.
Source code in gkc/profiles/models.py
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
get_linked_profile_names()
Get a list of all profile names linked from this profile.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of unique profile names referenced in linkage metadata. |
Side effects
None.
Example
profile.get_linked_profile_names() ['OfficeHeldByHeadOfState']
Plain meaning: Find all other profiles this one can link to.
Source code in gkc/profiles/models.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 | |
get_statement_linkages()
Get all statements that have linkage metadata.
Returns:
| Type | Description |
|---|---|
List[ProfileFieldDefinition]
|
List of ProfileFieldDefinition instances with linkage metadata. |
Side effects
None.
Example
linked_statements = profile.get_statement_linkages() for stmt in linked_statements: ... print(stmt.linkage.target_profile)
Plain meaning: Find all statements that link to other profiles.
Source code in gkc/profiles/models.py
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
statement_by_id(statement_id)
Get a statement definition by its identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement_id
|
str
|
Statement identifier to locate. |
required |
Returns:
| Type | Description |
|---|---|
Optional[ProfileFieldDefinition]
|
Matching ProfileFieldDefinition or None if not found. |
Side effects
None.
Example
profile.statement_by_id("instance_of")
Plain meaning: Find a statement configuration by its ID.
Source code in gkc/profiles/models.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | |
statement_by_property(property_id)
Get a statement definition by property ID from configured routes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
property_id
|
str
|
Property ID (e.g., |
required |
Returns:
| Type | Description |
|---|---|
Optional[ProfileFieldDefinition]
|
Matching ProfileFieldDefinition or None if not found. |
Side effects
None.
Example
profile.statement_by_property("P31")
Plain meaning: Find the statement that maps to a property ID.
Source code in gkc/profiles/models.py
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | |
ProfileValidator
Validate Wikidata item data against a ProfileDefinition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
ProfileDefinition
|
Parsed ProfileDefinition instance. |
required |
Side effects
None.
Example
validator = ProfileValidator(profile) result = validator.validate_item(entity_data)
Plain meaning: Apply profile rules to a Wikidata item.
Source code in gkc/profiles/validation/validator.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
validate_item(entity_data, policy='lenient')
Validate a Wikidata item against the profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_data
|
dict
|
Wikidata JSON entity data. |
required |
policy
|
ValidationPolicy
|
Validation policy ("strict" or "lenient"). |
'lenient'
|
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with errors, warnings, and normalized data. |
Side effects
None.
Example
result = validator.validate_item(item, policy="lenient")
Plain meaning: Check a Wikidata item for profile compliance.
Source code in gkc/profiles/validation/validator.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
ValidationResult
Bases: BaseModel
Result of validating an item against a profile.
Attributes:
| Name | Type | Description |
|---|---|---|
ok |
bool
|
Whether validation passed without errors. |
errors |
List[ValidationIssue]
|
Validation errors. |
warnings |
List[ValidationIssue]
|
Validation warnings. |
normalized |
Dict[str, List[StatementData]]
|
Normalized statement data. |
Plain meaning: Validation status and issues found.
Source code in gkc/profiles/validation/validator.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
is_valid()
Return True when validation has no errors.
Returns:
| Type | Description |
|---|---|
bool
|
True if no errors are present. |
Side effects
None.
Example
result.is_valid()
Plain meaning: Check if validation succeeded.
Source code in gkc/profiles/validation/validator.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
FormSchemaGenerator
Generate form/CLI schemas from YAML profiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
ProfileDefinition
|
Parsed ProfileDefinition instance. |
required |
Side effects
None.
Example
schema = FormSchemaGenerator(profile).build_schema()
Plain meaning: Convert a profile into a form-friendly schema.
Source code in gkc/profiles/generators/form_generator.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
build_schema()
Build a form schema dictionary for the profile.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary describing statements, qualifiers, and references. |
Side effects
None.
Example
schema = FormSchemaGenerator(profile).build_schema()
Plain meaning: Export statement definitions for CLI or UI prompts.
Source code in gkc/profiles/generators/form_generator.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
Examples
Load a YAML Profile
from gkc.profiles import ProfileLoader
loader = ProfileLoader()
profile = loader.load_from_file(
"/path/to/SpiritSafe/profiles/TribalGovernmentUS/profile.yaml"
)
print(profile.name)
Generate a Form Schema
from gkc.profiles import FormSchemaGenerator, ProfileLoader
profile = ProfileLoader().load_from_file(
"/path/to/SpiritSafe/profiles/TribalGovernmentUS/profile.yaml"
)
form_schema = FormSchemaGenerator(profile).build_schema()
print(form_schema["statements"][0]["label"])
Validate a Wikidata Item (Lenient)
from gkc.profiles import ProfileLoader, ProfileValidator
profile = ProfileLoader().load_from_file(
"/path/to/SpiritSafe/profiles/TribalGovernmentUS/profile.yaml"
)
validator = ProfileValidator(profile)
result = validator.validate_item(item_json, policy="lenient")
if result.ok:
print("Valid (lenient)")
for warning in result.warnings:
print(warning.message)
Validate a Wikidata Item (Strict)
from gkc.profiles import ProfileLoader, ProfileValidator
profile = ProfileLoader().load_from_file(
"/path/to/SpiritSafe/profiles/TribalGovernmentUS/profile.yaml"
)
validator = ProfileValidator(profile)
result = validator.validate_item(item_json, policy="strict")
if not result.ok:
for error in result.errors:
print(error.message)
Error Handling
Profile Schema Validation Errors
from gkc.profiles import ProfileLoader
loader = ProfileLoader()
try:
loader.load_from_file("bad_profile.yaml")
except ValueError as exc:
print(f"Schema error: {exc}")
See Also
- Mash - Load Wikidata items for validation
- ShEx - Schema validation against EntitySchemas
- CLI Profiles - Profile commands