feat: completude de l'editeur Excel - mise en forme, sortie, concurrence, cache, outils IA #156
CI / lint (push) Successful in 2m36s
CI / security (push) Successful in 1m49s
CI / test (push) Successful in 4m33s
CI / build (push) Successful in 2m39s
CI / e2e (push) Successful in 15m45s

L'editeur lisait les styles mais ne les ecrivait pas, exportait la feuille
entiere et laissait le dernier-ecrivain gagner entre processus.

- A8 : bouton Mise en forme (gras/italique/souligne, alignements, couleurs via
  selecteur natif, formats de nombre, fusion, volets figes, largeur/hauteur) et
  nouvelle route PUT .../xlsx/style (verrou, backup, swap atomique, garde de
  perte, If-Match) ; A9 : decision "pas de moteur de formule" annoncee dans
  l'UI ; A10 : undo/redo unifie, piles par fichier conservees au re-rendu
- A11 : export de la selection + Markdown/HTML/impression et recherche sur
  toutes les feuilles ; A12 : concurrence optimiste (If-Match -> 409 reparable,
  retry qui relit) ; A13 : cache des metadonnees par (chemin, mtime, taille)
- A14 : outils IA .xlsm/.csv + search_workbook, analyze_range,
  edit_xlsx_structure
- tests : test_xlsx_styles.py (17), test_spreadsheet_tools.py (34),
  TestOptimisticConcurrency/TestMetaCache, JSDOM xlsx-viewer 107/107
