High Level Functions
These functions provide a high-level interface for generating and managing file records, both locally and on DorsalHub.
dorsal.api
activate_model_by_name
Activates (enables) a model in the pipeline by name.
Source code in venv/lib/python3.12/site-packages/dorsal/api/config.py
add_files_to_collection
Adds a list of files to a remote collection by their hash.
This function automatically handles batching for large lists of hashes to comply with the API limit (10,000 per request).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to modify. |
required |
hashes
|
list[str]
|
A list of SHA-256 file hashes to add. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
AddFilesResponse |
AddFilesResponse
|
A consolidated response summarizing the results of all batches. |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
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 | |
add_tag_to_file
Adds a single tag to a file record on DorsalHub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_string
|
str
|
The hash of the file record to tag. |
required |
name
|
str
|
The name of the tag. |
required |
value
|
Any
|
The value of the tag. |
required |
public
|
bool
|
The visibility of the tag itself. Defaults to False (Private). |
False
|
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FileTagResponse |
FileTagResponse
|
A response object from the API. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
deactivate_model_by_name
Deactivates (disables) a model in the pipeline by name.
Source code in venv/lib/python3.12/site-packages/dorsal/api/config.py
delete_collection
Deletes a file collection from DorsalHub by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to delete. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
delete_private_dorsal_file_record
Delete the private file record for the given hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_hash
|
str
|
The SHA-256 hash of the file record to delete. |
required |
tags
|
DeletionScope | None
|
Specifies which tags to delete. Options: "all", "public", "private", "none", or None (treated as "none"). Defaults to "none". |
'none'
|
annotations
|
DeletionScope | None
|
Specifies which annotations to delete. Options: "all", "public", "private", "none", or None (treated as "none"). Defaults to "none". |
'none'
|
api_key
|
str
|
An API key to use for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FileDeleteResponse |
'FileDeleteResponse'
|
An object summarizing the result of the delete operation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided |
DorsalClientError
|
For API errors (e.g., not found, permission denied). |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
delete_public_dorsal_file_record
Delete the public file record for the given hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_hash
|
str
|
The SHA-256 hash of the file record to delete. |
required |
tags
|
DeletionScope | None
|
Specifies which tags to delete. Options: "all", "public", "private", "none", or None (treated as "none"). Defaults to "none". |
'none'
|
annotations
|
DeletionScope | None
|
Specifies which annotations to delete. Options: "all", "public", "private", "none", or None (treated as "none"). Defaults to "none". |
'none'
|
api_key
|
str
|
An API key to use for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FileDeleteResponse |
'FileDeleteResponse'
|
An object summarizing the result of the delete operation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided |
DorsalClientError
|
For API errors (e.g., not found, permission denied). |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
export_collection
export_collection(
collection_id,
output_path,
poll_interval=5,
timeout=3600,
api_key=None,
console=None,
palette=None,
)
Exports a remote file collection from DorsalHub.
Starts an export job, polls for its completion, and downloads to a local path.
The exported file will be in .json.gz format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The ID of the collection to export. |
required |
output_path
|
str | Path
|
The local path to save the exported file. |
required |
poll_interval
|
int
|
Seconds to wait between status checks. |
5
|
timeout
|
int | None
|
Total seconds to wait for the job to complete. |
3600
|
api_key
|
str
|
An API key for this request. |
None
|
console
|
Console
|
A rich.console.Console for progress display. |
None
|
palette
|
dict
|
Color palette for the progress bar. |
None
|
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
find_duplicates
find_duplicates(
path,
recursive=False,
min_size=0,
max_size=None,
mode="hybrid",
use_cache=True,
progress_console=None,
palette=None,
)
Finds duplicate files in a directory using a multi-pass filtering strategy.
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 | |
generate_html_directory_report
generate_html_directory_report(
dir_path,
output_path=None,
*,
local_collection=None,
template="default",
use_cache=True,
recursive=False
)
Generates a self-contained HTML dashboard for a directory of files.
This function orchestrates the creation of a rich, interactive HTML document. It processes a directory into a LocalFileCollection, generates data for various UI panel based on user configuration, and renders the result using a flexible Jinja2 template system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
The path to the local directory to report on. |
required |
local_collection
|
LocalFileCollection
|
An existing, pre-processed collection can be passed to avoid re-scanning the directory. |
None
|
output_path
|
str
|
If provided, the HTML dashboard will be saved to this file path. |
None
|
template
|
str
|
The name of the template to use. Defaults to "default". |
'default'
|
use_cache
|
bool
|
Whether to use the local cache during file processing. |
True
|
recursive
|
bool
|
Whether to scan the directory recursively. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str | None
|
The generated HTML dashboard as a string. |
Raises:
| Type | Description |
|---|---|
DorsalError
|
If file processing or report generation fails. |
TemplateNotFoundError
|
If the specified template cannot be located. |
FileNotFoundError
|
If the specified |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 | |
generate_html_file_report
generate_html_file_report(
file_path,
*,
local_file=None,
output_path=None,
template="default",
use_cache=True,
api_key=None
)
Generates a self-contained HTML report for a single local file.
This function serves as a high-level entry point to the reporting engine. It
leverages scan_file to perform a full metadata extraction and then renders
the result into a rich, interactive HTML document using a flexible,
user-configurable Jinja2 template system. The final output is a single,
portable HTML file with all CSS and JavaScript embedded.
Example
from dorsal.api import generate_html_file_report
# Generate the report and save it to a file
generate_html_file_report(
"path/to/my_document.pdf",
output_path="report.html"
)
# Generate a report using a custom template and get the HTML as a string
html_content = generate_html_file_report(
"path/to/archive.zip",
template="compact"
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the local file to report on. |
required |
output_path
|
str
|
If provided, the HTML report will be saved to this file path. Defaults to None. |
None
|
template
|
str
|
The name of a built-in/user-defined template or an absolute path to a custom template .html file. Defaults to "default". |
'default'
|
use_cache
|
bool
|
Whether to use the local cache during file processing. Defaults to True. |
True
|
api_key
|
str
|
An API key for operations that may require it. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str | None
|
The generated HTML report as a string. |
Raises:
| Type | Description |
|---|---|
DorsalError
|
If file processing or report generation fails. |
TemplateNotFoundError
|
If the specified template cannot be located. |
FileNotFoundError
|
If the specified |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 | |
get_collection
get_collection(
collection_id: str,
hydrate: Literal[True],
page: int = 1,
per_page: int = 30,
api_key: str | None = None,
*,
mode: Literal["pydantic"]
) -> HydratedSingleCollectionResponse
get_collection(
collection_id: str,
hydrate: Literal[True],
page: int = 1,
per_page: int = 30,
api_key: str | None = None,
*,
mode: Literal["dict"]
) -> dict[str, Any]
get_collection(
collection_id: str,
hydrate: Literal[True],
page: int = 1,
per_page: int = 30,
api_key: str | None = None,
*,
mode: Literal["json"]
) -> str
get_collection(
collection_id: str,
hydrate: Literal[False],
page: int = 1,
per_page: int = 30,
api_key: str | None = None,
*,
mode: Literal["pydantic"]
) -> SingleCollectionResponse
get_collection(
collection_id,
hydrate=False,
page=1,
per_page=30,
api_key=None,
*,
mode="pydantic"
)
Retrieves a specific collection and its contents from DorsalHub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to fetch. |
required |
hydrate
|
bool
|
If True, returns fully detailed file records. Defaults to False. |
False
|
page
|
int
|
The page number for file contents. Defaults to 1. |
1
|
per_page
|
int
|
The number of file records per page. Defaults to 30. |
30
|
api_key
|
str
|
An API key for this request. |
None
|
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return format. |
'pydantic'
|
Returns:
| Type | Description |
|---|---|
SingleCollectionResponse | HydratedSingleCollectionResponse | dict[str, Any] | str
|
The collection data, formatted according to the specified |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors like not found or authentication issues. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 | |
get_dataset
Retrieves the full definition of an existing dataset from DorsalHub.
Fetches a dataset's metadata, including its name, description, schema, and other properties.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_id
|
str
|
The unique identifier for the dataset, in the format "namespace/dataset-name". |
required |
api_key
|
str
|
An API key to use for this request, especially for private datasets. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Dataset |
Dataset
|
A Pydantic model instance representing the full dataset definition. |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If no dataset with the specified ID is found. |
DorsalClientError
|
If the API call fails for any other reason. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/dataset.py
get_dataset_schema
Fetches the JSON schema for a given dataset.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_id
|
str
|
Identifier for a dataset (e.g., "dorsal/arxiv"). |
required |
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
dict[str, Any]: The JSON schema of the dataset. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
DorsalClientError
|
Base client error or for unexpected issues (propagated
from |
AuthError
|
Authentication failure (propagated from |
NotFoundError
|
If the dataset is not found (propagated from |
ForbiddenError
|
Access to the dataset is denied (propagated from |
RateLimitError
|
If the request is rate-limited by the API (propagated from
|
NetworkError
|
If a network issue occurs (propagated from |
APIError
|
For other HTTP errors from the API (propagated from |
ApiDataValidationError
|
If the API response for the dataset is malformed and
cannot be parsed into a valid |
Source code in venv/lib/python3.12/site-packages/dorsal/api/dataset.py
get_directory_info
get_directory_info(
dir_path,
recursive=False,
media_type=True,
progress_console=None,
palette=None,
)
Calculates and returns a detailed summary of a directory using a single-pass method.
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
get_dorsal_file_record
get_dorsal_file_record(
hash_string: str,
mode: Literal["pydantic"],
public: bool | None = None,
api_key: str | None = None,
) -> "FileRecord"
Gets metadata for a file record from DorsalHub
public=None(Default): check for a private record first, and if not found, check for a public one.public=True: get public recordpublic=False: get private record
Example
from dorsal.api import get_file_metadata
# Agnostic search (recommended)
agnostic_file = get_file_metadata("some_hash")
# Public-only search
public_file = get_file_metadata("some_hash", public=True)
# Private-only search as a dictionary
private_file_dict = get_file_metadata("some_hash", public=False, mode="dict")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_string
|
str
|
The hash of the file to fetch (e.g., "sha256:..."). |
required |
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return
format. Defaults to "pydantic", returning a |
'pydantic'
|
public
|
Optional[bool]
|
Controls the search visibility. Defaults to None (agnostic search). |
None
|
api_key
|
str
|
An API key to use for this request, overriding any globally configured key. Defaults to None. |
None
|
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
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 | |
identify_file
identify_file(
file_path: str,
quick: bool = True,
file_size: int | None = None,
*,
mode: Literal["pydantic"],
api_key: str | None = None,
use_cache: bool = True
) -> "FileRecord"
identify_file(
file_path,
quick=True,
file_size=None,
*,
mode="pydantic",
api_key=None,
use_cache=True
)
Gets metadata for a local file from DorsalHub using its content hash.
This function identifies a file by first calculating its hash locally and then querying the remote API for a matching record. It provides an efficient "quick hash" option for large files and falls back to a secure SHA-256 hash. Uses local cache to avoid re-calculating hashes on subsequent calls to same file path.
Example
from dorsal.api import identify_file
try:
# Identify a file, using the fast "quick hash" if possible
record = identify_file("path/to/my_video.mp4", mode="dict")
print(f"Successfully identified '{record['name']}'")
print(f"Dorsal URL: {record['url']}")
except FileNotFoundError:
print("Error: The file could not be found at that path.")
except DorsalClientError as e:
# Catches errors like record not found on the server
print(f"API Error: {e.message}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the local file to identify. |
required |
quick
|
bool
|
If True, attempts to use the faster "quick hash" for files >= 32MiB. Defaults to True. |
True
|
file_size
|
int
|
An optional pre-calculated file size in bytes. If not provided, it will be calculated. Defaults to None. |
None
|
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return format. Defaults to "pydantic". |
'pydantic'
|
api_key
|
str
|
An API key for this request, overriding the client's default. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
FileRecord | dict[str, Any] | str
|
Union[FileRecord, dict, str]: The file record from DorsalHub, formatted
according to the specified |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist at the specified path. |
DorsalClientError
|
For API-level errors, such as |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 | |
index_directory
index_directory(
dir_path,
recursive=False,
*,
public=False,
api_key=None,
use_cache=True,
fail_fast=True,
strict=False
)
Scans a directory and indexes all files to DorsalHub.
This function is a high-level wrapper around the MetadataReader. It performs
three main steps:
1. Scans the directory for files.
2. Generates rich metadata for each file locally (offline).
3. Uploads the records to DorsalHub in managed batches.
It supports a Fail-Fast mode (default) for debugging and a Best-Effort mode for bulk operations.
Example
from dorsal.api import index_directory
from dorsal.common.exceptions import BatchIndexingError
# Scenario 1: Standard usage (Fail-Fast)
try:
summary = index_directory("path/to/project_assets", recursive=True)
print(f"Success! {summary['success']} files indexed.")
except BatchIndexingError as e:
print(f"Indexing failed at batch {e.summary['batches'][-1]['batch_index']}.")
print(f"Error: {e}")
# Scenario 2: Bulk Upload (Best-Effort)
# Continue processing even if individual batches fail.
summary = index_directory(
"path/to/massive_dataset",
recursive=True,
fail_fast=False
)
print(f"Completed. Success: {summary['success']}, Failed: {summary['failed']}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
The path to the directory you want to scan and index. |
required |
recursive
|
bool
|
If True, scans all subdirectories recursively. Defaults to False. |
False
|
public
|
bool
|
If True, all file records will be created as public on DorsalHub. Defaults to False. |
False
|
api_key
|
str | None
|
An API key to use for this operation, overriding the client's default. Defaults to None. |
None
|
use_cache
|
bool
|
If True, uses cached metadata for files that haven't changed. Defaults to True. |
True
|
fail_fast
|
bool
|
If True, raises |
True
|
strict
|
bool
|
If True, raises |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A summary dictionary detailing the results of the operation. Keys: 'total_records', 'processed', 'success', 'failed', 'batches', 'errors'. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the directory does not exist. |
BatchIndexingError
|
If |
PartialIndexingError
|
If |
DorsalClientError
|
For critical errors preventing the operation from starting. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
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 831 832 833 834 | |
index_file
Processes a single local file and uploads its metadata to DorsalHub.
This function provides a simple, one-shot way to get a local file's metadata indexed on the remote server.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the local file to process and index. |
required |
public
|
bool
|
If True, the record will be created as public. Defaults to False (Private). |
False
|
api_key
|
str
|
An API key to use for this specific request. Defaults to None. |
None
|
strict
|
bool
|
If True, raises PartialIndexingError if any part of the request fails (e.g., invalid annotation schema), ensuring zero data loss. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
FileIndexResponse |
FileIndexResponse
|
A response object from the API detailing the result of the indexing operation. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
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 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 | |
list_collections
list_collections(
page: int = 1,
per_page: int = 25,
api_key: str | None = None,
*,
mode: Literal["pydantic"]
) -> CollectionsResponse
Retrieves a paginated list of collections from DorsalHub.
This is a high-level wrapper around the DorsalClient's list_collections method, providing a simple way to access collection information.
Example
from dorsal.api.collection import list_dorsal_collections
# Get the first page of collections as Pydantic objects
response = list_dorsal_collections()
print(f"Found {response.pagination.record_count} total collections.")
for collection in response.results:
print(f"- {collection.name} (ID: {collection.id})")
# Get the second page as a JSON string
response_json = list_dorsal_collections(page=2, mode="json")
print(response_json)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
int
|
The page number for pagination. Defaults to 1. |
1
|
per_page
|
int
|
The number of collections per page. Defaults to 25. |
25
|
api_key
|
str
|
An API key for this request, overriding the client's default. Defaults to None. |
None
|
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return format. Defaults to "pydantic". |
'pydantic'
|
Returns:
| Type | Description |
|---|---|
CollectionsResponse | dict[str, Any] | str
|
Union[CollectionsResponse, dict, str]: The search results, formatted
according to the specified |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For client-side validation errors or API errors like authentication or rate limiting. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 | |
make_collection_private
Makes a public collection private.
This is a high-level wrapper that handles client instantiation and error logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to make private. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CollectionWebLocationResponse |
CollectionWebLocationResponse
|
An object containing the new private web URL. |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors (e.g., ConflictError if already private). |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
make_collection_public
Makes a private collection public.
This is a high-level wrapper that handles client instantiation and error logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to make public. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
CollectionWebLocationResponse |
CollectionWebLocationResponse
|
An object containing the new public web URL. |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors (e.g., ConflictError if already public). |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
make_schema_validator
Fetches a dataset's schema and returns a callable validator function.
This is useful for validating records client-side before attempting to insert them, which can save API calls.
Example
from dorsal.api import make_schema_validator
validator = make_schema_validator("my-org/my-book-collection")
good_record = {"author": "J.R.R. Tolkien", "title": "The Hobbit"}
bad_record = {"author": "J.R.R. Tolkien"} # Missing title
try:
validator(good_record)
print("Good record is valid.")
validator(bad_record)
except Exception as e:
print(f"Bad record is invalid: {e}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_id
|
str
|
Identifier for the dataset whose schema will be used. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
JsonSchemaValidator |
JsonSchemaValidator
|
A callable instance that validates a dictionary record against the fetched schema. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
DorsalClientError
|
Base client error or for unexpected issues (propagated
from |
AuthError
|
Authentication failure (propagated from |
NotFoundError
|
If the dataset is not found (propagated from |
ForbiddenError
|
Access to the dataset is denied (propagated from |
RateLimitError
|
If the request is rate-limited by the API (propagated from
|
NetworkError
|
If a network issue occurs (propagated from |
APIError
|
For other HTTP errors from the API (propagated from |
ApiDataValidationError
|
If the API response for the dataset is malformed and
cannot be parsed into a valid |
JsonSchemaValidationError
|
If the schema is invalid. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/dataset.py
register_model
register_model(
annotation_model,
schema_id,
validation_model=None,
dependencies=None,
options=None,
overwrite=False,
*,
scope="project"
)
Programmatically registers a new annotation model in the dorsal config.
Source code in venv/lib/python3.12/site-packages/dorsal/api/config.py
132 133 134 135 136 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
remove_files_from_collection
Removes a list of files from a remote collection by their hash.
This function automatically handles batching for large lists of hashes to comply with the API limit (10,000 per request).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to modify. |
required |
hashes
|
list[str]
|
A list of SHA-256 file hashes to remove. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
RemoveFilesResponse |
RemoveFilesResponse
|
A consolidated response summarizing the results. |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
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 | |
remove_model_by_name
Removes a model from the pipeline by its name (e.g., "PDFAnnotationModel"). Raises an error if the name is ambiguous (duplicates exist).
Source code in venv/lib/python3.12/site-packages/dorsal/api/config.py
remove_tag_from_file
Removes a specific tag from a file record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hash_string
|
str
|
The hash of the file record. |
required |
tag_id
|
str
|
The unique ID of the tag to remove. |
required |
api_key
|
str
|
An API key for this request. |
None
|
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
scan_directory
scan_directory(
dir_path,
recursive=False,
*,
api_key=None,
use_cache=True,
offline=False,
follow_symlinks=True
)
Scans a directory and returns a list of LocalFile objects.
This function is a high-level wrapper that processes all files in a given directory and generates their metadata offline using the local Annotation Model pipeline.
It returns a list of LocalFile objects.
Example
from dorsal.api import scan_directory
# Scan a directory non-recursively for all files
processed_files = scan_directory("path/to/my_invoices")
print(f"Found {len(processed_files)} files to process.")
# You can now iterate over the list
for f in processed_files:
if f.size > 500000:
print(f"{f.name} is a large file.")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
The path to the directory you want to scan. |
required |
recursive
|
bool
|
If True, scans all subdirectories recursively. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
list[LocalFile]
|
list[LocalFile]: A list of processed |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | |
scan_file
Processes a single file and returns a LocalFile object.
This is a direct wrapper for dorsal.LocalFile. It's a convenient
entry point for processing a single file and accessing its metadata
without needing to import the LocalFile class directly.
Example
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 |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 | |
search_global_files
search_global_files(
query: str,
page: int = 1,
per_page: int = 25,
sort_by: Literal[
"date_modified", "date_created", "size", "name"
] = "date_modified",
sort_order: Literal["asc", "desc"] = "desc",
api_key: str | None = None,
*,
match_any: bool = False,
mode: Literal["pydantic"],
model_dump_exclude_none: bool = True,
model_dump_by_alias: bool = True
) -> "FileSearchResponse"
search_global_files(
query: str,
page: int = 1,
per_page: int = 25,
sort_by: Literal[
"date_modified", "date_created", "size", "name"
] = "date_modified",
sort_order: Literal["asc", "desc"] = "desc",
api_key: str | None = None,
*,
match_any: bool = False,
mode: Literal["dict"],
model_dump_exclude_none: bool = True,
model_dump_by_alias: bool = True
) -> dict[str, Any]
search_global_files(
query: str,
page: int = 1,
per_page: int = 25,
sort_by: Literal[
"date_modified", "date_created", "size", "name"
] = "date_modified",
sort_order: Literal["asc", "desc"] = "desc",
api_key: str | None = None,
*,
match_any: bool = False,
mode: Literal["json"],
model_dump_exclude_none: bool = True,
model_dump_by_alias: bool = True
) -> str
search_global_files(
query,
page=1,
per_page=25,
sort_by="date_modified",
sort_order="desc",
api_key=None,
*,
match_any=False,
mode="pydantic",
model_dump_exclude_none=True,
model_dump_by_alias=True
)
Searches for public file records across the entire DorsalHub platform.
This function provides a simple interface to search all public files. Note: This is a premium feature and requires an appropriate account status.
Example
from dorsal.api import search_global_files
# Find all publicly indexed files tagged with 'research'
try:
response = search_global_files(
query="tag:research",
mode="dict"
)
print(f"Found {response['pagination']['record_count']} public files tagged 'research'.")
for record in response['results']:
print(f"- {record['name']} (hash: {record['hash']})")
except Exception as e:
print(f"An error occurred during search: {e}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The search query string. Supports operators like
|
required |
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return format. Defaults to "pydantic". |
'pydantic'
|
page
|
int
|
The page number for pagination. Defaults to 1. |
1
|
per_page
|
int
|
The number of results per page. Must be between 1 and 50. Defaults to 25. |
25
|
sort_by
|
Literal
|
The field to sort results by. Defaults to 'date_modified'. |
'date_modified'
|
sort_order
|
Literal
|
The sort order ('asc' or 'desc'). Defaults to 'desc'. |
'desc'
|
api_key
|
str | None
|
An API key for this request, overriding the client's default. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
'FileSearchResponse | dict | str'
|
Union[FileSearchResponse, dict, str]: The search results, formatted
according to the specified |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For client-side validation errors or API errors like authentication, rate limiting, or insufficient permissions (e.g. using this feature on a non-premium account). |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 | |
search_user_files
search_user_files(
query: str,
deduplicate: bool = True,
page: int = 1,
per_page: int = 25,
sort_by: Literal[
"date_modified", "date_created", "size", "name"
] = "date_modified",
sort_order: Literal["asc", "desc"] = "desc",
*,
match_any: bool = False,
api_key: str | None = None,
mode: Literal["pydantic"],
model_dump_exclude_none: bool = True,
model_dump_by_alias: bool = True
) -> "FileSearchResponse"
search_user_files(
query: str,
deduplicate: bool = True,
page: int = 1,
per_page: int = 25,
sort_by: Literal[
"date_modified", "date_created", "size", "name"
] = "date_modified",
sort_order: Literal["asc", "desc"] = "desc",
*,
match_any: bool = False,
api_key: str | None = None,
mode: Literal["dict"],
model_dump_exclude_none: bool = True,
model_dump_by_alias: bool = True
) -> dict[str, Any]
search_user_files(
query: str,
deduplicate: bool = True,
page: int = 1,
per_page: int = 25,
sort_by: Literal[
"date_modified", "date_created", "size", "name"
] = "date_modified",
sort_order: Literal["asc", "desc"] = "desc",
*,
match_any: bool = False,
api_key: str | None = None,
mode: Literal["json"],
model_dump_exclude_none: bool = True,
model_dump_by_alias: bool = True
) -> str
search_user_files(
query,
deduplicate=True,
page=1,
per_page=25,
sort_by="date_modified",
sort_order="desc",
*,
match_any=False,
api_key=None,
mode="pydantic",
model_dump_exclude_none=True,
model_dump_by_alias=True
)
Searches for file records indexed by the authenticated user.
The query supports simple text matching as well as advanced operators.
Example
from dorsal.api import search_user_files
# Find all PDF files you have indexed, sorted by name
try:
response = search_user_files(
query="extension:pdf",
sort_by="name",
sort_order="asc",
mode="dict"
)
print(f"Found {response['pagination']['record_count']} matching PDF files.")
for record in response['results']:
print(f"- {record['name']}")
except Exception as e:
print(f"An error occurred during search: {e}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The search query string. Supports operators like
|
required |
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return format. Defaults to "pydantic". |
'pydantic'
|
deduplicate
|
bool
|
If True, returns only unique file records based on their content hash. Defaults to True. |
True
|
page
|
int
|
The page number for pagination. Defaults to 1. |
1
|
per_page
|
int
|
The number of results per page. Must be between 1 and 50. Defaults to 25. |
25
|
sort_by
|
Literal
|
The field to sort results by. Defaults to 'date_modified'. |
'date_modified'
|
sort_order
|
Literal
|
The sort order ('asc' or 'desc'). Defaults to 'desc'. |
'desc'
|
api_key
|
str | None
|
An API key for this request, overriding the client's default. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
'FileSearchResponse | dict | str'
|
Union[FileSearchResponse, dict, str]: The search results, formatted
according to the specified |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For client-side validation errors or API errors like authentication or rate limiting. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/file.py
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 | |
show_model_pipeline
Returns a simplified, human-readable summary of the pipeline.
Source code in venv/lib/python3.12/site-packages/dorsal/api/config.py
update_collection
update_collection(
collection_id: str,
name: str | None = None,
description: str | None = None,
api_key: str | None = None,
*,
mode: Literal["pydantic"]
) -> FileCollection
Updates the metadata of a remote collection on DorsalHub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_id
|
str
|
The unique ID of the collection to update. |
required |
name
|
str
|
The new name for the collection. |
None
|
description
|
str
|
The new description for the collection. |
None
|
api_key
|
str
|
An API key for this request. |
None
|
mode
|
Literal['pydantic', 'dict', 'json']
|
The desired return format. |
'pydantic'
|
Returns:
| Type | Description |
|---|---|
FileCollection | dict[str, Any] | str
|
The updated collection data, formatted according to the specified |
Raises:
| Type | Description |
|---|---|
DorsalClientError
|
For API errors. |
DorsalError
|
For other unexpected library errors. |
Source code in venv/lib/python3.12/site-packages/dorsal/api/collection.py
validate_dataset_records
Validates records against a dataset's JSON schema.
Orchestrates schema retrieval (if a schema is not provided directly), custom validator preparation, and record-by-record validation. Returns a summary of the validation results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_id
|
str
|
Identifier of the dataset. Used to fetch the schema if
|
required |
records
|
list[dict]
|
A list of dictionaries, where each dictionary is a record to be validated. |
required |
schema_dict
|
dict | None
|
Optional. A pre-fetched JSON schema dictionary. If provided,
|
None
|
api_key
|
str | None
|
Optional API key, used by |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A summary of validation results, including counts for total, valid, and invalid records, and detailed error information for each invalid record. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ApiDataValidationError
|
If a schema (fetched or provided) is invalid or
cannot be used to prepare a validator (e.g., due
to |
DorsalClientError
|
(And its subclasses like AuthError, NotFoundError,
NetworkError, APIError, etc.) Propagated if |
JsonSchemaValidationError
|
Propagated from |
Source code in venv/lib/python3.12/site-packages/dorsal/api/dataset.py
258 259 260 261 262 263 264 265 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 | |