File operations¶
Reformatting with a JSON-LD context¶
reformat() accepts an optional context as a Python dict, a JSON string, or a pathlib.Path to a UTF-8 JSON file.
Use either a complete {"@context": {...}} statement or a bare context mapping. The context must be valid JSON-LD;
invalid contexts raise ValueError before output files are written. A directory input applies the context to every RDF file.
For Turtle (including the default longturtle) and N3 output, namespace prefixes from the context are bound to the graph
so serializers can shorten IRIs. For example, supply a prefix mapping when reformatting Turtle:
from pathlib import Path
from kurra.file import reformat
context = {"@context": {"ex": "https://example.org/"}}
reformat(
Path("input.ttl"),
check=False,
output_filename=Path("prefixed.ttl"),
context=context,
)
An IRI such as https://example.org/alice can then be written as ex:alice, with the corresponding prefix declaration.
For JSON-LD output, PyLD's compact() uses the context to compact terms and IRIs. indent defaults to 4 spaces and
only affects JSON-LD output when a context is supplied. Load a context from a file and request two-space indentation:
reformat(
Path("input.ttl"),
check=False,
output_format="json-ld",
output_filename=Path("compacted.jsonld"),
context=Path("context.jsonld"),
indent=2,
)
Alternatively, pass inline JSON as context='{"@context": {"ex": "https://example.org/"}}'.
kurra.file ¶
RDF file manipulation functions.
FailOnChangeError ¶
Bases: Exception
Raised when running format, if check is set and the file would change as a result of the operation.
merge ¶
Merge RDF files into one RDF graph or dataset document.
Named graphs are preserved. Triple-only inputs are placed in DEFAULT_GRAPH_IRI when the output format supports datasets, and named graphs are flattened when it only supports triples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
Path
|
The RDF files to merge. |
()
|
destination
|
Optional[Path]
|
The output file path. If omitted, the merged RDF is printed. |
None
|
output_format
|
str
|
The RDFLib serialization format for the merged RDF. |
'longturtle'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in kurra/file.py
do_format ¶
do_format(content: str, output_format: keys() = 'longturtle', input_format: str | None = None, context: dict | str | Path | None = None, indent: int = 4) -> Tuple[str, bool]
Reformat RDF content to a given serialization format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The RDF content to reformat, as a string. |
required |
output_format
|
keys()
|
The RDFLib serialization format to write. |
'longturtle'
|
input_format
|
str | None
|
The RDFLib format of |
None
|
context
|
dict | str | Path | None
|
A JSON-LD context object, JSON string, or path to a JSON file. |
None
|
indent
|
int
|
Indentation for compacted JSON-LD output. Defaults to 4 spaces. |
4
|
Returns:
| Type | Description |
|---|---|
Tuple[str, bool]
|
A tuple of the reformatted content and whether it changed from the input. |
Source code in kurra/file.py
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 213 214 215 216 217 | |
reformat ¶
reformat(path: Path, check: bool, output_format: keys() = 'longturtle', output_filename: Path = None, context: dict | str | Path | None = None, indent: int = 4) -> None
Reformat one RDF file or every RDF file in a directory to a given format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The file or directory of RDF files to be formatted. |
required |
check
|
bool
|
If True, check whether files will be changed by this command without applying the effect. |
required |
output_format
|
keys()
|
The RDFLib serialization format to write. |
'longturtle'
|
output_filename
|
Path
|
The name of the file to write the reformatted content to. Only valid when |
None
|
context
|
dict | str | Path | None
|
A JSON-LD context dict (wrapped in |
None
|
indent
|
int
|
Indentation for compacted JSON-LD output. Defaults to 4 spaces.
Only applies when |
4
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
FailOnChangeError
|
If |
Source code in kurra/file.py
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
make_dataset ¶
Wrap a graph, or an RDF file or string of triples, as a Dataset under a given graph IRI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_str_or_graph
|
Union[Path, str, Graph]
|
The RDF source to wrap as a Graph, file path, or string of triples. |
required |
graph_iri
|
Union[str, URIRef]
|
The IRI to assign every triple's named graph. |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
A Dataset containing the source's triples under |
Source code in kurra/file.py
hierarchy ¶
hierarchy(path_str_or_graph: Union[Path, str, Graph], graph_iri: Optional[Union[str, URIRef]] = None, use_names: bool = False) -> None
Print the class, property, or concept hierarchy found in an RDF source.
Resources are displayed as namespace-qualified names where possible, or by their preferred label if use_names is set. Separate hierarchy roots, and separate hierarchy kinds, are divided by a blank line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_str_or_graph
|
Union[Path, str, Graph]
|
An RDF file, serialized RDF string, or RDFLib Graph. |
required |
graph_iri
|
Optional[Union[str, URIRef]]
|
The named graph to use. Only valid for a remote URL or a |
None
|
use_names
|
bool
|
If True, select each resource's name from (in order): |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in kurra/file.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 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 | |
export_quads ¶
export_quads(path_str_or_dataset: Union[Path, str, Dataset], destination: Optional[Path] = None) -> bool | str
Export triples from a given Dataset, quads string in trig format, or a quads file specified by Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_str_or_dataset
|
Union[Path, str, Dataset]
|
A Dataset, a trig file path, or a string of trig data. |
required |
destination
|
Optional[Path]
|
The file to write the quads to. If given and the file already exists, its quads are merged in. |
None
|
Returns:
| Type | Description |
|---|---|
bool | str
|
True if written to |