108 lines
5.1 KiB
Python
108 lines
5.1 KiB
Python
"""La page de revue d'un cas appris : tout ce qu'il faut à l'expert pour trancher, sur une seule page (D-075).
|
|
|
|
En tête, la présentation de l'agent revue (out/revue-cas/C-xxx.md, si elle existe), puis la page des détecteurs sur la
|
|
plage (detecteurs.py, sur data/<STATION>/, sinon sur le dossier du cas) ; dessous, la fiche, l'étiquetage, le rendu du
|
|
builder et la critique du lot d'origine, puis toutes les figures : celles du cas et celles du lot de la séance.
|
|
Les vrais noms y figurent (registre) : la page reste dans out/, hors de git.
|
|
Usage : python3 .claude/skills/revue-cas/page.py C-xxx --du AAAA-MM-JJ --au AAAA-MM-JJ (--au exclu ; page ouverte)
|
|
"""
|
|
import argparse
|
|
import base64
|
|
import csv
|
|
import glob
|
|
import html
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
ICI = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, os.path.join(ICI, "..", "boucle"))
|
|
import detecteurs # noqa: E402
|
|
from comparaison import ROOT, open_html # noqa: E402
|
|
|
|
CAS = os.path.join(ROOT, "agents", "diagnostic", "cas")
|
|
|
|
|
|
def lire(chemin):
|
|
return open(chemin, encoding="utf-8").read() if os.path.exists(chemin) else None
|
|
|
|
|
|
def station_du_cas(cas):
|
|
with open(os.path.join(ICI, "..", "anonymiser-cas", "registre.csv"), encoding="utf-8") as f:
|
|
for ligne in csv.DictReader(f):
|
|
if ligne["code"] == cas:
|
|
return ligne["station"]
|
|
raise SystemExit(f"{cas} n'est pas dans le registre.")
|
|
|
|
|
|
def lot_d_origine(cas, fiche, station):
|
|
m = re.search(r"séance `?(?:vues-deplacements/boucle/)?(\d{4}-\d\d-\d\dT\d{4}Z)", fiche)
|
|
if not m:
|
|
return None
|
|
for builder in sorted(glob.glob(os.path.join(ROOT, "vues-deplacements", "boucle", f"{m[1]}_{station}", "lot-*", "builder.md"))):
|
|
if re.search(rf"\*\*Cas créés\*\*.*\b{cas}\b", lire(builder)):
|
|
return os.path.dirname(builder)
|
|
return None
|
|
|
|
|
|
def bloc(titre, texte, ouvert=True):
|
|
# les figures citées dans les .md sont dans la galerie, en bas : on retire les liens d'image, cassés depuis out/
|
|
texte = re.sub(r"!\[[^\]]*\]\([^)]*\)", "", texte)
|
|
source = json.dumps(texte).replace("</", "<\\/")
|
|
return (f"<details{' open' if ouvert else ''}><summary><h2 style='display:inline'>{html.escape(titre)}</h2></summary>"
|
|
f"<div class='md' data-md='{html.escape(source, quote=True)}'></div></details>")
|
|
|
|
|
|
def figure(chemin):
|
|
donnees = base64.b64encode(open(chemin, "rb").read()).decode()
|
|
nom = os.path.relpath(chemin, ROOT)
|
|
return f"<figure><img src='data:image/png;base64,{donnees}' alt='{html.escape(nom)}'><figcaption>{html.escape(nom)}</figcaption></figure>"
|
|
|
|
|
|
def page(cas, du, au):
|
|
dossier_cas = os.path.join(CAS, cas)
|
|
fiche = lire(os.path.join(dossier_cas, "cas.md"))
|
|
if fiche is None:
|
|
raise SystemExit(f"Pas de fiche {dossier_cas}/cas.md.")
|
|
station = station_du_cas(cas)
|
|
lot = lot_d_origine(cas, fiche, station)
|
|
donnees = os.path.join(ROOT, "data", station)
|
|
if not os.path.isdir(donnees):
|
|
donnees = dossier_cas
|
|
|
|
presentation = lire(os.path.join(ROOT, "out", "revue-cas", f"{cas}.md"))
|
|
entete = (f"<h1>Revue de {cas} : {html.escape(station)}</h1>"
|
|
f"<p>Lot d'origine : {html.escape(os.path.relpath(lot, ROOT)) if lot else 'introuvable'} · "
|
|
f"détecteurs sur {html.escape(os.path.relpath(donnees, ROOT))}</p>")
|
|
entete += bloc("Présentation de l'agent revue", presentation) if presentation else "<p>Pas encore de présentation de l'agent revue.</p>"
|
|
|
|
annexe = bloc("Fiche du cas (cas.md, noms codés)", fiche)
|
|
for titre, chemin in [("Étiquetage du cas", os.path.join(dossier_cas, "etiquetage.md")),
|
|
("Rendu du builder", lot and os.path.join(lot, "builder.md")),
|
|
("Critique de l'analyste, lot d'origine", lot and os.path.join(lot, "critique.md"))]:
|
|
texte = chemin and lire(chemin)
|
|
if texte:
|
|
annexe += bloc(titre, texte, ouvert=False)
|
|
pngs = sorted(glob.glob(os.path.join(dossier_cas, "*.png"))) + (sorted(glob.glob(os.path.join(lot, "*.png"))) if lot else [])
|
|
annexe += f"<h2>Figures ({len(pngs)})</h2>" + "".join(figure(p) for p in pngs)
|
|
annexe += """<style>details{margin:12px 0} summary{cursor:pointer} .md{max-width:70em} .md table{margin:8px 0}
|
|
figure{margin:16px 0} figure img{max-width:100%;border:1px solid #e6e6e2} figcaption{color:#5f5e5a;font-size:13px}</style>
|
|
<script src="https://cdn.jsdelivr.net/npm/marked@12.0.2/marked.min.js"></script>
|
|
<script>document.querySelectorAll('.md').forEach(d => d.innerHTML = marked.parse(JSON.parse(d.dataset.md)));</script>"""
|
|
|
|
chemin = os.path.join(ROOT, "out", "revue-cas", f"{cas}.html")
|
|
os.makedirs(os.path.dirname(chemin), exist_ok=True)
|
|
return detecteurs.page(donnees, du, au, entete=entete, annexe=annexe, chemin=chemin)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
p.add_argument("cas")
|
|
p.add_argument("--du", required=True)
|
|
p.add_argument("--au", required=True)
|
|
o = p.parse_args()
|
|
chemin = page(o.cas, o.du, o.au)
|
|
print(chemin)
|
|
open_html(chemin)
|