cellprose 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cellprose/reader.py ADDED
@@ -0,0 +1,591 @@
1
+ """Read Cellprose v0.2 and v0.3 text back into a workbook (the inverse of writer.py).
2
+
3
+ wb = cellprose.load(text) # parse; raises ParseError with a line number
4
+ wb.to_xlsx("book.xlsx") # values, formulas, number formats, merges, names, sheet state
5
+
6
+ Formula results in the grid are display values and are ignored: formulas are rewritten from the
7
+ `formulas` blocks (fill ranges are expanded by shifting relative references, as Excel does), and a
8
+ spreadsheet app recomputes them. Typed-in values are read back exactly. Compact-profile files drop rows
9
+ and cannot be turned back into a workbook (`CompactProfileError`).
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import datetime as dt
14
+ import math
15
+ import re
16
+ from contextlib import contextmanager
17
+ from dataclasses import dataclass, field
18
+ from pathlib import Path
19
+
20
+ from openpyxl.formula.translate import Translator
21
+ from openpyxl.utils import column_index_from_string, range_boundaries
22
+ from openpyxl.utils import get_column_letter as L
23
+
24
+ LEGEND_PREFIX = "Note: the first field of each table row is its row number, not a cell;"
25
+ SUPPORTED = ("0.2", "0.3") # 0.2 grids have outer pipes on every row, 0.3 grids have none
26
+ NUMBER = re.compile(r"^[-+]?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?$")
27
+ DATETIME = re.compile(r"^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2}))?$")
28
+ TIME = re.compile(r"^(\d{2}):(\d{2}):(\d{2})$")
29
+ ERROR = re.compile(r"^#[A-Z/0!?]+$")
30
+ RANGE = re.compile(r"^[A-Z]{1,3}[0-9]+(:[A-Z]{1,3}[0-9]+)?$")
31
+ EXTERNAL = re.compile(r"\[\d+\][^!]*!") # a reference into another workbook: 'file'!A1 is stored as '[1]Sheet'!A1
32
+ MAX_RANGE_CELLS = 1_000_000 # largest range one block line may name
33
+ MAX_LINKS = 1000 # highest [n] external-link index written
34
+ MAX_CELLS = 5_000_000 # cells a whole file may define
35
+ ESCAPES = {"\\": "\\", "|": "|", "<": "<"}
36
+
37
+
38
+ class ParseError(ValueError):
39
+ def __init__(self, message: str, line: int | None = None):
40
+ self.line = line
41
+ super().__init__(f"line {line}: {message}" if line else message)
42
+
43
+
44
+ class CompactProfileError(ParseError):
45
+ """The file is a compact-profile excerpt; it omits data and cannot become a workbook."""
46
+
47
+
48
+ # ------------------------------------------------------------------ model
49
+ @contextmanager
50
+ def _exact_floats():
51
+ """openpyxl writes numbers with "%.16g", which cuts 17-digit floats (53.779999999999994 -> 53.77999999999999).
52
+ Use repr, the shortest string that reads back as the same float, while a workbook is saved."""
53
+ import openpyxl.cell._writer as cell_writer
54
+ original = cell_writer.safe_string
55
+
56
+ def safe_string(value):
57
+ if isinstance(value, float) and math.isfinite(value):
58
+ return repr(value)
59
+ return original(value)
60
+
61
+ cell_writer.safe_string = safe_string
62
+ try:
63
+ yield
64
+ finally:
65
+ cell_writer.safe_string = original
66
+
67
+
68
+ def _patch_external(path: Path, cells: dict[int, dict[str, tuple[str, object, bool]]]) -> None:
69
+ """Make formulas that refer to another workbook open cleanly, and keep what they showed.
70
+
71
+ The link behind `[1]` is not part of the format, and a spreadsheet app cannot recompute these formulas: it would
72
+ show #NAME?, and Excel would report a damaged file for a `[1]` with no link part. So, in the saved .xlsx,
73
+ - each such cell is rewritten as its formula (exactly as in the file) plus the result shown in the grid, as the
74
+ cached value, and
75
+ - an `externalLink` part is added for every `[n]` used, pointing at a placeholder file name (Excel offers to
76
+ update links, as it does for any workbook with them).
77
+ `cells` is {sheet number: {A1 address: (formula, shown, is_error)}}."""
78
+ import zipfile
79
+ from xml.sax.saxutils import escape
80
+ from openpyxl.utils.datetime import to_excel
81
+
82
+ def cached(v, error: bool) -> tuple[str, str] | None:
83
+ if v is None:
84
+ return None
85
+ if error:
86
+ return "e", escape(str(v))
87
+ if isinstance(v, bool):
88
+ return "b", "1" if v else "0"
89
+ if isinstance(v, (int, float)):
90
+ return "", repr(v)
91
+ if isinstance(v, (dt.datetime, dt.time)):
92
+ return "", repr(to_excel(v))
93
+ return "str", escape(str(v))
94
+
95
+ links: dict[int, set[str]] = {}
96
+ for per_sheet in cells.values():
97
+ for formula, _, _ in per_sheet.values():
98
+ for m in re.finditer(r"\[(\d+)\]([^!]*)!", formula):
99
+ links.setdefault(int(m.group(1)), set()).add(m.group(2).strip("'"))
100
+ if any(n > MAX_LINKS for n in links):
101
+ raise ValueError(f"external link index above {MAX_LINKS}")
102
+ top = max(links, default=0)
103
+ main = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
104
+ rel = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
105
+ attr = {'"': "&quot;"}
106
+
107
+ def patch_sheet(xml: str, per_sheet: dict) -> str:
108
+ def put(m):
109
+ addr = m.group(1)
110
+ if addr not in per_sheet:
111
+ return m.group(0)
112
+ formula, shown, err = per_sheet[addr]
113
+ style = re.search(r'\bs="(\d+)"', m.group(2))
114
+ hit = cached(shown, err)
115
+ t = f' t="{hit[0]}"' if hit and hit[0] else ""
116
+ v = f"<v>{hit[1]}</v>" if hit else ""
117
+ s = f' s="{style.group(1)}"' if style else ""
118
+ return f'<c r="{addr}"{s}{t}><f>{escape(formula)}</f>{v}</c>'
119
+ return re.sub(r'<c r="([A-Z]+\d+)"([^>]*?)(?:/>|>.*?</c>)', put, xml, flags=re.S)
120
+
121
+ tmp = path.with_suffix(".tmp")
122
+ with zipfile.ZipFile(path) as zin, zipfile.ZipFile(tmp, "w", zipfile.ZIP_DEFLATED) as zout:
123
+ for item in zin.infolist():
124
+ data = zin.read(item.filename)
125
+ m = re.fullmatch(r"xl/worksheets/sheet(\d+)\.xml", item.filename)
126
+ if m and int(m.group(1)) in cells:
127
+ data = patch_sheet(data.decode("utf-8"), cells[int(m.group(1))]).encode("utf-8")
128
+ elif top and item.filename == "xl/workbook.xml":
129
+ refs = "".join(f'<externalReference xmlns:r="{rel}" r:id="rIdExt{n}"/>' for n in range(1, top + 1))
130
+ data = data.decode("utf-8").replace("</sheets>", f"</sheets><externalReferences>{refs}</externalReferences>", 1).encode("utf-8")
131
+ elif top and item.filename == "xl/_rels/workbook.xml.rels":
132
+ rels = "".join(f'<Relationship Id="rIdExt{n}" Type="{rel}/externalLink" Target="externalLinks/externalLink{n}.xml"/>'
133
+ for n in range(1, top + 1))
134
+ data = data.decode("utf-8").replace("</Relationships>", rels + "</Relationships>", 1).encode("utf-8")
135
+ elif top and item.filename == "[Content_Types].xml":
136
+ over = "".join(f'<Override PartName="/xl/externalLinks/externalLink{n}.xml" ContentType="application/'
137
+ f'vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml"/>' for n in range(1, top + 1))
138
+ data = data.decode("utf-8").replace("</Types>", over + "</Types>", 1).encode("utf-8")
139
+ zout.writestr(item, data)
140
+ for n in range(1, top + 1):
141
+ names = "".join(f'<sheetName val="{escape(x, attr)}"/>' for x in sorted(links.get(n, ()))) or '<sheetName val="Sheet1"/>'
142
+ zout.writestr(f"xl/externalLinks/externalLink{n}.xml",
143
+ f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<externalLink xmlns="{main}">'
144
+ f'<externalBook xmlns:r="{rel}" r:id="rId1"><sheetNames>{names}</sheetNames></externalBook></externalLink>')
145
+ zout.writestr(f"xl/externalLinks/_rels/externalLink{n}.xml.rels",
146
+ f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<Relationships xmlns="http://schemas.openxmlformats.org/'
147
+ f'package/2006/relationships"><Relationship Id="rId1" Type="{rel}/externalLinkPath" '
148
+ f'Target="external-{n}.xlsx" TargetMode="External"/></Relationships>')
149
+ tmp.replace(path)
150
+
151
+
152
+ @dataclass
153
+ class Cell:
154
+ value: object = None # typed value (None for formula cells)
155
+ formula: str | None = None # without the leading "="
156
+ number_format: str = "General"
157
+ error: bool = False # value is an Excel error literal, not text
158
+ array_ref: str | None = None # set on the anchor of an array formula: the range it fills
159
+ shown: object = None # a formula cell's displayed result (used for external-link formulas only)
160
+ shown_error: bool = False
161
+
162
+
163
+ @dataclass
164
+ class Sheet:
165
+ name: str
166
+ state: str = "visible"
167
+ freeze: str | None = None
168
+ cells: dict[tuple[int, int], Cell] = field(default_factory=dict)
169
+ merges: list[str] = field(default_factory=list)
170
+ names: dict[str, str] = field(default_factory=dict)
171
+ comments: dict[tuple[int, int], str] = field(default_factory=dict)
172
+ validations: list[tuple[str, str, str | None, str, str | None]] = field(default_factory=list) # range, type, operator, f1, f2
173
+
174
+
175
+ @dataclass
176
+ class Workbook:
177
+ sheets: list[Sheet] = field(default_factory=list)
178
+ names: dict[str, str] = field(default_factory=dict)
179
+ meta: dict[str, str] = field(default_factory=dict) # front matter
180
+ warnings: list[str] = field(default_factory=list)
181
+
182
+ def sheet(self, name: str) -> Sheet:
183
+ return next(s for s in self.sheets if s.name == name)
184
+
185
+ def to_openpyxl(self):
186
+ from openpyxl import Workbook as XlWorkbook
187
+ from openpyxl.comments import Comment
188
+ from openpyxl.worksheet.formula import ArrayFormula
189
+ from openpyxl.worksheet.datavalidation import DataValidation
190
+ from openpyxl.workbook.defined_name import DefinedName
191
+
192
+ wb = XlWorkbook()
193
+ wb.remove(wb.active)
194
+ for sh in self.sheets:
195
+ ws = wb.create_sheet(sh.name)
196
+ for (r, c), cell in sorted(sh.cells.items()):
197
+ x = ws.cell(row=r, column=c)
198
+ if cell.array_ref:
199
+ x.value = ArrayFormula(cell.array_ref, "=" + cell.formula)
200
+ elif cell.formula is not None:
201
+ x.value = "=" + cell.formula
202
+ else:
203
+ x.value = cell.value
204
+ if isinstance(cell.value, str) and not cell.error:
205
+ x.data_type = "s" # keep "=x" and "#N/A" text as text, not formula or error
206
+ # a date typed in must not pick up openpyxl's default date format unless the file lists one
207
+ x.number_format = cell.number_format
208
+ for rng in sh.merges:
209
+ ws.merge_cells(rng)
210
+ for (r, c), text in sorted(sh.comments.items()):
211
+ ws.cell(row=r, column=c).comment = Comment(text, "")
212
+ for rng, kind, op, f1, f2 in sh.validations:
213
+ ws.add_data_validation(DataValidation(type=kind, operator=op, formula1=f1, formula2=f2, sqref=rng,
214
+ showErrorMessage=True))
215
+ if sh.freeze:
216
+ ws.freeze_panes = sh.freeze
217
+ ws.sheet_state = sh.state
218
+ for n, text in sh.names.items():
219
+ ws.defined_names[n] = DefinedName(n, attr_text=text)
220
+ for n, text in self.names.items():
221
+ wb.defined_names[n] = DefinedName(n, attr_text=text)
222
+ return wb
223
+
224
+ def _external_results(self) -> dict[int, dict[str, tuple[str, object, bool]]]:
225
+ """{sheet number: {A1 address: (formula, shown result, is_error)}} for formulas that refer to another workbook."""
226
+ out: dict[int, dict[str, tuple[str, object, bool]]] = {}
227
+ for n, sh in enumerate(self.sheets, start=1):
228
+ for (r, c), cell in sh.cells.items():
229
+ if cell.formula is not None and EXTERNAL.search(cell.formula):
230
+ out.setdefault(n, {})[f"{L(c)}{r}"] = cell.formula, cell.shown, cell.shown_error
231
+ return out
232
+
233
+ def to_xlsx(self, path: str | Path, *, recalc: str = "never") -> Path:
234
+ """Write an .xlsx. recalc="never" keeps exactly what was written (formulas have no cached values, so
235
+ Excel computes them on open); "auto"/"always" pass the file through headless LibreOffice so values are
236
+ cached, which also lets LibreOffice rewrite styles and formats."""
237
+ path = Path(path)
238
+ with _exact_floats():
239
+ self.to_openpyxl().save(path)
240
+ if recalc == "always" or (recalc == "auto" and any(
241
+ c.formula is not None for s in self.sheets for c in s.cells.values())):
242
+ from .recalc import recalc_copy, soffice
243
+ if recalc == "always" or soffice():
244
+ done = recalc_copy(path)
245
+ path.write_bytes(done.read_bytes())
246
+ external = self._external_results()
247
+ if external:
248
+ self.warnings.append("formulas that refer to another workbook keep their shown result as the cached "
249
+ "value; the link itself is not carried")
250
+ _patch_external(path, external)
251
+ return path
252
+
253
+
254
+ # ------------------------------------------------------------------ values
255
+ def split_row(line: str, n: int) -> list[str]:
256
+ """Split `|a|b|c|` on unescaped pipes. Escapes stay in the returned cells; see decode_cell."""
257
+ if not line.startswith("|"):
258
+ raise ParseError("table row must start with '|'", n)
259
+ cells, cur, i = [], [], 1
260
+ while i < len(line):
261
+ ch = line[i]
262
+ if ch == "\\" and i + 1 < len(line) and line[i + 1] in ESCAPES:
263
+ cur.append(line[i:i + 2])
264
+ i += 2
265
+ continue
266
+ if ch == "|":
267
+ cells.append("".join(cur))
268
+ cur = []
269
+ else:
270
+ cur.append(ch)
271
+ i += 1
272
+ if cur:
273
+ raise ParseError("table row must end with '|'", n)
274
+ return cells
275
+
276
+
277
+ def split_row_bare(line: str) -> list[str]:
278
+ """Split a v0.3 row (`2|Units|1200`, no outer pipes) on unescaped pipes. A row ending in a pipe has an empty last cell."""
279
+ cells, cur, i = [], [], 0
280
+ while i < len(line):
281
+ ch = line[i]
282
+ if ch == "\\" and i + 1 < len(line) and line[i + 1] in ESCAPES:
283
+ cur.append(line[i:i + 2])
284
+ i += 2
285
+ continue
286
+ if ch == "|":
287
+ cells.append("".join(cur))
288
+ cur = []
289
+ else:
290
+ cur.append(ch)
291
+ i += 1
292
+ cells.append("".join(cur))
293
+ return cells
294
+
295
+
296
+ def unescape(s: str) -> str:
297
+ """Resolve backslash escapes (before a backslash, pipe or <) and turn the <br> token into a newline, in one pass."""
298
+ out, i = [], 0
299
+ while i < len(s):
300
+ if s[i] == "\\" and i + 1 < len(s) and s[i + 1] in ESCAPES:
301
+ out.append(ESCAPES[s[i + 1]])
302
+ i += 2
303
+ elif s.startswith("<br>", i):
304
+ out.append("\n")
305
+ i += 4
306
+ else:
307
+ out.append(s[i])
308
+ i += 1
309
+ return "".join(out)
310
+
311
+
312
+ def decode_cell(raw: str) -> tuple[object, bool]:
313
+ """Raw grid text -> (value, is_error). Inverse of writer.fmt_value for typed values."""
314
+ if raw == "":
315
+ return None, False
316
+ if len(raw) >= 2 and raw[0] == '"' and raw[-1] == '"': # quoted: always text
317
+ inner = unescape(raw[1:-1].replace('""', '"'))
318
+ return (inner[1:] if inner.startswith("'") else inner), False
319
+ if raw.startswith("'"): # apostrophe: text that would read as another type
320
+ return unescape(raw[1:]), False
321
+ if raw in ("TRUE", "FALSE"):
322
+ return raw == "TRUE", False
323
+ if ERROR.match(raw):
324
+ return raw, True
325
+ if NUMBER.match(raw):
326
+ return (float(raw) if any(k in raw for k in ".eE") else int(raw)), False
327
+ m = DATETIME.match(raw)
328
+ if m:
329
+ y, mo, d, hh, mi, ss = m.groups()
330
+ try:
331
+ return dt.datetime(int(y), int(mo), int(d), int(hh or 0), int(mi or 0), int(ss or 0)), False
332
+ except ValueError:
333
+ pass
334
+ m = TIME.match(raw)
335
+ if m:
336
+ try:
337
+ return dt.time(*map(int, m.groups())), False
338
+ except ValueError:
339
+ pass
340
+ return unescape(raw), False
341
+
342
+
343
+ # ------------------------------------------------------------------ parsing
344
+ def _cells_in(rng: str, n: int):
345
+ try:
346
+ c1, r1, c2, r2 = range_boundaries(rng)
347
+ except Exception:
348
+ raise ParseError(f"bad range {rng!r}", n) from None
349
+ if None in (c1, r1, c2, r2):
350
+ raise ParseError(f"bad range {rng!r}", n)
351
+ if (r2 - r1 + 1) * (c2 - c1 + 1) > MAX_RANGE_CELLS:
352
+ raise ParseError(f"range {rng!r} covers more than {MAX_RANGE_CELLS} cells", n)
353
+ return [(r, c) for r in range(r1, r2 + 1) for c in range(c1, c2 + 1)]
354
+
355
+
356
+ def _anchor(rng: str) -> str:
357
+ return rng.split(":")[0]
358
+
359
+
360
+ def _split_assign(line: str, n: int) -> tuple[str, str]:
361
+ left, sep, right = line.partition(" = ")
362
+ if not sep:
363
+ raise ParseError(f"expected 'RANGE = ...', got {line!r}", n)
364
+ return left.strip(), right
365
+
366
+
367
+ def _sheet_name(heading: str) -> str:
368
+ name = heading
369
+ if len(name) >= 2 and name[0] == '"' and name[-1] == '"' and "#" in name[1:-1]:
370
+ name = name[1:-1]
371
+ return name
372
+
373
+
374
+ def load(text: str) -> Workbook:
375
+ """Parse Cellprose v0.3 text (or v0.2, which differs only in the grid and arrays) into a Workbook."""
376
+ lines = text.split("\n")
377
+ wb = Workbook()
378
+ i = 0
379
+
380
+ # front matter
381
+ if not lines or lines[0].strip() != "---":
382
+ raise ParseError("file must start with '---' front matter", 1)
383
+ i = 1
384
+ while i < len(lines) and lines[i].strip() != "---":
385
+ k, sep, v = lines[i].partition(":")
386
+ if sep:
387
+ v = v.strip()
388
+ if len(v) >= 2 and v[0] == v[-1] == "'":
389
+ v = v[1:-1]
390
+ wb.meta[k.strip()] = v
391
+ i += 1
392
+ if i >= len(lines):
393
+ raise ParseError("front matter is not closed", len(lines))
394
+ i += 1
395
+ version = wb.meta.get("cellprose") or wb.meta.get("sheetmark") # `sheetmark:` is the key of files written before the rename
396
+ if version not in SUPPORTED:
397
+ raise ParseError(f"unsupported cellprose version {version!r}; this reader handles {', '.join(SUPPORTED)}", 2)
398
+ bare = version != "0.2"
399
+ if wb.meta.get("profile") == "compact":
400
+ raise CompactProfileError("compact-profile files omit rows and cannot be converted back to a workbook", 2)
401
+
402
+ sheet: Sheet | None = None
403
+ region: tuple[int, int, int, int] | None = None
404
+ pending_formulas: list[tuple[int, str]] = []
405
+ while i < len(lines):
406
+ line, n = lines[i], i + 1
407
+ if not line.strip():
408
+ i += 1
409
+ elif line.startswith("# Sheet: "):
410
+ sheet = Sheet(_sheet_name(line[len("# Sheet: "):]))
411
+ wb.sheets.append(sheet)
412
+ region = None
413
+ i += 1
414
+ elif line.strip() == "# Names":
415
+ sheet, region = None, None
416
+ i += 1
417
+ elif line.startswith("## "):
418
+ if sheet is None:
419
+ raise ParseError("region heading outside a sheet", n)
420
+ rng = line[3:].strip()
421
+ if not RANGE.match(rng):
422
+ raise ParseError(f"bad region heading {rng!r}", n)
423
+ c1, r1, c2, r2 = range_boundaries(rng)
424
+ region = (r1, c1, r2, c2)
425
+ i += 1
426
+ elif line.startswith("#|") if bare else line.startswith("|"):
427
+ if sheet is None:
428
+ raise ParseError("table outside a sheet", n)
429
+ start = i
430
+ while i < len(lines) and (lines[i].strip() if bare else lines[i].startswith("|")):
431
+ i += 1
432
+ _read_table(sheet, lines[start:i], start + 1, wb, bare)
433
+ elif line.startswith("```"):
434
+ kind = line[3:].strip()
435
+ start = i + 1
436
+ j = start
437
+ while j < len(lines) and lines[j].strip() != "```":
438
+ j += 1
439
+ if j >= len(lines):
440
+ raise ParseError(f"unclosed ```{kind} block", n)
441
+ body = [(k + 1, lines[k]) for k in range(start, j)]
442
+ _read_block(wb, sheet, kind, body, n)
443
+ i = j + 1
444
+ elif line.startswith(LEGEND_PREFIX):
445
+ i += 1 # the row-number legend (SPEC §3.1): prose for the model, nothing to read
446
+ else:
447
+ wb.warnings.append(f"line {n}: ignored {line[:60]!r}")
448
+ i += 1
449
+ if sum(len(sh.cells) for sh in wb.sheets) > MAX_CELLS:
450
+ raise ParseError(f"file defines more than {MAX_CELLS} cells")
451
+ return wb
452
+
453
+
454
+ def load_file(path: str | Path) -> Workbook:
455
+ return load(Path(path).read_text(encoding="utf-8"))
456
+
457
+
458
+ def _read_table(sheet: Sheet, rows: list[str], first_line: int, wb: Workbook, bare: bool = False) -> None:
459
+ if len(rows) < 2:
460
+ raise ParseError("table needs a header and a separator row", first_line)
461
+ split = (lambda row, _n: split_row_bare(row)) if bare else split_row
462
+ head = split(rows[0], first_line)
463
+ if not head or head[0] != "#":
464
+ raise ParseError("grid header must start with '#'", first_line)
465
+ try:
466
+ cols = [column_index_from_string(h) for h in head[1:]]
467
+ except ValueError:
468
+ raise ParseError(f"bad column letters in {rows[0]!r}", first_line) from None
469
+ sep = split(rows[1], first_line + 1)
470
+ if len(sep) != len(head) or not all(re.fullmatch(r"-+", s) for s in sep):
471
+ raise ParseError("second table row must be the '|-|-|' separator", first_line + 1)
472
+ for k, row in enumerate(rows[2:], start=first_line + 2):
473
+ cells = split(row, k)
474
+ if cells and cells[0] == "…":
475
+ continue # elision marker: only appears in compact files, which are rejected
476
+ if bare and len(cells) < len(head):
477
+ cells += [""] * (len(head) - len(cells)) # trailing empty cells are left out of a v0.3 row
478
+ if len(cells) != len(head):
479
+ raise ParseError(f"expected {len(head)} cells, found {len(cells)}", k)
480
+ try:
481
+ r = int(cells[0])
482
+ except ValueError:
483
+ raise ParseError(f"row label {cells[0]!r} is not a number", k) from None
484
+ for c, raw in zip(cols, cells[1:]):
485
+ v, err = decode_cell(raw)
486
+ if v is not None:
487
+ sheet.cells[(r, c)] = Cell(value=v, error=err)
488
+
489
+
490
+ def _read_block(wb: Workbook, sheet: Sheet | None, kind: str, body, n: int) -> None:
491
+ if kind == "names":
492
+ for k, line in body:
493
+ if not line.strip():
494
+ continue
495
+ left, right = _split_assign(line, k)
496
+ if "!" in left:
497
+ sh, name = left.rsplit("!", 1)
498
+ try:
499
+ wb.sheet(sh).names[name] = right
500
+ except StopIteration:
501
+ raise ParseError(f"name {left!r} refers to unknown sheet {sh!r}", k) from None
502
+ else:
503
+ wb.names[left] = right
504
+ return
505
+ if sheet is None:
506
+ raise ParseError(f"```{kind} block outside a sheet", n)
507
+ if kind == "meta":
508
+ for k, line in body:
509
+ key, sep, val = line.partition(":")
510
+ if not sep:
511
+ raise ParseError(f"bad meta line {line!r}", k)
512
+ key, val = key.strip(), val.strip()
513
+ if key == "hidden":
514
+ sheet.state = "hidden" if val == "true" else val
515
+ elif key == "freeze":
516
+ sheet.freeze = val
517
+ elif kind == "formulas":
518
+ for k, line in body:
519
+ if not line.strip():
520
+ continue
521
+ rng, formula = _split_assign(line, k)
522
+ anchor = _anchor(rng)
523
+ for (r, c) in _cells_in(rng, k):
524
+ dest = f"{L(c)}{r}"
525
+ f = formula if dest == anchor else Translator("=" + formula, origin=anchor).translate_formula(dest)[1:]
526
+ cell = sheet.cells.setdefault((r, c), Cell())
527
+ cell.shown, cell.shown_error = cell.value, cell.error
528
+ cell.formula, cell.value = f, None
529
+ elif kind == "formats":
530
+ for k, line in body:
531
+ if not line.strip():
532
+ continue
533
+ rng, fmt = _split_assign(line, k)
534
+ for pos in _cells_in(rng, k):
535
+ sheet.cells.setdefault(pos, Cell()).number_format = fmt
536
+ elif kind == "cells":
537
+ for k, line in body:
538
+ if not line.strip():
539
+ continue
540
+ m = re.match(r"^([A-Z]{1,3}[0-9]+)(: | = )(.*)$", line)
541
+ if not m:
542
+ raise ParseError(f"bad cells line {line!r}", k)
543
+ addr, sep, rest = m.groups()
544
+ c, r = range_boundaries(addr)[:2]
545
+ if sep == " = ":
546
+ formula, hash_sep, _shown = rest.rpartition(" # ") # trailing comment = cached display value
547
+ f = formula if hash_sep else rest
548
+ shown = _shown.strip() if hash_sep else ""
549
+ v, err = (None, False) if shown in ("", "?") else ("", False) if shown == '""' else decode_cell(shown)
550
+ sheet.cells[(r, c)] = Cell(formula=f, shown=v, shown_error=err)
551
+ else:
552
+ v, err = decode_cell(rest)
553
+ # the cells block carries no number formats, so keep dates and times readable as dates
554
+ fmt = ("hh:mm:ss" if isinstance(v, dt.time) else
555
+ "yyyy-mm-dd" if isinstance(v, dt.datetime) and v.time() == dt.time(0) else
556
+ "yyyy-mm-dd hh:mm:ss" if isinstance(v, dt.datetime) else "General")
557
+ sheet.cells[(r, c)] = Cell(value=v, error=err, number_format=fmt)
558
+ elif kind == "merges":
559
+ sheet.merges += [line.strip() for _, line in body if line.strip()]
560
+ elif kind == "arrays":
561
+ for k, line in body:
562
+ if not line.strip():
563
+ continue
564
+ rng, formula = _split_assign(line, k)
565
+ anchor = sheet.cells.setdefault(_cells_in(rng, k)[0], Cell())
566
+ anchor.shown, anchor.shown_error = anchor.value, anchor.error
567
+ anchor.formula, anchor.value, anchor.array_ref = formula, None, rng
568
+ elif kind == "comments":
569
+ for k, line in body:
570
+ if not line.strip():
571
+ continue
572
+ m = re.match(r"^([A-Z]{1,3}[0-9]+): (.*)$", line)
573
+ if not m:
574
+ raise ParseError(f"bad comments line {line!r}", k)
575
+ c, r = range_boundaries(m.group(1))[:2]
576
+ sheet.comments[(r, c)] = unescape(m.group(2))
577
+ elif kind == "validation":
578
+ for k, line in body:
579
+ if not line.strip():
580
+ continue
581
+ rng, rule = _split_assign(line, k)
582
+ head, sep, formulas = rule.partition(": ")
583
+ kind_op = head.split(" ")
584
+ if not sep or not RANGE.match(rng) or len(kind_op) > 2:
585
+ raise ParseError(f"bad validation line {line!r}", k)
586
+ f1, has2, f2 = formulas.partition(" ; ")
587
+ sheet.validations.append((rng, kind_op[0], kind_op[1] if len(kind_op) == 2 else None, f1, f2 if has2 else None))
588
+ elif kind == "elided":
589
+ raise CompactProfileError("compact-profile files omit rows and cannot be converted back to a workbook", n)
590
+ else:
591
+ wb.warnings.append(f"line {n}: unknown block ```{kind}")
cellprose/recalc.py ADDED
@@ -0,0 +1,39 @@
1
+ """Recalculate a workbook's formulas with headless LibreOffice, so cached values exist."""
2
+ from __future__ import annotations
3
+
4
+ import os
5
+ import shutil
6
+ import subprocess
7
+ import tempfile
8
+ from pathlib import Path
9
+
10
+
11
+ def soffice() -> str | None:
12
+ for name in ("soffice", "libreoffice"):
13
+ p = shutil.which(name)
14
+ if p:
15
+ return p
16
+ mac = "/Applications/LibreOffice.app/Contents/MacOS/soffice"
17
+ return mac if os.path.exists(mac) else None
18
+
19
+
20
+ def recalc_copy(path: str | Path, timeout: int = 300) -> Path:
21
+ """Return the path of a recalculated copy in a temp folder. Raises RuntimeError if LibreOffice is missing."""
22
+ exe = soffice()
23
+ if not exe:
24
+ raise RuntimeError("LibreOffice (soffice) not found; install it to compute formula values")
25
+ path = Path(path)
26
+ out = Path(tempfile.mkdtemp(prefix="cellprose-"))
27
+ profile = out / "profile" # private profile: works even while the user has LibreOffice open
28
+ try:
29
+ subprocess.run([exe, f"-env:UserInstallation={profile.as_uri()}", "--headless", "--calc",
30
+ "--convert-to", "xlsx", "--outdir", str(out), str(path)],
31
+ check=True, capture_output=True, timeout=timeout)
32
+ result = out / (path.stem + ".xlsx")
33
+ if not result.exists():
34
+ raise RuntimeError(f"LibreOffice did not produce {result.name}")
35
+ except BaseException:
36
+ shutil.rmtree(out, ignore_errors=True)
37
+ raise
38
+ shutil.rmtree(profile, ignore_errors=True) # the converted copy stays; the callers' temp dirs are small
39
+ return result