"""The committed allowlist limits-allowlist.json: functions that are allowed to miss a limit, and by how much.""" import json from .model import KINDS, allowed FILE = "" # the entry for lines outside any function of a file (consts, attributes, imports): only "width" COMMENT = ("Functions that may miss a limit of scripts/check-limits.py because they were moved verbatim from legacy " "code. Every recorded number may only fall; an entry whose function is back within the limits is stale and " "must go (scripts/check-limits.py --update-baseline). New entries: --allowlist-add --reason ... --owner ...") def load(path): """{(path, name): entry}; a missing file is an empty allowlist.""" if not path.exists(): return {} data = json.loads(path.read_text(encoding="utf-8")) return {(entry["path"], entry["name"]): entry for entry in data["functions"]} def save(path, entries): rows = [entries[key] for key in sorted(entries)] with open(path, "w", encoding="utf-8", newline="\n") as handle: handle.write(json.dumps({"_comment": COMMENT, "functions": rows}, indent=1) + "\n") def new_entry(rel, name, measures, reason, owner): return {"path": rel, "name": name, **measures, "reason": reason, "owner": owner} def missing_fields(entry): """Names of the mandatory fields an entry leaves empty.""" return [name for name in ("reason", "owner") if not str(entry.get(name, "")).strip()] def recorded(entry, limits): """The numbers an entry allows: a kind it does not mention is allowed up to the limit only.""" return {kind: entry.get(kind, allowed(kind, limits)) for kind in KINDS}