File size: 2,278 Bytes
6d32faf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Configuration models and loaders for CLI commands."""

from __future__ import annotations

import json
import tomllib
from datetime import date
from pathlib import Path
from typing import Any, Dict, Optional

from pydantic import BaseModel, Field, field_validator

# Configuration Models

class GenerateConfig(BaseModel):
    """Configuration for case generation command."""
    n_cases: int = Field(10000, ge=1)
    start: date = Field(..., description="Case filing start date")
    end: date = Field(..., description="Case filing end date")
    output: Path = Path("data/generated/cases.csv")
    seed: int = 42

    @field_validator("end")
    @classmethod
    def _check_range(cls, v: date, info):
        return v


class SimulateConfig(BaseModel):
    """Configuration for simulation command."""
    cases: Path = Path("data/generated/cases.csv")
    days: int = Field(384, ge=1)
    start: Optional[date] = None
    policy: str = Field("readiness", pattern=r"^(readiness|fifo|age)$")
    seed: int = 42
    duration_percentile: str = Field("median", pattern=r"^(median|p90)$")
    courtrooms: int = Field(5, ge=1)
    daily_capacity: int = Field(151, ge=1)
    log_dir: Optional[Path] = None


class WorkflowConfig(BaseModel):
    """Configuration for full workflow command."""
    generate: GenerateConfig
    simulate: SimulateConfig


# Configuration Loaders

def _read_config(path: Path) -> Dict[str, Any]:
    """Read configuration from .toml or .json file."""
    suf = path.suffix.lower()
    if suf == ".json":
        return json.loads(path.read_text(encoding="utf-8"))
    if suf == ".toml":
        return tomllib.loads(path.read_text(encoding="utf-8"))
    raise ValueError(f"Unsupported config format: {path.suffix}. Use .toml or .json")


def load_generate_config(path: Path) -> GenerateConfig:
    """Load generation configuration from file."""
    data = _read_config(path)
    return GenerateConfig(**data)


def load_simulate_config(path: Path) -> SimulateConfig:
    """Load simulation configuration from file."""
    data = _read_config(path)
    return SimulateConfig(**data)


def load_workflow_config(path: Path) -> WorkflowConfig:
    """Load workflow configuration from file."""
    data = _read_config(path)
    return WorkflowConfig(**data)