File size: 9,984 Bytes
13fe504 | 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 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 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 | """Shared helpers for DataForge CLI commands."""
from __future__ import annotations
from collections.abc import Iterable
from importlib import resources
from pathlib import Path
from typing import cast
import typer
import yaml
from dataforge.table import Table
from dataforge.table import read_csv as read_table_csv
from dataforge.verifier.schema import (
AcceptedValues,
AggregateDependency,
AggregateLiteral,
DomainBound,
FunctionalDependency,
RegexConstraint,
RelationshipConstraint,
Schema,
)
_PACKAGED_DEMO_FIXTURES = {
"fixtures/hospital_10rows.csv": "fixtures/hospital_10rows.csv",
"fixtures/hospital_schema.yaml": "fixtures/hospital_schema.yaml",
}
def resolve_cli_path(path: Path) -> Path:
"""Resolve a user path, including DataForge's packaged demo fixture aliases."""
if path.exists():
return path
normalized = path.as_posix().replace("\\", "/").lstrip("./")
packaged_name = _PACKAGED_DEMO_FIXTURES.get(normalized)
if packaged_name is None:
return path
fixture = resources.files("dataforge").joinpath(packaged_name)
if not fixture.is_file():
return path
return Path(str(fixture))
def schema_from_mapping(raw_mapping: object) -> Schema:
"""Build a Schema from a raw YAML mapping-like payload.
Args:
raw_mapping: Untrusted YAML-decoded value.
Returns:
Parsed Schema object.
Raises:
typer.BadParameter: If the payload is not a mapping.
"""
if raw_mapping is None:
mapping: dict[str, object] = {}
elif isinstance(raw_mapping, dict):
mapping = raw_mapping
else:
raise typer.BadParameter("Schema payload must be a YAML mapping.")
columns: dict[str, str] = {}
raw_columns = mapping.get("columns", {})
if isinstance(raw_columns, dict):
columns = {str(key): str(value) for key, value in raw_columns.items()}
fds: list[FunctionalDependency] = []
raw_fds = mapping.get("functional_dependencies", [])
if isinstance(raw_fds, list):
for raw_fd in raw_fds:
if not isinstance(raw_fd, dict):
continue
raw_determinant = raw_fd.get("determinant", [])
determinant_values = (
tuple(str(value) for value in raw_determinant)
if isinstance(raw_determinant, Iterable)
and not isinstance(raw_determinant, (str, bytes))
else ()
)
fds.append(
FunctionalDependency(
determinant=determinant_values,
dependent=str(raw_fd.get("dependent", "")),
)
)
raw_pii_columns = mapping.get("pii_columns", [])
pii_columns = (
frozenset(str(value) for value in raw_pii_columns)
if isinstance(raw_pii_columns, Iterable) and not isinstance(raw_pii_columns, (str, bytes))
else frozenset()
)
raw_primary_key_columns = mapping.get("primary_key_columns", [])
primary_key_columns = (
frozenset(str(value) for value in raw_primary_key_columns)
if isinstance(raw_primary_key_columns, Iterable)
and not isinstance(raw_primary_key_columns, (str, bytes))
else frozenset()
)
raw_not_null_columns = mapping.get("not_null_columns", [])
not_null_columns = (
frozenset(str(value) for value in raw_not_null_columns)
if isinstance(raw_not_null_columns, Iterable)
and not isinstance(raw_not_null_columns, (str, bytes))
else frozenset()
)
raw_unique_columns = mapping.get("unique_columns", [])
unique_columns = (
frozenset(str(value) for value in raw_unique_columns)
if isinstance(raw_unique_columns, Iterable)
and not isinstance(raw_unique_columns, (str, bytes))
else frozenset()
)
accepted_values: list[AcceptedValues] = []
raw_accepted_values = mapping.get("accepted_values", {})
if isinstance(raw_accepted_values, dict):
for column, values in raw_accepted_values.items():
if isinstance(values, Iterable) and not isinstance(values, (str, bytes)):
accepted_values.append(
AcceptedValues(
column=str(column),
values=tuple(str(value) for value in values),
)
)
elif isinstance(raw_accepted_values, list):
for raw_rule in raw_accepted_values:
if not isinstance(raw_rule, dict):
continue
raw_values = raw_rule.get("values", [])
if isinstance(raw_values, Iterable) and not isinstance(raw_values, (str, bytes)):
accepted_values.append(
AcceptedValues(
column=str(raw_rule.get("column", "")),
values=tuple(str(value) for value in raw_values),
)
)
regex_constraints: list[RegexConstraint] = []
raw_regex_constraints = mapping.get("regex_constraints", {})
if isinstance(raw_regex_constraints, dict):
for column, pattern in raw_regex_constraints.items():
regex_constraints.append(RegexConstraint(column=str(column), pattern=str(pattern)))
elif isinstance(raw_regex_constraints, list):
for raw_rule in raw_regex_constraints:
if isinstance(raw_rule, dict):
regex_constraints.append(
RegexConstraint(
column=str(raw_rule.get("column", "")),
pattern=str(raw_rule.get("pattern", "")),
)
)
relationships: list[RelationshipConstraint] = []
raw_relationships = mapping.get("relationships", [])
if isinstance(raw_relationships, list):
for raw_rule in raw_relationships:
if not isinstance(raw_rule, dict):
continue
relationships.append(
RelationshipConstraint(
column=str(raw_rule.get("column", "")),
reference=str(raw_rule.get("reference", "")),
reference_column=str(raw_rule.get("reference_column", "")),
)
)
bounds: list[DomainBound] = []
raw_bounds = mapping.get("domain_bounds", {})
if isinstance(raw_bounds, dict):
for column, bound_payload in raw_bounds.items():
if not isinstance(bound_payload, dict):
continue
bounds.append(
DomainBound(
column=str(column),
min_value=(
float(bound_payload["min"])
if bound_payload.get("min") is not None
else None
),
max_value=(
float(bound_payload["max"])
if bound_payload.get("max") is not None
else None
),
inclusive_min=bool(bound_payload.get("inclusive_min", True)),
inclusive_max=bool(bound_payload.get("inclusive_max", True)),
)
)
aggregate_dependencies: list[AggregateDependency] = []
raw_aggregates = mapping.get("aggregate_dependencies", [])
if isinstance(raw_aggregates, list):
for raw_dependency in raw_aggregates:
if not isinstance(raw_dependency, dict):
continue
raw_aggregate = str(raw_dependency.get("aggregate", "")).lower()
if raw_aggregate not in {"sum", "avg"}:
continue
raw_group_by = raw_dependency.get("group_by", [])
group_by = (
tuple(str(value) for value in raw_group_by)
if isinstance(raw_group_by, Iterable) and not isinstance(raw_group_by, (str, bytes))
else ()
)
aggregate_dependencies.append(
AggregateDependency(
source_column=str(raw_dependency.get("source_column", "")),
aggregate=cast(AggregateLiteral, raw_aggregate),
target_column=str(raw_dependency.get("target_column", "")),
group_by=group_by,
)
)
return Schema(
columns=columns,
functional_dependencies=tuple(fds),
pii_columns=pii_columns,
primary_key_columns=primary_key_columns,
not_null_columns=not_null_columns,
unique_columns=unique_columns,
accepted_values=tuple(accepted_values),
regex_constraints=tuple(regex_constraints),
relationships=tuple(relationships),
domain_bounds=tuple(bounds),
aggregate_dependencies=tuple(aggregate_dependencies),
)
def load_schema(schema_path: Path) -> Schema:
"""Load a Schema from a YAML file.
Args:
schema_path: Path to the YAML schema file.
Returns:
Parsed Schema object.
Raises:
typer.BadParameter: If the schema file is malformed or unreadable.
"""
try:
raw = yaml.safe_load(schema_path.read_text(encoding="utf-8"))
except OSError as exc:
raise typer.BadParameter(f"Could not read schema file '{schema_path}': {exc}") from exc
if raw is not None and not isinstance(raw, dict):
raise typer.BadParameter(f"Schema file '{schema_path}' must be a YAML mapping.")
return schema_from_mapping(raw)
def read_csv(path: Path) -> Table:
"""Read a CSV using conservative string-preserving defaults.
Args:
path: CSV path.
Returns:
A string-preserving DataForge table.
"""
return read_table_csv(path)
|