File operations¶
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) -> 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
|
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
reformat ¶
reformat(path: Path, check: bool, output_format: keys() = 'longturtle', output_filename: Path = None) -> 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
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
FailOnChangeError
|
If |
Source code in kurra/file.py
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
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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 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 | |
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 |