MetadataReader
dorsal.file.metadata_reader.MetadataReader
MetadataReader(
api_key=None,
client=None,
model_config="default",
ignore_duplicates=False,
base_url=constants.BASE_URL,
file_class=LocalFile,
offline=False,
)
A high-level utility for processing local files and indexing metadata.
This class provides a convenient interface to the ModelRunner engine.
It is designed to simplify the process of scanning local files, extracting
rich metadata based on a configurable pipeline, and optionally pushing
the results directly to a DorsalHub instance.
Example
from dorsal.file import MetadataReader
# Initialize the reader
reader = MetadataReader()
# Use the reader to process a directory and get LocalFile objects
local_files = reader.scan_directory("path/to/my_data")
print(f"Processed {len(local_files)} files.")
# Or, use the reader to process and immediately index a directory
summary = reader.index_directory("path/to/my_data")
print(f"Indexed {summary['total_records_accepted_by_api']} files to DorsalHub.")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
DorsalClient
|
An existing |
None
|
model_config
|
str | list
|
A path to a custom JSON pipeline configuration file or a dictionary defining the pipeline for the ModelRunner. If None, the default pipeline is used. Defaults to None. |
'default'
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str | None
|
Optional API key for DorsalHub. If not provided, DorsalClient will attempt to read it from environment variables. |
None
|
model_config
|
str | list[dict[str, Any]] | None
|
Optional configuration for the ModelRunner instance used for local file processing. |
'default'
|
ignore_duplicates
|
bool
|
If True, duplicate files (based on content hash) encountered during directory indexing will be skipped. If False (default), a DuplicateFileError will be raised. |
False
|
base_url
|
str
|
The base URL for the Dorsal API. Defaults to the common BASE_URL. |
BASE_URL
|
file_class
|
Type[LocalFile]
|
Currently only |
LocalFile
|
Source code in venv/lib/python3.13/site-packages/dorsal/file/metadata_reader.py
generate_processed_records_from_directory
generate_processed_records_from_directory(
dir_path,
*,
recursive=False,
limit=None,
console=None,
palette=None,
skip_cache=False,
overwrite_cache=False
)
Scan directory, and sends each file through the ModelRunner pipeline.
When duplicates are ignored, this returned map intentionally contains only the path of the first file
encountered for each hash
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
Path to the directory to scan. |
required |
recursive
|
bool
|
If True, scans subdirectories recursively. Defaults to False. |
False
|
limit
|
int | None
|
Optional. top processing files once this many unique records have been generated. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[FileRecordStrict], dict[str, str]]
|
tuple[list[FileRecordStrict], dict[str, str]]:
- list of unique |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If |
DuplicateFileError
|
If |
DorsalClientError
|
For errors scanning directory or during local processing. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/metadata_reader.py
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 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 | |
index_directory
Scans, processes, and indexes all files in a directory to DorsalHub.
This is an online method that performs a complete workflow: 1. Scans the directory for files, handling recursion as specified. 2. Runs the metadata extraction pipeline on each unique file. 3. Indexes all resulting records to DorsalHub in a single batch API call.
It handles duplicate file content within the directory based on the
ignore_duplicates setting provided during the reader's initialization.
Note
This method is designed for convenience and sends all discovered
records in a single API request. It will raise a BatchSizeError
if the number of unique files in the directory exceeds the API's
batch limit. For very large directories, it is recommended to use
the dorsal.api.file.index_directory() wrapper, which handles
splitting the upload into multiple batches automatically.
Example
from dorsal.file import MetadataReader
# Initialize a reader that will raise an error on duplicates.
reader = MetadataReader(ignore_duplicates=False)
try:
response = reader.index_directory("path/to/project/assets", private=True)
print(f"Indexing complete. {response.success} of {response.total} records indexed.")
except Exception as e:
print(f"An error occurred during indexing: {e}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
The path to the directory to scan and index. |
required |
recursive
|
bool
|
If True, scans all subdirectories recursively. Defaults to False. |
False
|
private
|
bool
|
If True, all file records will be created as private on DorsalHub. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
FileIndexResponse |
FileIndexResponse
|
A response object from the API detailing the outcome
of the batch indexing operation. The |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the |
DuplicateFileError
|
If duplicate file content is detected and the
reader was initialized with |
BatchSizeError
|
If the number of unique files found exceeds the API's single-request batch limit. |
DorsalClientError
|
For any other error occurring during metadata extraction or the subsequent API call. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/metadata_reader.py
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 | |
index_file
Processes a single file and immediately indexes it to DorsalHub.
This method provides a complete "read and push" workflow for a single file. It runs the metadata extraction pipeline and then uploads the resulting record to DorsalHub.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the local file to process and index. |
required |
private
|
bool
|
If True, the file record will be created as private on DorsalHub. Defaults to True. |
True
|
api_key
|
str
|
An API key to use for this specific request, overriding the client's default key. Defaults to None. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FileIndexResponse |
FileIndexResponse
|
A response object from the API detailing the result of the indexing operation. |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
If there's an error processing the file locally or an API error during indexing. |
FileNotFoundError
|
If the file_path does not exist. |
IOError
|
If the file cannot be read by ModelRunner. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/metadata_reader.py
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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 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 | |
scan_directory
scan_directory(
dir_path,
*,
recursive=False,
return_errors=False,
console=None,
palette=None,
skip_cache=False,
overwrite_cache=False
)
Scans a directory and runs the pipeline on all found files.
This method discovers all files within a given directory (and optionally
its subdirectories), runs the metadata extraction pipeline on each one,
and returns a list of the resulting LocalFile objects. This is an
offline operation.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
The path to the directory to scan. |
required |
recursive
|
bool
|
If True, scans all subdirectories recursively. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
list[LocalFile] | tuple[list[LocalFile], list[str]]
|
list[LocalFile]: initialized |
|
list[LocalFile] | tuple[list[LocalFile], list[str]]
|
Or |
|
tuple |
list[LocalFile] | tuple[list[LocalFile], list[str]]
|
|
list[LocalFile] | tuple[list[LocalFile], list[str]]
|
|
|
list[LocalFile] | tuple[list[LocalFile], list[str]]
|
|
Source code in venv/lib/python3.13/site-packages/dorsal/file/metadata_reader.py
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 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | |
scan_file
Runs the metadata extraction pipeline on a single file.
This method processes a single local file through the configured
ModelRunner pipeline, generating all its metadata. This is an
offline operation that does not contact the DorsalHub API.
Example
from dorsal.file import MetadataReader
reader = MetadataReader()
try:
local_file = reader.scan_file("path/to/document.pdf")
print(f"Successfully processed: {local_file.name}")
if local_file.pdf:
print(f"Page Count: {local_file.pdf.page_count}")
except FileNotFoundError:
print("The specified file does not exist.")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the local file to process. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
LocalFile |
LocalFile
|
An initialized |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file_path does not exist or is not a file. |
IOError
|
If there are issues reading the file. |
DorsalError
|
For other errors during model execution |