SPB Git

Repository Public MIT Research preview

exporter_donnees_figures.py 6.3 KB 141 lines python

1"""Écrit les données des figures S11-F01 à S11-F05 à partir du simulateur et des scénarios mesurés.23Aucun nombre d'une figure n'est saisi à la main, et l'arbre de la figure S11-F02 est dessiné à4partir de l'état du système de fichiers simulé.5Usage :  python exporter_donnees_figures.py [--check]6"""7from __future__ import annotations89import sys10from pathlib import Path1112sys.path.insert(0, str(Path(__file__).resolve().parent.parent))1314from atlas_s11 import garde, scenarios  # noqa: E40215from atlas_s11.fichiers import est_dans, parties  # noqa: E40216from atlas_s11.monde import BAC, monde_initial  # noqa: E4021718DOSSIER = Path(__file__).resolve().parents[3] / "figures" / "src"19ENTETE = "% GÉNÉRÉ par demos/src/atlas_s11/exporter_donnees_figures.py — ne pas modifier à la main.%\n"2021# Mise en page de l'arbre (figure S11-F02) : une abscisse par profondeur, une ordonnée par feuille.22ABSCISSES = (3, 15, 31, 55, 83, 113)23HAUT, PAS, SAUT_HORS_BAC = 44.5, 6.5, 8.5242526def macro(nom: str, valeur) -> str:27    return f"\\def\\AtlasFig{nom}{{{valeur}}}%\n"282930def f01() -> str:31    exemple = next(r for r in scenarios.flux_de_travail() if r.ligne.startswith("grep") and ">" in r.ligne)32    commande = exemple.commandes[0]33    return (ENTETE + macro("ArchExemple", exemple.ligne)34            + macro("ArchCommande", commande.mots[0])35            + macro("ArchArguments", len(commande.mots) - 1)36            + macro("ArchCible", commande.cible)37            + macro("ArchCode", commande.code))383940def disposition() -> tuple[dict[str, tuple[float, float]], list[tuple[str, str]]]:41    """Coordonnées (mm) de chaque nœud de l'arbre et liste des liens parent-enfant."""42    fichiers = monde_initial()43    positions: dict[str, tuple[float, float]] = {}44    liens: list[tuple[str, str]] = []45    prochaine = [HAUT]4647    def placer(chemin: str) -> float:48        profondeur = len(parties(chemin))49        if fichiers.est_dossier(chemin) and fichiers.lister(chemin):50            ys = []51            for nom in fichiers.lister(chemin):52                enfant = chemin.rstrip("/") + "/" + nom53                if est_dans(BAC, chemin) != est_dans(BAC, enfant) or (not est_dans(BAC, enfant) and ys54                                                                       and profondeur >= len(parties(BAC)) - 1):55                    prochaine[0] -= SAUT_HORS_BAC - PAS56                ys.append(placer(enfant))57                liens.append((chemin, enfant))58            y = sum(ys) / len(ys)59        else:60            y = prochaine[0]61            prochaine[0] -= PAS62        positions[chemin] = (ABSCISSES[profondeur], round(y, 2))63        return y6465    placer("/")66    return positions, liens676869def identifiant(chemin: str) -> str:70    return "n" + "".join(c if c.isalnum() else "-" for c in chemin)717273def f02() -> str:74    fichiers = monde_initial()75    positions, liens = disposition()76    texte = ENTETE + "\\def\\AtlasFigArbreNoeuds{%\n"77    for chemin, (x, y) in sorted(positions.items()):78        nom = "/" if chemin == "/" else chemin.split("/")[-1]79        genre = "Dossier" if fichiers.est_dossier(chemin) else "Fichier"80        if chemin == BAC:81            genre = "Bac"82        texte += f"\\AtlasFigArbre{genre}{{{identifiant(chemin)}}}{{{nom}{'/' if genre != 'Fichier' and nom != '/' else ''}}}{{{x}}}{{{y:g}}}%\n"83    texte += "}%\n\\def\\AtlasFigArbreLiens{%\n"84    for parent, enfant in sorted(liens):85        texte += f"\\AtlasFigArbreLien{{{identifiant(parent)}}}{{{identifiant(enfant)}}}%\n"86    texte += "}%\n"87    dans = [c for c in positions if est_dans(BAC, c)]88    ys = [positions[c][1] for c in dans]89    return (texte + macro("ArbreBacBas", f"{min(ys):g}") + macro("ArbreBacHaut", f"{max(ys):g}")90            + macro("ArbreBacNoeud", identifiant(BAC))91            + macro("ArbreFichiersBac", sum(fichiers.est_fichier(c) for c in dans))92            + macro("ArbreDossiersBac", sum(fichiers.est_dossier(c) for c in dans))93            + macro("ArbreFichiersHors", sum(fichiers.est_fichier(c) for c in positions if c not in dans))94            + macro("ArbreAbsolu", BAC + "/notes/mardi.txt") + macro("ArbreRelatif", "notes/mardi.txt"))959697def f03() -> str:98    t = scenarios.tube()99    cat, grep = t["normal"].commandes100    cat2, grep2 = t["masque"].commandes101    return (ENTETE102            + macro("TubeFichiers", len(cat.mots) - 1) + macro("TubeLignesCat", cat.lignes_sortie)103            + macro("TubeLignesGrep", grep.lignes_sortie) + macro("TubeLignesEcrites", t["lignes_ecrites"])104            + macro("TubeCodeCat", cat.code) + macro("TubeCodeGrep", grep.code) + macro("TubeCode", t["normal"].code)105            + macro("TubeCible", grep.cible)106            + macro("MasqueLignesCat", cat2.lignes_sortie) + macro("MasqueSortie", t["masque"].sortie.strip())107            + macro("MasqueCodeCat", cat2.code) + macro("MasqueCodeGrep", grep2.code)108            + macro("MasqueCode", t["masque"].code))109110111def f04() -> str:112    s = scenarios.script_inconnu()113    texte = ENTETE114    for nom, m in (("Hote", s["hote"]), ("Bac", s["bac"])):115        texte += (macro(f"Iso{nom}Reussies", m["reussies"]) + macro(f"Iso{nom}Lus", m["lus_hors_bac"])116                  + macro(f"Iso{nom}Modifies", m["modifies_hors_bac"])117                  + macro(f"Iso{nom}Avant", m["budget_lignes_avant"]) + macro(f"Iso{nom}Apres", m["budget_lignes_apres"]))118    return texte + macro("IsoLignes", s["hote"]["lignes_script"])119120121def f05() -> str:122    p = scenarios.propositions()123    regles = [x.decision.regle for x in p["passages"]]124    return (ENTETE + macro("GardeTotal", p["total"])125            + macro("GardeHorsListe", regles.count("liste")) + macro("GardeHorsBac", regles.count("bac"))126            + macro("GardeEcritures", regles.count("ecriture")) + macro("GardeLectures", regles.count("lecture"))127            + macro("GardeRefusees", p["compte"][garde.REFUSER]) + macro("GardeExecutees", p["executees"]))128129130FIGURES = {"S11-F01-donnees.tex": f01, "S11-F02-donnees.tex": f02, "S11-F03-donnees.tex": f03,131           "S11-F04-donnees.tex": f04, "S11-F05-donnees.tex": f05}132133134if __name__ == "__main__":135    if "--check" in sys.argv:136        sys.exit(0 if all((DOSSIER / nom).exists() and (DOSSIER / nom).read_text(encoding="utf-8") == f()137                          for nom, f in FIGURES.items()) else 1)138    for nom, f in FIGURES.items():139        (DOSSIER / nom).write_text(f(), encoding="utf-8")140        print(f"écrit : {DOSSIER / nom}")141
Files main
Menu

Theme