utils/sql_drills/sql_drills/_store.py
Krzysztof kuhy Rudnicki f865508eb4
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
sql-drills pre-commit / pre-commit (push) Waiting to run
sql-drills tests / test (push) Waiting to run
Add sql_drills engine package
Shared MySQL drill engine extracted from testsAndMisc: sandboxed
per-run db and user, exact-match cell diff, predict drills (builtin
theory set incl. an error drill), save_generated validation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYGW5NZL5uVpnqmNKo
2026-11-02 13:53:39 +01:00

97 lines
2.5 KiB
Python

"""Drill folders: ``meta.json``, ``DOCS-task.md``, ``schema.sql``, ``expected.txt``."""
from __future__ import annotations
from dataclasses import dataclass
import json
from typing import TYPE_CHECKING
from sql_drills import _paths
from sql_drills._model import (
TOPICS,
Drill,
DrillMeta,
Table,
UnknownDrillError,
)
from sql_drills._normalize import Rows, parse_rows
if TYPE_CHECKING:
from pathlib import Path
from sql_drills._model import Source
PREVIEW_ROWS = 5
def topics() -> list[str]:
return list(TOPICS)
@dataclass(frozen=True)
class Record:
meta: DrillMeta
folder: Path
error_code: int | None
error_message: str | None
def text(self, name: str) -> str:
return (self.folder / name).read_text()
def expected_rows(self) -> Rows:
return parse_rows(self.text("expected.txt"))
def _record(folder: Path, source: Source) -> Record:
info = json.loads((folder / "meta.json").read_text())
title = (folder / "DOCS-task.md").read_text().splitlines()[0].lstrip("# ")
meta = DrillMeta(folder.name, title, info["topic"], info["kind"], source)
return Record(meta, folder, info.get("error_code"), info.get("error_message"))
def _scan(root: Path, source: Source) -> list[Record]:
if not root.is_dir():
return []
return [
_record(folder, source)
for folder in sorted(root.iterdir())
if (folder / "meta.json").is_file()
]
def records() -> list[Record]:
return _scan(_paths.builtin_dir(), "builtin") + _scan(
_paths.generated_dir(), "generated"
)
def find(key: str) -> Record:
found = records()
for rec in found:
if rec.meta.id == key:
return rec
for rec in found:
if rec.meta.source == "builtin" and rec.meta.id.split("_", 1)[0] == key:
return rec
msg = f"unknown drill: {key}"
raise UnknownDrillError(msg)
def list_drills() -> list[DrillMeta]:
return [rec.meta for rec in records()]
def get_drill(key: str) -> Drill:
rec = find(key)
raw = json.loads(rec.text("tables.json"))
tables = {name: Table(t["columns"], t["rows"]) for name, t in raw.items()}
predict = rec.meta.kind == "predict"
return Drill(
meta=rec.meta,
task_md=rec.text("DOCS-task.md"),
schema_sql=rec.text("schema.sql"),
tables=tables,
expected_preview=[] if predict else rec.expected_rows()[:PREVIEW_ROWS],
query=rec.text("query.sql").strip() if predict else None,
)