DorsalFile
dorsal.file.dorsal_file.DorsalFile
Bases: _DorsalFile
Represents a file whose metadata is fetched from the DorsalHub platform.
This constructor performs a network request to the DorsalHub API to download the file's record. The operation may take some time depending on network conditions and API responsiveness.
This class provides an object-oriented interface to a file's record on DorsalHub, identified by its hash string.
Attributes:
| Name | Type | Description |
|---|---|---|
hash |
str
|
The primary SHA-256 hash of the file content. |
name |
str
|
The base name of the file. |
size |
int
|
The file size in bytes. |
media_type |
str
|
The detected media type of the file. |
tags |
list[FileTag]
|
A list of tags associated with the file. |
annotations |
object
|
A container for detailed metadata records. |
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_string
|
str
|
The hash of the file to fetch. Can be prefixed with the algorithm (e.g., "blake3:...") but defaults to SHA-256. |
required |
private
|
bool
|
If True, searches for the file in your private records. If False, searches public records. Defaults to False. |
None
|
client
|
DorsalClient
|
An existing |
None
|
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
If the API call fails. |
NotFoundError
|
If no file record with the specified hash is found. |
Initializes a DorsalFile instance by fetching its metadata from DorsalHub.
If _file_record is provided, it initializes from that data directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_string
|
str
|
The hash string (e.g., "sha256:value" or just "value" for SHA-256) of the file to fetch. |
required |
private (optional)
|
By default (None) will attempt to download the private file record, and fall back on the public, before raising an error if neither are found. |
required | |
client
|
DorsalClient | None
|
An optional DorsalClient instance for API communication. If None, a globally shared DorsalClient instance will be used. |
None
|
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
If an error occurs during instantiation. |
TypeError
|
If hash_string is not a string. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
add_private_tag
Adds a single private tag to the remote file record on DorsalHub.
This method makes a direct API call. On success, the local object is refreshed to reflect the new state on the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the tag. |
required |
value
|
Any
|
The value for the tag (str, bool, int, etc.). |
required |
api_key
|
str | None
|
An optional API key for this specific request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DorsalFile |
DorsalFile
|
The instance of the class, allowing for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tag data fails Pydantic validation. |
TaggingError
|
If the server is unable to apply the tag. |
DorsalClientError
|
For underlying client, network, or API errors. |
Example
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
add_public_tag
Adds a single public tag to the remote file record on DorsalHub.
This method makes a direct API call. On success, the local object is refreshed to reflect the new state on the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the tag (typically 3-64 alphanumeric characters and underscores). |
required |
value
|
Any
|
Value of the tag (str, bool, datetime, int, or float). |
required |
auto_validate
|
If True, immediately validates the tag against the DorsalHub API (requires internet connection). Defaults to False (lazy validation). |
required | |
api_key
|
str | None
|
Optional API key to use if auto_validate is True. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DorsalFile |
DorsalFile
|
The instance of the class, allowing for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tag data fails Pydantic validation. |
TaggingError
|
If the server is unable to apply the tag. |
DorsalClientError
|
For underlying client, network, or API errors. |
Example
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
add_tags
Adds multiple tags to the remote file record in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public
|
dict[str, Any] | None
|
A dictionary of public tags to add {name: value}. |
None
|
private
|
dict[str, Any] | None
|
A dictionary of private tags to add {name: value}. |
None
|
api_key
|
str | None
|
An optional API key for this specific request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DorsalFile |
DorsalFile
|
The instance of the class, refreshed with the new tags. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tag names or values are invalid. |
DorsalClientError
|
If the API call fails. |
Example
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
delete
Deletes this file's record and/or associated data from DorsalHub with granular control.
This method is context-aware. If no scope options are provided, it
derives a default behavior from how the object was initialized:
- If initialized with private=True, it defaults to deleting only
private data (record="private", tags="private", etc.).
- If initialized with private=False, it defaults to deleting only
public data.
- If initialized with private=None (agnostic), it defaults to a
"full clean" (record="all", etc.).
You can override this default behavior by providing explicit scope arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Scope
|
Specifies which record(s) to delete. If None, uses the context-aware default. |
None
|
tags
|
Scope
|
Specifies which tags to delete. If None, uses the context-aware default. |
None
|
annotations
|
Scope
|
Specifies which annotations to delete. If None, uses the context-aware default. |
None
|
api_key
|
str | None
|
An optional API key for this specific request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FileDeleteResponse |
'FileDeleteResponse'
|
A detailed report of the deletion operation. |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
If the remote deletion fails due to an API, network, or authentication error. |
DorsalError
|
If this method is called on an already deleted object. |
Example
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 | |
delete_tag
Deletes a specific tag from the remote file record using its unique ID.
This method makes a direct API call to delete the tag. On success, the local object is automatically refreshed with the latest data from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag_id
|
str
|
The unique 24-character ID of the tag to delete. |
required |
api_key
|
str | None
|
An optional API key for this specific request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DorsalFile |
DorsalFile
|
The instance of the class, allowing for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided tag_id is not a valid format. |
DorsalClientError
|
For underlying client, network, or API errors, including NotFoundError if the tag does not exist or the user does not have permission to delete it. |
Example
from dorsal import DorsalFile
# Assume dorsal_file is an initialized DorsalFile object with tags
dorsal_file = DorsalFile(hash_string="YOUR_FILE_HASH_HERE")
if dorsal_file.tags:
# Get the ID of the first tag on the file
id_to_delete = dorsal_file.tags[0].id
print(f"Attempting to delete tag with ID: {id_to_delete}")
dorsal_file.delete_tag(tag_id=id_to_delete)
print("Tag deleted successfully.")
else:
print("File has no tags to delete.")
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
from_record
classmethod
Alternative constructor to create a DorsalFile instance from an already-fetched FileRecordDateTime object.
This avoids making an additional API call to download the record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
'FileRecordDateTime'
|
The FileRecordDateTime Pydantic model instance. |
required |
client
|
DorsalClient | None
|
An optional DorsalClient instance to attach to the object. |
None
|
Returns:
| Type | Description |
|---|---|
'DorsalFile'
|
An initialized DorsalFile instance. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
get_annotations
Retrieves a list of annotations (or stubs) from the remote record, by schema_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_id
|
str
|
The unique identifier of the dataset/schema (e.g. 'open/classification'). |
required |
source_id
|
str | None
|
Optional. Filter annotations by their source ID. |
None
|
Returns:
| Type | Description |
|---|---|
Sequence[FileAnnotationStub | PDFValidationModel | MediaInfoValidationModel | EbookValidationModel | OfficeDocumentValidationModel]
|
A list of FileAnnotationStub objects (for custom schemas) or Core Models. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
refresh
Reloads the file record's data from DorsalHub.
This method re-fetches the record from the API, updating the object's attributes with the latest data from the server.
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
If the API call fails. |
NotFoundError
|
If the file record can no longer be found on the server. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
set_validation_hash
Sets the BLAKE3 validation hash, potentially upgrading the model.
This method validates the format of the provided BLAKE3 hash string.
If valid, it updates the instance's validation_hash and the
underlying Pydantic model (self.model). The model is explicitly
re-instantiated to ensure all Pydantic validations run.
If the DorsalFile instance's model (initially FileRecordDateTime)
has its annotations field populated, setting this validation_hash
will upgrade self.model to FileRecordStrict. Otherwise, self.model
will remain FileRecordDateTime but with its validation_hash field now set.
This method is specific to DorsalFile and allows users to provide
the BLAKE3 hash to "validate" possession of a file, as the server
does not disclose this hash for FileRecordDateTime downloads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation_hash
|
str
|
The candidate string for the BLAKE3 validation hash. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
RuntimeError
|
For unexpected errors during the process. |
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
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 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
dorsal.file.dorsal_file.FileAnnotationStub
Interactive AnnotationStub.
Download the full annotation with download method.
Source code in venv/lib/python3.13/site-packages/dorsal/file/dorsal_file.py
download
Fetches full annotation record from DorsalHub.
Returns:
| Name | Type | Description |
|---|---|---|
FileAnnotationResponse |
FileAnnotationResponse
|
A Pydantic model of the full annotation record. |
Raises:
| Type | Description |
|---|---|
DorsalError
|
If the |
DorsalClientError
|
If the API call fails for any reason. |