|
8 | 8 | import sys |
9 | 9 | from copy import deepcopy |
10 | 10 | from enum import Enum |
11 | | -from typing import Dict, List, Optional, Type, Union |
| 11 | +from typing import Any, Dict, List, Optional, Type, Union |
12 | 12 | from uuid import UUID |
13 | 13 | from warnings import warn |
14 | 14 |
|
| 15 | +import rdflib |
15 | 16 | from jsonpath_ng.ext import parse |
16 | 17 | from pydantic.v1 import BaseModel, PrivateAttr, create_model, validator |
| 18 | +from pyld import jsonld |
17 | 19 |
|
18 | 20 | import osw.model.entity as model |
19 | 21 | from osw.model.static import OswBaseModel |
|
23 | 25 | ) |
24 | 26 | from osw.utils.util import parallelize |
25 | 27 | from osw.utils.wiki import ( |
| 28 | + get_full_title, |
26 | 29 | get_namespace, |
27 | 30 | get_title, |
28 | 31 | get_uuid, |
@@ -1300,6 +1303,126 @@ def query_instances( |
1300 | 1303 | full_page_titles = self.site.semantic_search(search_param) |
1301 | 1304 | return full_page_titles |
1302 | 1305 |
|
| 1306 | + class JsonLdMode(str, Enum): |
| 1307 | + """enum for jsonld processing mode""" |
| 1308 | + |
| 1309 | + expand = "expand" |
| 1310 | + flatten = "flatten" |
| 1311 | + compact = "compact" |
| 1312 | + frame = "frame" |
| 1313 | + |
| 1314 | + class ExportJsonLdParams(OswBaseModel): |
| 1315 | + context_loader_config: Optional[WtSite.JsonLdContextLoaderParams] = None |
| 1316 | + """The configuration for the JSON-LD context loader.""" |
| 1317 | + entities: Union[OswBaseModel, List[OswBaseModel]] |
| 1318 | + """The entities to convert to JSON-LD. Can be a single entity or a list of |
| 1319 | + entities.""" |
| 1320 | + resolve_context: Optional[bool] = True |
| 1321 | + """If True, remote context URLs are resolved.""" |
| 1322 | + mode: Optional[OSW.JsonLdMode] = "expand" |
| 1323 | + """The JSON-LD processing mode to apply if resolve_context is True.""" |
| 1324 | + context: Optional[Union[str, list, Dict[str, Any]]] = None |
| 1325 | + """The JSON-LD context to apply. Replaces any existing context.""" |
| 1326 | + additional_context: Optional[Union[str, list, Dict[str, Any]]] = None |
| 1327 | + """The JSON-LD context to apply on top of the existing context.""" |
| 1328 | + frame: Optional[Dict[str, Any]] = None |
| 1329 | + """The JSON-LD frame to use for framed mode. If not set, the existing context is used""" |
| 1330 | + build_rdf_graph: Optional[bool] = False |
| 1331 | + """If True, the output is a graph.""" |
| 1332 | + debug: Optional[bool] = False |
| 1333 | + |
| 1334 | + def __init__(self, **data): |
| 1335 | + super().__init__(**data) |
| 1336 | + if not isinstance(self.entities, list): |
| 1337 | + self.entities = [self.entities] |
| 1338 | + |
| 1339 | + class ExportJsonLdResult(OswBaseModel): |
| 1340 | + documents: List[Union[Dict[str, Any]]] |
| 1341 | + """A single JSON-LD document per entity""" |
| 1342 | + graph_document: Dict[str, Any] = None |
| 1343 | + """A single JSON-LD document with a @graph element containing all entities""" |
| 1344 | + graph: rdflib.Graph = None |
| 1345 | + """RDF graph containing all entities. Build only if build_rdf_graph is True""" |
| 1346 | + |
| 1347 | + class Config: |
| 1348 | + arbitrary_types_allowed = True |
| 1349 | + |
| 1350 | + def export_jsonld(self, params: ExportJsonLdParams) -> ExportJsonLdResult: |
| 1351 | + """Exports the given entity/entities as JSON-LD.""" |
| 1352 | + |
| 1353 | + if params.resolve_context: |
| 1354 | + jsonld.set_document_loader( |
| 1355 | + self.site.get_jsonld_context_loader(params.context_loader_config) |
| 1356 | + ) |
| 1357 | + |
| 1358 | + documents = [] |
| 1359 | + graph_document = {"@graph": []} |
| 1360 | + graph = None |
| 1361 | + if params.build_rdf_graph: |
| 1362 | + graph = rdflib.Graph() |
| 1363 | + prefixes = self.site.get_prefix_dict() |
| 1364 | + for prefix in prefixes: |
| 1365 | + graph.bind(prefix, prefixes[prefix]) |
| 1366 | + |
| 1367 | + for e in params.entities: |
| 1368 | + data = json.loads(e.json(exclude_none=True, indent=4, ensure_ascii=False)) |
| 1369 | + |
| 1370 | + data["@context"] = [] |
| 1371 | + if params.context is None: |
| 1372 | + for t in e.type: |
| 1373 | + data["@context"].append("/wiki/" + t) |
| 1374 | + if params.context is not None: |
| 1375 | + data["@context"].append(params.context) |
| 1376 | + else: |
| 1377 | + data["@context"] = { |
| 1378 | + **self.site.get_jsonld_context_prefixes(), |
| 1379 | + **params.context, |
| 1380 | + } |
| 1381 | + if params.additional_context is not None: |
| 1382 | + if data["@context"] is None: |
| 1383 | + data["@context"] = [] |
| 1384 | + elif not isinstance(data["@context"], list): |
| 1385 | + data["@context"] = [data["@context"]] |
| 1386 | + data["@context"].append(params.additional_context) |
| 1387 | + |
| 1388 | + data["@id"] = get_full_title(e) |
| 1389 | + |
| 1390 | + if params.resolve_context: |
| 1391 | + graph_document["@graph"].append(jsonld.expand(data)) |
| 1392 | + if params.mode == "expand": |
| 1393 | + data = jsonld.expand(data) |
| 1394 | + if isinstance(data, list): |
| 1395 | + data = data[0] |
| 1396 | + elif params.mode == "flatten": |
| 1397 | + data = jsonld.flatten(data) |
| 1398 | + elif params.mode == "compact": |
| 1399 | + # data = jsonld.expand(data) |
| 1400 | + # if isinstance(data, list): data = data[0] |
| 1401 | + data = jsonld.compact( |
| 1402 | + data, |
| 1403 | + data["@context"] if params.context is None else params.context, |
| 1404 | + ) |
| 1405 | + elif params.mode == "frame": |
| 1406 | + data = jsonld.frame( |
| 1407 | + data, |
| 1408 | + ( |
| 1409 | + {"@context": data["@context"]} |
| 1410 | + if params.frame is None |
| 1411 | + else params.frame |
| 1412 | + ), |
| 1413 | + ) |
| 1414 | + |
| 1415 | + if params.build_rdf_graph: |
| 1416 | + graph.parse(data=json.dumps(data), format="json-ld") |
| 1417 | + |
| 1418 | + documents.append(data) |
| 1419 | + |
| 1420 | + result = OSW.ExportJsonLdResult( |
| 1421 | + documents=documents, graph_document=graph_document, graph=graph |
| 1422 | + ) |
| 1423 | + return result |
| 1424 | + |
1303 | 1425 |
|
1304 | 1426 | OSW._ApplyOverwriteParam.update_forward_refs() |
1305 | 1427 | OSW.StoreEntityParam.update_forward_refs() |
| 1428 | +OSW.ExportJsonLdParams.update_forward_refs() |
0 commit comments