This commit is contained in:
2026-09-30 07:04:47 -04:00
parent 1bacfd69d9
commit c5c225a68e
32 changed files with 4615 additions and 302 deletions
+390 -6
View File
@@ -278,6 +278,67 @@ def _xlsx_write_lock(key: str) -> Iterator[None]:
lock.release()
def _invalidate_meta(file_path: Path) -> None:
"""Drop the cached workbook metadata after a write (#156-A13).
Imported lazily so the module stays importable when the spreadsheet reader
is not needed (it pulls openpyxl in).
"""
try:
from backend.xlsx_reader import invalidate_workbook_meta
invalidate_workbook_meta(file_path)
except Exception: # pragma: no cover - cache invalidation is best-effort
logger.debug("workbook meta invalidation skipped", exc_info=True)
def file_revision(file_path: Path) -> str:
"""Opaque revision token of a file — ``mtime_ns:size`` in hex (#156-A12).
Cheap by design (one ``stat``) and enough for optimistic concurrency: any
writer that replaces the file changes at least one of the two fields. The
viewer reads it with the file and sends it back as the ``if_match`` of a
write, so an external editor (Excel, the watcher, another worker) can no
longer be silently overwritten.
Returns:
The token, or ``""`` when the file cannot be stat'ed.
"""
try:
st = file_path.stat()
except OSError:
return ""
return f"{st.st_mtime_ns:x}-{st.st_size:x}"
def _check_revision(file_path: Path, expected: str | None) -> None:
"""Refuse a write on a file that changed since it was read (#156-A12).
``expected`` comes from the read payload (``if_match``): when it is absent
the write keeps its pre-A12 behaviour (last writer wins), so curl, the AI
tools and the batch uploader are unaffected.
Raises:
ServiceError: ``conflict`` (409, ``reason=stale_revision``) when the
file on disk is not the revision the caller read.
"""
if not expected:
return
current = file_revision(file_path)
if current and current != expected:
raise ServiceError(
"The file changed on disk since it was read; reload before saving",
code="conflict",
status=409,
details={
"reason": "stale_revision",
"path": str(file_path),
"expected_revision": expected,
"current_revision": current,
},
)
def _coerce_xlsx_value(value: Any) -> Any:
"""Turn the string sent by the cell editor back into a scalar (#153 A10).
@@ -354,6 +415,7 @@ def edit_xlsx_cells(
backup: bool = True,
allow_formula: bool = False,
force: bool = False,
expected_revision: str | None = None,
) -> dict[str, Any]:
"""Apply a batch of cell edits to an ``.xlsx`` workbook.
@@ -416,6 +478,9 @@ def edit_xlsx_cells(
)
with _xlsx_write_lock(str(file_path)):
# #156-A12 — inside the lock: no writer can slip in between the check
# and the load.
_check_revision(file_path, expected_revision)
from openpyxl import load_workbook
# #153 A16 — .xlsm round-trips with keep_vba=True so the macro
@@ -451,6 +516,9 @@ def edit_xlsx_cells(
except Exception:
tmp_path.unlink(missing_ok=True)
raise
# #156-A13 — the metadata cache is keyed on (mtime, size); drop it too
# so a rewrite that lands on the same tick can never serve stale maps.
_invalidate_meta(file_path)
logger.info(f"XLSX cells saved: {vault_name}/{rel_path} [{sheet}] +{len(cells)}")
return {
@@ -458,6 +526,7 @@ def edit_xlsx_cells(
"vault": vault_name,
"path": rel_path,
"size": len(cells),
"revision": file_revision(file_path),
}
@@ -468,6 +537,7 @@ def mutate_xlsx_structure(
*,
backup: bool = True,
force: bool = False,
expected_revision: str | None = None,
) -> dict[str, Any]:
"""Apply structural changes to an ``.xlsx`` workbook (#153 A14).
@@ -521,6 +591,8 @@ def mutate_xlsx_structure(
)
with _xlsx_write_lock(str(file_path)):
# #156-A12 — stale-write guard (see edit_xlsx_cells).
_check_revision(file_path, expected_revision)
from openpyxl import load_workbook
from openpyxl.worksheet.copier import WorksheetCopy
@@ -636,6 +708,7 @@ def mutate_xlsx_structure(
wb.close()
raise
wb.close()
_invalidate_meta(file_path)
logger.info(
f"XLSX structure: {vault_name}/{rel_path} {applied}"
@@ -645,6 +718,306 @@ def mutate_xlsx_structure(
"vault": vault_name,
"path": rel_path,
"applied": applied,
"revision": file_revision(file_path),
}
# #156-A8 — write-side formatting. Every value is data-driven (the colours
# come from the caller, never from a hardcoded palette) and the whole batch
# rides the same lock, backup and atomic swap as the cell edits.
_STYLE_KEYS = {
"bold",
"italic",
"underline",
"font_color",
"fill_color",
"align",
"number_format",
}
_STYLE_ALIGNS = {"left", "center", "right"}
MAX_STYLE_CELLS = 10_000
_HEX_COLOR_RE = re.compile(r"^#?[0-9a-fA-F]{6}$")
def _style_color(value: Any) -> str | None:
"""Normalise ``#rrggbb`` to openpyxl's ARGB, or ``None`` to clear."""
if value is None or value == "":
return None
if not isinstance(value, str) or not _HEX_COLOR_RE.match(value.strip()):
raise ServiceError(
f"Couleur invalide: {value!r}", code="invalid", status=400
)
return "FF" + value.strip().lstrip("#").upper()
def _apply_cell_style(cell: Any, style: dict[str, Any]) -> None:
"""Apply the data-driven style fragment of one ``cell`` operation.
Only the keys listed in :data:`_STYLE_KEYS` are accepted — an unknown one
is a client bug, not something to ignore silently. Font attributes are
copied before mutation so the shared style of the other cells is left
untouched.
"""
import copy as copy_mod
from openpyxl.styles import Alignment, Color, PatternFill
unknown = set(style) - _STYLE_KEYS
if unknown:
raise ServiceError(
f"Style inconnu: {sorted(unknown)}", code="invalid", status=400
)
if {"bold", "italic", "underline", "font_color"} & set(style):
font = copy_mod.copy(cell.font)
if "bold" in style:
font.bold = bool(style["bold"])
if "italic" in style:
font.italic = bool(style["italic"])
if "underline" in style:
font.underline = "single" if style["underline"] else None
if "font_color" in style:
rgb = _style_color(style["font_color"])
font.color = Color(rgb=rgb) if rgb else None
cell.font = font
if "fill_color" in style:
rgb = _style_color(style["fill_color"])
cell.fill = (
PatternFill(start_color=rgb, end_color=rgb, fill_type="solid")
if rgb
else PatternFill(fill_type=None)
)
if "align" in style:
align = style["align"]
if align not in _STYLE_ALIGNS:
raise ServiceError(
f"Alignement invalide: {align!r}", code="invalid", status=400
)
current = cell.alignment
cell.alignment = Alignment(
horizontal=align,
vertical=getattr(current, "vertical", None),
wrap_text=getattr(current, "wrap_text", None),
)
if "number_format" in style:
fmt = style["number_format"] or "General"
if not isinstance(fmt, str) or len(fmt) > 120:
raise ServiceError(
"Format de nombre invalide", code="invalid", status=400
)
cell.number_format = fmt
def mutate_xlsx_style(
vault_name: str,
path: str,
ops: list[dict[str, Any]],
*,
backup: bool = True,
force: bool = False,
expected_revision: str | None = None,
) -> dict[str, Any]:
"""Write formatting on an ``.xlsx``/``.xlsm`` workbook (#156-A8).
``ops`` is an ordered list (1 to 50) applied in one locked, atomic rewrite:
* ``{"op": "cell", "sheet": "X", "range": "A1:B2", "style": {…}}`` with
``bold``, ``italic``, ``underline``, ``font_color`` / ``fill_color``
(``#rrggbb``, ``""`` clears), ``align`` (left/center/right) and
``number_format`` ;
* ``{"op": "merge"|"unmerge", "sheet": "X", "range": "A1:B2"}`` ;
* ``{"op": "col_width", "sheet": "X", "col": "A", "width": 24}`` ;
* ``{"op": "row_height", "sheet": "X", "row": 3, "height": 30}`` ;
* ``{"op": "freeze", "sheet": "X", "cell": "B2"}`` (``""`` releases).
Same guards as the cell edits: per-file lock, ``.bak`` backup,
``.tmp`` + ``os.replace`` atomic swap, lossy-write 409 and the optional
``expected_revision`` concurrency check (#156-A12).
"""
from openpyxl.utils import get_column_letter
from openpyxl.utils.cell import range_boundaries
root = get_vault_root(vault_name)
_ensure_writable(root)
file_path = resolve_safe_path(root, path)
if not file_path.exists() or not file_path.is_file():
raise ServiceError(
f"File not found: {path}",
code="not_found",
status=404,
details={"vault": vault_name, "path": path},
)
if file_path.suffix.lower() not in (".xlsx", ".xlsm"):
raise ServiceError(
f"Not an .xlsx/.xlsm file: {path}", code="invalid", status=400
)
if not ops or len(ops) > 50:
raise ServiceError(
"Invalid ops (1 to 50 per request)", code="invalid", status=400
)
if not force and file_path.suffix.lower() != ".xlsm":
from backend.xlsx_reader import inspect_workbook
lossy = inspect_workbook(file_path)
if lossy:
raise ServiceError(
"Restyling this workbook would drop features ObsiGate cannot "
"preserve; retry with force=true after confirmation",
code="xlsx_lossy_content",
status=409,
details={"path": path, "features": lossy},
)
def _range(ref: str) -> tuple[int, int, int, int]:
try:
min_col, min_row, max_col, max_row = range_boundaries(str(ref).upper())
except Exception as exc:
raise ServiceError(
f"Plage invalide: {ref!r}", code="invalid", status=400
) from exc
if (max_row - min_row + 1) * (max_col - min_col + 1) > MAX_STYLE_CELLS:
raise ServiceError(
f"Plage trop grande (max {MAX_STYLE_CELLS} cellules)",
code="invalid",
status=400,
)
return min_col, min_row, max_col, max_row
def _sheet(wb: Any, action: dict[str, Any]) -> Any:
name = str(action.get("sheet", ""))
if name not in wb.sheetnames:
raise ServiceError(
f"Feuille introuvable: {name}",
code="invalid",
status=400,
details={"sheets": wb.sheetnames},
)
return wb[name]
applied: list[str] = []
with _xlsx_write_lock(str(file_path)):
_check_revision(file_path, expected_revision)
from openpyxl import load_workbook
try:
wb = load_workbook(
file_path, keep_vba=file_path.suffix.lower() == ".xlsm"
)
except Exception as exc:
raise ServiceError(
f"Cannot open workbook: {exc}", code="invalid", status=400
) from exc
rel_path = _rel(root, file_path)
try:
for i, action in enumerate(ops):
if not isinstance(action, dict):
raise ServiceError(
f"Action {i + 1} invalide", code="invalid", status=400
)
op = action.get("op")
try:
if op == "cell":
ws = _sheet(wb, action)
ref = str(action.get("range") or action.get("cell") or "")
min_col, min_row, max_col, max_row = _range(ref)
style = action.get("style") or {}
if not isinstance(style, dict) or not style:
raise ServiceError(
"Style vide", code="invalid", status=400
)
for row in ws.iter_rows(
min_row=min_row,
max_row=max_row,
min_col=min_col,
max_col=max_col,
):
for cell in row:
_apply_cell_style(cell, style)
applied.append(
f"cell:{ws.title}!{ref}:{','.join(sorted(style))}"
)
elif op in ("merge", "unmerge"):
ws = _sheet(wb, action)
ref = str(action.get("range", ""))
_range(ref)
if op == "merge":
ws.merge_cells(ref)
else:
ws.unmerge_cells(ref)
applied.append(f"{op}:{ws.title}!{ref}")
elif op == "col_width":
ws = _sheet(wb, action)
col = action.get("col")
width = action.get("width")
if isinstance(col, int):
col = get_column_letter(col)
if not isinstance(col, str) or not col.strip():
raise ServiceError(
"Colonne invalide", code="invalid", status=400
)
if not isinstance(width, (int, float)) or not 0 <= float(width) <= 255:
raise ServiceError(
"Largeur invalide (0 à 255)", code="invalid", status=400
)
ws.column_dimensions[col.strip().upper()].width = float(width)
applied.append(f"col_width:{ws.title}!{col}={width}")
elif op == "row_height":
ws = _sheet(wb, action)
row = action.get("row")
height = action.get("height")
if not isinstance(row, int) or row < 1:
raise ServiceError("Ligne invalide", code="invalid", status=400)
if not isinstance(height, (int, float)) or not 0 <= float(height) <= 409:
raise ServiceError(
"Hauteur invalide (0 à 409)", code="invalid", status=400
)
ws.row_dimensions[row].height = float(height)
applied.append(f"row_height:{ws.title}!{row}={height}")
elif op == "freeze":
ws = _sheet(wb, action)
cell_ref = str(action.get("cell", ""))
if cell_ref:
_range(cell_ref)
ws.freeze_panes = cell_ref.upper() or None
applied.append(f"freeze:{ws.title}!{cell_ref or '-'}")
else:
raise ServiceError(
f"Action inconnue: {op!r}", code="invalid", status=400
)
except ServiceError:
raise
except Exception as exc:
raise ServiceError(
f"Action {i + 1} ({op}) a échoué: {exc}",
code="invalid",
status=400,
) from exc
except ServiceError:
wb.close()
raise
if backup:
create_backup(file_path, vault_name, rel_path)
tmp_path = file_path.with_name(f"{file_path.name}.{os.getpid()}.tmp")
try:
wb.save(tmp_path)
os.replace(tmp_path, file_path)
except Exception:
tmp_path.unlink(missing_ok=True)
wb.close()
raise
wb.close()
_invalidate_meta(file_path)
logger.info(f"XLSX style: {vault_name}/{rel_path} {applied}")
return {
"success": True,
"vault": vault_name,
"path": rel_path,
"applied": applied,
"revision": file_revision(file_path),
}
@@ -654,14 +1027,18 @@ def save_csv_cells(
cells: dict[str, Any],
*,
backup: bool = True,
expected_revision: str | None = None,
) -> dict[str, Any]:
"""Apply A1-addressed cell edits to a ``.csv`` file (#153 A16).
The file is re-parsed, patched and re-serialized with :mod:`csv` so
quoting follows RFC 4180. References beyond the current extent grow the
grid (missing rows/cells are filled with empty strings). Values are
stored as text: a CSV has no formula engine, so any string — including
ones starting with ``=`` — is written verbatim (the render escapes it).
quoting follows RFC 4180. BUG-098 — the delimiter is **sniffed** and the
same one is reused on write-back: a `;`-separated French CSV used to be
parsed as a single column and rewritten with `,`. References beyond the
current extent grow the grid (missing rows/cells are filled with empty
strings). Values are stored as text: a CSV has no formula engine, so any
string — including ones starting with ``=`` — is written verbatim (the
render escapes it).
Raises:
ServiceError: ``not_found`` (404), ``read_only`` (403), ``conflict``
@@ -670,6 +1047,8 @@ def save_csv_cells(
import csv as csv_mod
import io as io_mod
from backend.xlsx_reader import sniff_csv_delimiter
root = get_vault_root(vault_name)
_ensure_writable(root)
file_path = resolve_safe_path(root, path)
@@ -691,9 +1070,13 @@ def save_csv_cells(
f"Invalid cell reference: {ref!r}", code="invalid", status=400
)
# #156-A12 — same stale-write guard as the workbook path.
_check_revision(file_path, expected_revision)
raw = file_path.read_text(encoding="utf-8", errors="replace")
# BUG-098 — parse AND rewrite with the file's own delimiter.
delimiter = sniff_csv_delimiter(raw)
try:
rows = list(csv_mod.reader(io_mod.StringIO(raw)))
rows = list(csv_mod.reader(io_mod.StringIO(raw), delimiter=delimiter))
except csv_mod.Error:
rows = [[line] for line in raw.splitlines()]
@@ -721,7 +1104,7 @@ def save_csv_cells(
create_backup(file_path, vault_name, rel_path)
buf = io_mod.StringIO()
csv_mod.writer(buf, lineterminator="\n").writerows(rows)
csv_mod.writer(buf, delimiter=delimiter, lineterminator="\n").writerows(rows)
tmp_path = file_path.with_name(f"{file_path.name}.{os.getpid()}.tmp")
try:
tmp_path.write_text(buf.getvalue(), encoding="utf-8")
@@ -736,6 +1119,7 @@ def save_csv_cells(
"vault": vault_name,
"path": rel_path,
"size": len(cells),
"revision": file_revision(file_path),
}