"""The catalog: entries, integrity, conflicts. Section 4 of SPECIFICATION.md. An entry is verifiable or it is not in the catalog: a matcher over the syntax tree, the edit pair it produces, a lock test, named refusals. """ import json import os from typing import Any, Dict, List, Optional, Tuple ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PATTERNS_PATH = os.path.join(ROOT, "catalog", "patterns.json") POLICY_PATH = os.path.join(ROOT, "catalog", "conflict_policy.json") REQUIRED_ENTRY_KEYS = ("id", "family", "shape", "effect", "lock", "refusals", "min_assurance", "frames") POLICIES = ("specificity", "family_order", "clarify") MAX_CANDIDATES = 4 class CatalogError(Exception): pass def load_entries() -> List[Dict[str, Any]]: if not os.path.isfile(PATTERNS_PATH): raise CatalogError("no catalog at %s" % PATTERNS_PATH) with open(PATTERNS_PATH, "r", encoding="utf-8") as handle: data = json.load(handle) entries = data.get("entries") or [] if not entries: raise CatalogError("catalog carries no entry") return entries def load_policies() -> Dict[str, str]: if not os.path.isfile(POLICY_PATH): return {} with open(POLICY_PATH, "r", encoding="utf-8") as handle: return json.load(handle).get("pairs", {}) def by_id(entries: List[Dict[str, Any]], entry_id: str) -> Optional[Dict[str, Any]]: for entry in entries: if entry.get("id") == entry_id: return entry return None def integrity(entries: List[Dict[str, Any]]) -> List[str]: """Return a line per defect. Empty means the catalog is admissible.""" defects: List[str] = [] seen: Dict[str, int] = {} for entry in entries: entry_id = str(entry.get("id") or "") if not entry_id: defects.append("entry without an id") continue if entry_id in seen: defects.append("duplicate id %s" % entry_id) seen[entry_id] = 1 for key in REQUIRED_ENTRY_KEYS: if key not in entry: defects.append("%s misses key %s" % (entry_id, key)) if not entry.get("refusals"): defects.append("%s carries no refusal" % entry_id) if len(entry.get("refusals") or []) < 2: defects.append("%s carries fewer than two refusals" % entry_id) if not entry.get("lock"): defects.append("%s carries no lock test" % entry_id) if not entry.get("frames"): defects.append("%s carries no frame" % entry_id) return defects def policy_for(policies: Dict[str, str], a: str, b: str) -> Optional[str]: key = "|".join(sorted((a, b))) value = policies.get(key) if value in POLICIES: return value return None def conflict_lines(claims: Dict[str, List[str]], policies: Dict[str, str]) -> Tuple[List[str], List[Tuple[str, str, str]]]: """Claims maps a symbol to the entry ids that match it. Returns (defects, resolved_pairs). A pair with no declared policy is a defect. """ defects: List[str] = [] resolved: List[Tuple[str, str, str]] = [] for symbol, ids in sorted(claims.items()): if len(ids) > MAX_CANDIDATES: defects.append("%s claimed by %d entries, over the limit of %d" % (symbol, len(ids), MAX_CANDIDATES)) for i, first in enumerate(sorted(ids)): for second in sorted(ids)[i + 1:]: policy = policy_for(policies, first, second) if policy is None: defects.append("no policy for pair %s|%s on %s" % (first, second, symbol)) else: resolved.append((first, second, policy)) return defects, resolved