|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Callable, Iterable, Mapping |
| 4 | +from dataclasses import dataclass |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import polars as pl |
| 9 | + |
| 10 | +from .base import BaseRule, RuleSeverity |
| 11 | +from .rules import ( |
| 12 | + AcceptedValuesRule, |
| 13 | + ExpressionRule, |
| 14 | + NotNullRule, |
| 15 | + RelationshipRule, |
| 16 | + UniqueRule, |
| 17 | +) |
| 18 | + |
| 19 | +RuleFactory = Callable[[Mapping[str, Any]], BaseRule] |
| 20 | + |
| 21 | + |
| 22 | +@dataclass(frozen=True) |
| 23 | +class RuleDefinition: |
| 24 | + name: str |
| 25 | + description: str |
| 26 | + tags: frozenset[str] |
| 27 | + builder: RuleFactory |
| 28 | + |
| 29 | + |
| 30 | +_REGISTRY: dict[str, RuleDefinition] = {} |
| 31 | + |
| 32 | + |
| 33 | +def _register_builtin( |
| 34 | + name: str, |
| 35 | + builder: RuleFactory, |
| 36 | + *, |
| 37 | + description: str, |
| 38 | + tags: Iterable[str], |
| 39 | +) -> None: |
| 40 | + register_rule(name, builder, description=description, tags=tags) |
| 41 | + |
| 42 | + |
| 43 | +def register_rule( |
| 44 | + name: str, |
| 45 | + builder: RuleFactory, |
| 46 | + *, |
| 47 | + description: str = "", |
| 48 | + tags: Iterable[str] | None = None, |
| 49 | +) -> None: |
| 50 | + key = name.lower() |
| 51 | + if key in _REGISTRY: |
| 52 | + raise ValueError(f"rule '{name}' is already registered") |
| 53 | + _REGISTRY[key] = RuleDefinition( |
| 54 | + name=name, |
| 55 | + description=description, |
| 56 | + tags=frozenset(tags or ()), |
| 57 | + builder=builder, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def get_rule(name: str) -> RuleDefinition: |
| 62 | + try: |
| 63 | + return _REGISTRY[name.lower()] |
| 64 | + except KeyError as exc: # pragma: no cover - defensive |
| 65 | + raise KeyError(f"unknown rule type: {name}") from exc |
| 66 | + |
| 67 | + |
| 68 | +def list_rules(tag: str | None = None) -> list[RuleDefinition]: |
| 69 | + definitions = _REGISTRY.values() |
| 70 | + if tag: |
| 71 | + tag = tag.lower() |
| 72 | + definitions = [ |
| 73 | + definition for definition in definitions if tag in definition.tags |
| 74 | + ] |
| 75 | + return sorted(definitions, key=lambda definition: definition.name) |
| 76 | + |
| 77 | + |
| 78 | +def _resolve_severity(config: Mapping[str, Any]) -> RuleSeverity: |
| 79 | + level = config.get("severity") |
| 80 | + if not level: |
| 81 | + return RuleSeverity.ERROR |
| 82 | + try: |
| 83 | + return RuleSeverity(level.lower()) |
| 84 | + except ValueError as exc: |
| 85 | + raise ValueError(f"unknown severity '{level}'") from exc |
| 86 | + |
| 87 | + |
| 88 | +def _resolve_description( |
| 89 | + config: Mapping[str, Any], |
| 90 | + fallback: str, |
| 91 | +) -> str: |
| 92 | + return config.get("description") or fallback |
| 93 | + |
| 94 | + |
| 95 | +def _build_not_null(config: Mapping[str, Any]) -> BaseRule: |
| 96 | + return NotNullRule( |
| 97 | + column=config["column"], |
| 98 | + severity=_resolve_severity(config), |
| 99 | + description=_resolve_description(config, f"NotNull on {config['column']}"), |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def _build_unique(config: Mapping[str, Any]) -> BaseRule: |
| 104 | + return UniqueRule( |
| 105 | + column=config["column"], |
| 106 | + severity=_resolve_severity(config), |
| 107 | + description=_resolve_description(config, f"Unique on {config['column']}"), |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def _build_accepted_values(config: Mapping[str, Any]) -> BaseRule: |
| 112 | + return AcceptedValuesRule( |
| 113 | + column=config["column"], |
| 114 | + allowed_values=config["allowed_values"], |
| 115 | + severity=_resolve_severity(config), |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def _build_relationship(config: Mapping[str, Any]) -> BaseRule: |
| 120 | + reference_cfg = config["reference"] |
| 121 | + ref_path = Path(reference_cfg["path"]) |
| 122 | + ref_format = reference_cfg.get("format", "parquet") |
| 123 | + if ref_format == "parquet": |
| 124 | + reference_df = pl.read_parquet(ref_path) |
| 125 | + elif ref_format == "csv": |
| 126 | + reference_df = pl.read_csv(ref_path) |
| 127 | + else: # pragma: no cover - validated via config tests |
| 128 | + raise ValueError(f"unsupported reference format: {ref_format}") |
| 129 | + return RelationshipRule( |
| 130 | + column=config["column"], |
| 131 | + reference_df=reference_df, |
| 132 | + reference_column=reference_cfg["column"], |
| 133 | + severity=_resolve_severity(config), |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +def _build_expression(config: Mapping[str, Any]) -> BaseRule: |
| 138 | + return ExpressionRule( |
| 139 | + expression=config["expression"], |
| 140 | + severity=_resolve_severity(config), |
| 141 | + description=_resolve_description( |
| 142 | + config, |
| 143 | + f"Expression rule {config['expression']}", |
| 144 | + ), |
| 145 | + ) |
| 146 | + |
| 147 | + |
| 148 | +_register_builtin( |
| 149 | + name="not_null", |
| 150 | + builder=_build_not_null, |
| 151 | + description="Fails when the specified column contains null values.", |
| 152 | + tags=("nulls", "integrity"), |
| 153 | +) |
| 154 | +_register_builtin( |
| 155 | + name="unique", |
| 156 | + builder=_build_unique, |
| 157 | + description="Fails when duplicate values are detected in the column.", |
| 158 | + tags=("uniqueness", "integrity"), |
| 159 | +) |
| 160 | +_register_builtin( |
| 161 | + name="accepted_values", |
| 162 | + builder=_build_accepted_values, |
| 163 | + description="Ensures all column values are part of an allowed set.", |
| 164 | + tags=("reference", "categorical"), |
| 165 | +) |
| 166 | +_register_builtin( |
| 167 | + name="relationship", |
| 168 | + builder=_build_relationship, |
| 169 | + description="Verifies referential integrity with an on-disk reference dataset.", |
| 170 | + tags=("reference", "integrity"), |
| 171 | +) |
| 172 | +_register_builtin( |
| 173 | + name="expression", |
| 174 | + builder=_build_expression, |
| 175 | + description="Evaluates a boolean Polars expression defined as a string.", |
| 176 | + tags=("expression", "flexible"), |
| 177 | +) |
0 commit comments