Source code for dartfx.workspace.models

"""
Pydantic models for dartfx-workspace.
"""

from datetime import UTC, datetime
from enum import StrEnum
from pathlib import Path
from uuid import UUID

from pydantic import BaseModel, Field

from dartfx.workspace.sniffers import FileFormat, FileType


[docs] class AccessTier(StrEnum): """Authentication/authorization tier for a caller principal.""" ANONYMOUS = "anonymous" USER = "user" ADMIN = "admin"
[docs] class Visibility(StrEnum): """Visibility scope for spaces and workspaces.""" PUBLIC = "public" TEAM = "team" PRIVATE = "private"
[docs] class Action(StrEnum): """Actions guarded by AccessPolicy.""" READ = "read" WRITE = "write" EXECUTE = "execute" ADMIN = "admin"
[docs] class Principal(BaseModel): """Identity and authorization context for programmatic or API callers.""" id: str = "anonymous" tier: AccessTier = AccessTier.ANONYMOUS label: str = "Anonymous" team_ids: list[str] = Field(default_factory=list) allowed_space_ids: list[str] = Field(default_factory=list)
[docs] class FileResource(BaseModel): """ Metadata representation of a file resource in the workspace. Maintains a stable UUID that persists across renames/moves within the workspace. """ id: UUID = Field(description="Stable URI component for the RDF Knowledge Base (urn:uuid:<id>)") path: Path = Field(description="Relative path of the file to the workspace root") size_bytes: int = Field(description="File size in bytes") created_at: datetime = Field(description="File creation timestamp") updated_at: datetime = Field(description="File last modification timestamp") blake3_hash: str = Field(description="BLAKE3 hash string for file integrity and deduplication/rename tracking") type: FileType = Field(default=FileType.OTHER, description="Controlled vocabulary type of the file") file_format: FileFormat = Field(default=FileFormat.UNDETERMINED, description="Specific detected file format") is_generated: bool = Field(default=False, description="Whether this file was generated by system tools/workflows") generated_by: str | None = Field(default=None, description="Tool that generated this file (e.g. qsv, readstat)") source_uuid: str | None = Field( default=None, description="UUID of the source file from which this artifact was derived" ) applicable_actions: list[str] = Field(default_factory=list, description="Actions applicable to this resource")
[docs] class FileTypeStats(BaseModel): count: int = 0 size_bytes: int = 0
[docs] class WorkspaceStats(BaseModel): """ Aggregation statistics for a workspace. """ total_files: int total_size_bytes: int registered_count: int registered_size_bytes: int unregistered_count: int unregistered_size_bytes: int types_info: dict[FileType, FileTypeStats]
[docs] class FileEntry(BaseModel): """Structured directory/file listing entry with Knowledge Base enrichment.""" name: str path: str is_dir: bool size_bytes: int modified_at: datetime registered: bool uuid: str | None = None file_type: str = "-" file_format: str = "undetermined" mime_type: str | None = None is_generated: bool = False generated_by: str | None = None source_uuid: str | None = None fairify_status: str | None = None applicable_actions: list[str] = Field(default_factory=list)
[docs] class TreeNode(BaseModel): """Recursive tree representation of workspace contents.""" name: str path: str is_dir: bool registered: bool = False uuid: str | None = None file_type: str = "-" file_format: str = "undetermined" is_generated: bool = False source_uuid: str | None = None applicable_actions: list[str] = Field(default_factory=list) children: list["TreeNode"] = Field(default_factory=list)
[docs] class ResourceDetail(BaseModel): """Detailed Knowledge Base metadata and format-specific attributes for a file resource.""" uuid: str path: str size_bytes: int blake3_hash: str type: str file_format: str mime_type: str | None = None created_at: str updated_at: str is_generated: bool = False generated_by: str | None = None source_uuid: str | None = None fairify_status: str | None = None applicable_actions: list[str] = Field(default_factory=list) attributes: dict[str, str] = Field(default_factory=dict)
[docs] class FilePreview(BaseModel): """Head or tail text preview of a workspace file.""" path: str mode: str lines_requested: int lines: list[str] total_size_bytes: int
[docs] class SpaceConfig(BaseModel): """Configuration metadata for a virtual Space grouping workspaces.""" id: str slug: str name: str description: str = "" root_path: Path storage_quota_bytes: int | None = None visibility: Visibility = Visibility.PUBLIC
[docs] class SpaceStats(BaseModel): """Aggregate usage and quota telemetry for a Space.""" space_id: str workspace_count: int total_files: int total_size_bytes: int storage_quota_bytes: int | None = None quota_used_pct: float | None = None
[docs] class WorkspaceConfig(BaseModel): """Configuration metadata and ownership settings for a managed Workspace.""" id: str slug: str name: str path: Path space_id: str | None = None visibility: Visibility = Visibility.PUBLIC is_ephemeral: bool = False storage_quota_bytes: int | None = None owner_id: str | None = None team_id: str | None = None
[docs] class TaskType(StrEnum): """Supported asynchronous workspace task types.""" SCAN = "scan" FAIRIFY = "fairify" READSTAT = "readstat" TOCSV = "tocsv"
[docs] class TaskStatus(StrEnum): """Lifecycle status for asynchronous workspace tasks.""" PENDING = "pending" RUNNING = "running" COMPLETED = "completed" FAILED = "failed" CANCELLED = "cancelled"
[docs] class TaskEvent(BaseModel): """Structured event emitted during asynchronous task execution.""" task_id: str workspace_id: str task_type: TaskType status: TaskStatus progress_pct: float = 0.0 current_step: str = "" log_line: str | None = None result: dict[str, str | int | float | bool | list[str] | None] | None = None error: str | None = None timestamp: datetime = Field(default_factory=lambda: datetime.now(UTC))
[docs] class SparqlResult(BaseModel): """Tabular result of a read-only SPARQL query against the Knowledge Base.""" columns: list[str] rows: list[dict[str, str | None]] row_count: int
[docs] class TabularQueryResult(BaseModel): """Tabular result of an analytical SQL query against workspace data files.""" columns: list[str] rows: list[dict[str, str | int | float | bool | None]] row_count: int