Cooperage API
Overview
The cooperage module is the active packet-to-operation barreling layer between still_charger and shipper.
Current primary functionality converts charged curation packet entities into operation payloads compatible with WikibaseShipper.plan_batch and write methods.
Legacy schema/RDF helpers remain available as compatibility re-exports.
Quick Start
from gkc.cooperage import barrel_curation_packet_to_wikibase_plan
packet = {
"packet_id": "pkt-demo",
"entities": [
{
"id": "ent-001",
"profile": "TribalGovernmentUS",
"profile_structure": {
"statements": [{"id": "instance_of", "io_map": [{"to": "https://www.wikidata.org/entity/P31"}]}]
},
"data": {
"labels": {"en": "Cherokee Nation"},
"statements": {"instance_of": [{"value": "Q7840353"}]},
},
}
],
}
operations, report = barrel_curation_packet_to_wikibase_plan(packet)
print(report.operations_created)
print(operations[0]["kind"], operations[0]["label"])
Public API Quick Starts
barrel_curation_packet_to_wikibase_plan()
from gkc.cooperage import barrel_curation_packet_to_wikibase_plan
operations, report = barrel_curation_packet_to_wikibase_plan(
packet,
property_id_map={"instance_of": "P31"},
)
print(report.operations_created)
print(report.entities_skipped)
print([issue.message for issue in report.issues])
BarrelIssue and BarrelPlanReport
from gkc.cooperage import BarrelIssue, BarrelPlanReport
issue = BarrelIssue(
severity="warning",
entity_id="ent-001",
field="statements.instance_of",
message="No property mapping found",
)
report = BarrelPlanReport(operations_created=0, entities_skipped=1, issues=[issue])
print(report.entities_skipped, report.issues[0].severity)
Compatibility re-exports
from gkc.cooperage import fetch_entity_rdf, fetch_schema_specification
rdf_ttl = fetch_entity_rdf("Q42", format="ttl")
schema_text = fetch_schema_specification("E502")
print(len(rdf_ttl), len(schema_text))
API Reference (mkdocstrings)
BarrelIssue
Source code in gkc/cooperage.py
37 38 39 40 41 42 | |
BarrelPlanReport
Source code in gkc/cooperage.py
45 46 47 48 49 | |
barrel_curation_packet_to_wikibase_plan()
Convert charged curation packet content into Wikibase plan operations.
Output operations are compatible with WikibaseShipper.plan_batch.
Source code in gkc/cooperage.py
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 315 316 317 318 319 320 321 322 | |
CooperageError (compatibility)
Bases: Exception
DEPRECATED: Use RuntimeError instead.
Raised when entity/schema fetch operations fail. This exception is provided for backward compatibility but new code should catch RuntimeError instead.
See: gkc.mash for fetch functions that raise RuntimeError.
Source code in gkc/cooperage.py
24 25 26 27 28 29 30 31 32 33 34 | |
fetch_entity_rdf() (compatibility)
Fetch RDF data for a Wikidata entity.
Retrieves entity data in RDF format using Wikibase's Special:EntityData endpoint, which supports multiple RDF serialization formats (Turtle, RDF/XML, N-Triples).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid
|
str
|
Wikidata entity ID (e.g., 'Q42', 'P31') |
required |
format
|
str
|
RDF format - 'ttl' (Turtle), 'rdf' (RDF/XML), 'nt' (N-Triples) |
'ttl'
|
user_agent
|
Optional[str]
|
Custom user agent string |
None
|
Returns:
| Type | Description |
|---|---|
str
|
RDF data as string |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If fetch fails |
Plain meaning: Download entity data in RDF format.
Example
rdf = fetch_entity_rdf('Q42') # Get Douglas Adams RDF rdf = fetch_entity_rdf('P31', format='nt') # Get property in N-Triples
Source code in gkc/mash/core.py
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 | |
fetch_schema_specification() (compatibility)
Fetch Wikidata EntitySchema specification text (ShExC format).
Retrieves a Wikidata EntitySchema's schemaText from the raw action endpoint. EntitySchemas define the shape and structure constraints that form part of Wikibase's validation schema (along with property constraints).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eid
|
str
|
EntitySchema ID (e.g., 'E502') |
required |
user_agent
|
Optional[str]
|
Custom user agent string |
None
|
Returns:
| Type | Description |
|---|---|
str
|
ShExC schema text as string |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If fetch fails |
Plain meaning: Get the shape/structure specification for a Wikibase entity type.
Example
schema = fetch_entity_schema_specification('E502') # Schema for tribes
Source code in gkc/mash/core.py
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 | |
fetch_entity_schema_json() (compatibility)
Fetch the JSON content for a Wikidata EntitySchema.
Uses the MediaWiki raw action endpoint to retrieve the full EntitySchema JSON, which includes labels, descriptions, aliases, and schemaText.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eid
|
str
|
EntitySchema ID (e.g., 'E502') |
required |
user_agent
|
Optional[str]
|
Custom user agent string |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed JSON dictionary for the EntitySchema |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If fetch or parsing fails |
Plain meaning: Retrieve an EntitySchema JSON document from Wikibase.
Source code in gkc/mash/core.py
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 | |
fetch_entity_schema_metadata() (compatibility)
DEPRECATED: Fetch metadata for a Wikidata EntitySchema.
This function is no longer actively maintained. For basic entity schema retrieval, use fetch_entity_schema_json() instead.
To be removed in v0.3.0.
Source code in gkc/cooperage.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
get_entity_uri() (compatibility)
Get the full URI for a Wikidata entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
Entity ID (e.g., 'Q42', 'P31', 'L1', 'E502') |
required |
Returns:
| Type | Description |
|---|---|
str
|
Full URI string |
Example
get_entity_uri('Q42') 'http://www.wikidata.org/entity/Q42' get_entity_uri('P31') 'http://www.wikidata.org/entity/P31'
Source code in gkc/utilities.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
validate_entity_reference() (compatibility)
Validate that a string looks like a Wikidata entity ID.
Plain meaning: Check if an ID is in valid Wikidata format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
String to validate |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid format, False otherwise |
Example
validate_entity_reference('Q42') True validate_entity_reference('P31') True validate_entity_reference('E502') True validate_entity_reference('invalid') False
Source code in gkc/utilities.py
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 | |