"""What the client is told of a detectors' event: what happens (message) and what to do (action), the latter from the "For the client" columns of families.md (per family, refined per variety). For a caller (aimsight-app, the Data page). python3 labeling/scripts/client.py # a check Standard library. """ import os import re FAMILIES_MD = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "families.md") def advice(): """{key: text}: the rows "| `key` | … | text |" of the tables of families.md, the key a family or a variety.""" with open(FAMILIES_MD, encoding="utf-8") as f: return {m.group(1): m.group(2).strip() for line in f if (m := re.match(r"\| `([^`]+)` \|(?:.*\|)? ([^|]+) \|\s*$", line))} def action(family, variety, level=None, table=None): """What to do: the variety with its level, otherwise the variety, otherwise the family; "" without. table: advice(), read once by a caller with many events.""" t = advice() if table is None else table text = t.get(f"{variety} {level or ''}".strip()) or t.get(variety) or t.get(family, "") return text[:1].upper() + text[1:] + "." * bool(text and not text.endswith(".")) def message(text): """What happens: the first sentence of the event's text (detectors/scripts/categories.py), up to its ";"; the rest says what to do about it, except the sentence of the absorbed jumps, kept. ". " only: "1.5×" or "0.30 px" do not cut.""" same = re.search(r" (The jumps? of .* same cause\.)$", text) return re.split(r"\. |; ", text, maxsplit=1)[0].rstrip(".") + "." + (" " + same.group(1) if same else "") if __name__ == "__main__": t = advice() assert action("Visibility", "Poor visibility", "severe", t) == "Do not trust the curves over the range; it is not a movement.", t assert action("Visibility", "Poor visibility", "reduced", t).startswith("Use the data over the range with care") assert action("Displacement", "Slow drift", None, t) == "Keep watching it." assert action("Displacement", "Lasting shift", None, t) == "Check what moved: look at the images." assert action("Perturbation", "Sudden jump, back to normal", None, t) == "Nothing to do: it came back; ignore these points." assert action("Perturbation", "x", None, t) == "Nothing to do: the curves came back; ignore these points." assert action("Unknown", "x", None, t) == "" assert message("A moved; nothing to do. The jump of 2 targets in this period has the same cause.") \ == "A moved. The jump of 2 targets in this period has the same cause." print("ok")