66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
"""Lecture des specs Vega-Lite produites, et leur enregistrement pour une vérification
|
|
par Vega-Lite (variable ``DUMP_SPECS`` : chemin du fichier JSON écrit en fin de session).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
|
|
from shortstats.web import spec_layers
|
|
|
|
DUMP = os.environ.get("DUMP_SPECS")
|
|
_dumped: list[dict] = []
|
|
|
|
|
|
def keep(out: dict, name: str) -> dict:
|
|
"""Garde la spec d'un résultat (``DUMP_SPECS``) et rend le résultat."""
|
|
if DUMP and out.get("spec"):
|
|
_dumped.append({"name": name, "spec": out["spec"]})
|
|
return out
|
|
|
|
|
|
def write_dump() -> None:
|
|
"""Écrit les specs gardées (fin de session, conftest.py)."""
|
|
if DUMP and _dumped:
|
|
with open(DUMP, "w") as fh:
|
|
json.dump(_dumped, fh)
|
|
|
|
|
|
def body_of(spec: dict) -> dict:
|
|
"""Corps d'une spec (celui de chaque petit diagramme quand il y en a un par chaîne)."""
|
|
return spec_layers.body_of(spec)
|
|
|
|
|
|
def layer_of(body, k):
|
|
"""Couche de la catégorie k (les barres sont dessinées d'abord : l'ordre peut changer)."""
|
|
return next(layer for layer in body["layer"] if layer["transform"] == [spec_layers.category_filter(k)])
|
|
|
|
|
|
def _hl_layers(node: dict) -> list[dict]:
|
|
"""Couches des vidéos mises en avant, à toute profondeur (plusieurs ordonnées, petits diagrammes)."""
|
|
out = []
|
|
for layer in node.get("layer", []):
|
|
if spec_layers.is_highlight(layer):
|
|
out.append(layer)
|
|
out += _hl_layers(layer)
|
|
return out
|
|
|
|
|
|
def _check_path(layers: list[dict]) -> None:
|
|
lines = [la for la in layers if la["mark"]["style"] == "hl-line"]
|
|
points = [la for la in layers if la["mark"]["style"] == "hl-point"]
|
|
assert lines and len(lines) == len(points)
|
|
for line in lines:
|
|
# la courbe seule : pas de points superposés, qui reprendraient son trait
|
|
assert "point" not in line["mark"] and "shape" not in line["encoding"]
|
|
assert "strokeDash" in line["encoding"]
|
|
for point in points:
|
|
# la forme seule : son contour est plein, quel que soit le trait de la courbe
|
|
assert point["mark"]["type"] == "point" and "strokeDash" not in point["encoding"]
|
|
assert "shape" in point["encoding"]
|
|
# même champ, même titre : forme et trait restent une seule légende (deux icônes)
|
|
for line, point in zip(lines, points):
|
|
assert line["encoding"]["strokeDash"]["field"] == point["encoding"]["shape"]["field"] == "hn"
|
|
assert line["encoding"]["strokeDash"]["legend"] == point["encoding"]["shape"]["legend"]
|
|
assert line["transform"] == point["transform"]
|