from __future__ import annotations import hashlib import importlib.util from pathlib import Path import sys from typing import Any def _find_repo_root() -> Path: current = Path(__file__).resolve().parent for parent in current.parents: if (parent / "codehygiene" / "__init__.py").is_file(): return parent return current.parent.parent REPO_ROOT = _find_repo_root() LOCAL_PACKAGE = REPO_ROOT / "codehygiene" / "__init__.py" if importlib.util.find_spec("codehygiene") is None and LOCAL_PACKAGE.is_file(): sys.path.insert(0, str(REPO_ROOT)) import codehygiene WEIGHTS_PATH = Path(codehygiene.__file__).with_name("weights.npz") SCORER_VERSION = str(codehygiene.__version__) # Version plus a weights digest: a recompile that keeps the version string still counts as a new scorer. SCORER_ID = f"{SCORER_VERSION}+{hashlib.sha256(WEIGHTS_PATH.read_bytes()).hexdigest()[:12]}" def score_dict(content: str, threshold: float) -> dict[str, Any]: return codehygiene.score(content, threshold=threshold).to_dict() def score_value(content: str) -> float: return float(codehygiene.score(content).score) def score_file(content: str) -> tuple[float, dict[str, int]]: result = codehygiene.score(content) counts: dict[str, int] = {} for squiggle in result.squiggles: counts[squiggle.kind] = counts.get(squiggle.kind, 0) + 1 return float(result.score), counts