transcript.md 0.1.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.
- transcript/__init__.py +35 -0
- transcript/_rule/__init__.py +0 -0
- transcript/_rule/archive.py +128 -0
- transcript/_rule/conda.py +52 -0
- transcript/_rule/container.py +290 -0
- transcript/_rule/core.py +524 -0
- transcript/_rule/lambdas.py +120 -0
- transcript/_rule/registry.py +54 -0
- transcript/_rule/serialize.py +26 -0
- transcript/_rule/snakefile.py +81 -0
- transcript/_rule/wildcards.py +110 -0
- transcript/cli.py +363 -0
- transcript/document.py +56 -0
- transcript/execute.py +240 -0
- transcript/html/__init__.py +17 -0
- transcript/html/_assets/.frontend.sha256 +1 -0
- transcript/html/_assets/__init__.py +21 -0
- transcript/html/_assets/style.min.css +1 -0
- transcript/html/_assets/transcript.min.js +21 -0
- transcript/html/_island.py +72 -0
- transcript/html/_navigate.py +198 -0
- transcript/html/_output.py +100 -0
- transcript/html/_resources.py +171 -0
- transcript/html/_review.py +34 -0
- transcript/html/markup.py +26 -0
- transcript/html/page.py +107 -0
- transcript/output.py +308 -0
- transcript/parse.py +256 -0
- transcript/renderers/__init__.py +126 -0
- transcript/renderers/bitmap.py +166 -0
- transcript/renderers/compose.py +174 -0
- transcript/renderers/compose_runtime.js +233 -0
- transcript/renderers/datavzrd.py +321 -0
- transcript/renderers/datavzrd_runtime.js +362 -0
- transcript/renderers/label.py +211 -0
- transcript/renderers/markdown.py +45 -0
- transcript/renderers/table.py +32 -0
- transcript/renderers/vegalite.py +250 -0
- transcript/review/__init__.py +13 -0
- transcript/review/_block.py +167 -0
- transcript/review/_migrate.py +212 -0
- transcript/review/_records.py +238 -0
- transcript/review/_span.py +287 -0
- transcript/review/load.py +64 -0
- transcript/tmd.py +159 -0
- transcript_md-0.1.0.dist-info/METADATA +17 -0
- transcript_md-0.1.0.dist-info/RECORD +49 -0
- transcript_md-0.1.0.dist-info/WHEEL +4 -0
- transcript_md-0.1.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"""The ``L``/``C`` coordinate grammar shared by review records.
|
|
2
|
+
|
|
3
|
+
The grammar is 1-based, but :class:`Span` stores 0-based half-open positions
|
|
4
|
+
(``[start, end)``) so it matches Python slicing. A range ``A-B`` runs from A's
|
|
5
|
+
start to B's end; ``+N`` counts N characters from the start; a bare line ``L5``
|
|
6
|
+
means the whole line (up to the next line's start) while ``L5C3`` is a single
|
|
7
|
+
character. A zero-length point is ``L5C3+0`` and is stored as ``start == end``.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import bisect
|
|
13
|
+
from datetime import UTC, datetime
|
|
14
|
+
from typing import NamedTuple
|
|
15
|
+
|
|
16
|
+
TYPE_SOURCE = "source"
|
|
17
|
+
TYPE_REVIEW = "review"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _parse_point(text: str):
|
|
21
|
+
if not text.startswith("L"):
|
|
22
|
+
raise ValueError(f"invalid coordinate: {text!r}")
|
|
23
|
+
line_text, column_mark, column_text = text.strip()[1:].partition("C")
|
|
24
|
+
return int(line_text), (int(column_text) if column_mark else None)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Span(NamedTuple):
|
|
28
|
+
"""A 0-based half-open character range ``[start, end)``.
|
|
29
|
+
|
|
30
|
+
A whole line ``L5`` is ``Span(4, 0, 5, 0)`` (ending at the next line's
|
|
31
|
+
start); a zero-length point ``L5C3+0`` is ``Span(4, 2, 4, 2)``.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
start_line: int
|
|
35
|
+
"""0-based line"""
|
|
36
|
+
start_col: int
|
|
37
|
+
"""0-based column"""
|
|
38
|
+
end_line: int
|
|
39
|
+
"""0-based exclusive line"""
|
|
40
|
+
end_col: int
|
|
41
|
+
"""0-based exclusive column"""
|
|
42
|
+
|
|
43
|
+
def lines(self):
|
|
44
|
+
"""
|
|
45
|
+
>>> list(Span.parse("L1-L2").lines())
|
|
46
|
+
[0, 1]
|
|
47
|
+
>>> list(Span.parse("L5C3+1").lines())
|
|
48
|
+
[4]
|
|
49
|
+
>>> list(Span.parse("L5C3+0").lines())
|
|
50
|
+
[]
|
|
51
|
+
"""
|
|
52
|
+
if (self.start_line, self.start_col) >= (self.end_line, self.end_col):
|
|
53
|
+
return range(0)
|
|
54
|
+
last = self.end_line if self.end_col > 0 else self.end_line - 1
|
|
55
|
+
return range(self.start_line, last + 1)
|
|
56
|
+
|
|
57
|
+
@classmethod
|
|
58
|
+
def empty(cls, line: int, col: int):
|
|
59
|
+
"""A zero-length point at 0-based *line*/*col*, written ``LxCc+0``.
|
|
60
|
+
|
|
61
|
+
>>> Span.empty(1, 0)
|
|
62
|
+
Span(start_line=1, start_col=0, end_line=1, end_col=0)
|
|
63
|
+
>>> str(Span.empty(2, 3))
|
|
64
|
+
'L3C4+0'
|
|
65
|
+
"""
|
|
66
|
+
return cls(line, col, line, col)
|
|
67
|
+
|
|
68
|
+
@classmethod
|
|
69
|
+
def parse(cls, text: str):
|
|
70
|
+
"""Parse a coordinate or range in the ``L/C`` grammar.
|
|
71
|
+
|
|
72
|
+
>>> Span.parse("L5")
|
|
73
|
+
Span(start_line=4, start_col=0, end_line=5, end_col=0)
|
|
74
|
+
>>> Span.parse("L5C3+1")
|
|
75
|
+
Span(start_line=4, start_col=2, end_line=4, end_col=3)
|
|
76
|
+
>>> Span.parse("L5C3+4")
|
|
77
|
+
Span(start_line=4, start_col=2, end_line=4, end_col=6)
|
|
78
|
+
>>> Span.parse("L5C3+0")
|
|
79
|
+
Span(start_line=4, start_col=2, end_line=4, end_col=2)
|
|
80
|
+
>>> Span.parse("L1C1+0")
|
|
81
|
+
Span(start_line=0, start_col=0, end_line=0, end_col=0)
|
|
82
|
+
>>> Span.parse("L7-L9")
|
|
83
|
+
Span(start_line=6, start_col=0, end_line=9, end_col=0)
|
|
84
|
+
>>> Span.parse("L7-L9C1")
|
|
85
|
+
Span(start_line=6, start_col=0, end_line=8, end_col=1)
|
|
86
|
+
>>> Span.parse("L7C1-L9C1") == Span.parse("L7-L9C1")
|
|
87
|
+
True
|
|
88
|
+
>>> Span.parse("L5+2")
|
|
89
|
+
Traceback (most recent call last):
|
|
90
|
+
...
|
|
91
|
+
ValueError: invalid coordinate: 'L5+2'
|
|
92
|
+
>>> Span.parse("5")
|
|
93
|
+
Traceback (most recent call last):
|
|
94
|
+
...
|
|
95
|
+
ValueError: invalid coordinate: '5'
|
|
96
|
+
"""
|
|
97
|
+
text = text.strip()
|
|
98
|
+
if "+" in text:
|
|
99
|
+
point, length = text.split("+", 1)
|
|
100
|
+
line, col = _parse_point(point)
|
|
101
|
+
if col is None:
|
|
102
|
+
raise ValueError(f"invalid coordinate: {text!r}")
|
|
103
|
+
return cls(line - 1, col - 1, line - 1, col - 1 + int(length))
|
|
104
|
+
start_text, separator, end_text = text.partition("-")
|
|
105
|
+
if not separator:
|
|
106
|
+
line, col = _parse_point(start_text)
|
|
107
|
+
if col is None:
|
|
108
|
+
return cls(line - 1, 0, line, 0)
|
|
109
|
+
return cls(line - 1, col - 1, line - 1, col)
|
|
110
|
+
start_line, start_col = _parse_point(start_text)
|
|
111
|
+
end_line, end_col = _parse_point(end_text)
|
|
112
|
+
return cls(
|
|
113
|
+
start_line - 1,
|
|
114
|
+
(start_col - 1) if start_col is not None else 0,
|
|
115
|
+
end_line if end_col is None else end_line - 1,
|
|
116
|
+
end_col if end_col is not None else 0,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def __str__(self):
|
|
120
|
+
"""Render a span back into the canonical coordinate grammar.
|
|
121
|
+
|
|
122
|
+
>>> str(Span(4, 0, 5, 0))
|
|
123
|
+
'L5'
|
|
124
|
+
>>> str(Span(4, 2, 4, 3))
|
|
125
|
+
'L5C3+1'
|
|
126
|
+
>>> str(Span(4, 2, 4, 6))
|
|
127
|
+
'L5C3+4'
|
|
128
|
+
>>> str(Span.empty(4, 2))
|
|
129
|
+
'L5C3+0'
|
|
130
|
+
>>> str(Span(6, 0, 9, 0))
|
|
131
|
+
'L7-L9'
|
|
132
|
+
>>> str(Span(6, 0, 8, 1))
|
|
133
|
+
'L7-L9C1'
|
|
134
|
+
>>> str(Span(6, 1, 8, 1))
|
|
135
|
+
'L7C2-L9C1'
|
|
136
|
+
"""
|
|
137
|
+
l1, c1, l2, c2 = self
|
|
138
|
+
if l1 == l2:
|
|
139
|
+
return f"L{l1 + 1}C{c1 + 1}+{c2 - c1}"
|
|
140
|
+
if c1 == c2 == 0 and l2 == l1 + 1:
|
|
141
|
+
return f"L{l1 + 1}"
|
|
142
|
+
start = f"L{l1 + 1}"
|
|
143
|
+
if c1 != 0:
|
|
144
|
+
start += f"C{c1 + 1}"
|
|
145
|
+
end = f"L{l2}" if c2 == 0 else f"L{l2 + 1}C{c2}"
|
|
146
|
+
return f"{start}-{end}"
|
|
147
|
+
|
|
148
|
+
def to_span(self, parent: Span):
|
|
149
|
+
"""Map a shift (relative to the parent's first line) to absolute coordinates.
|
|
150
|
+
|
|
151
|
+
>>> parent = Span.parse("L5C3+4")
|
|
152
|
+
>>> span = Span.parse("L5C4+2")
|
|
153
|
+
>>> shift = span.to_shift(parent)
|
|
154
|
+
>>> shift
|
|
155
|
+
Span(start_line=0, start_col=1, end_line=0, end_col=3)
|
|
156
|
+
>>> shift.to_span(parent) == span
|
|
157
|
+
True
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
def line(relative: int):
|
|
161
|
+
return parent.start_line + relative
|
|
162
|
+
|
|
163
|
+
def column(relative_line: int, column: int):
|
|
164
|
+
return (parent.start_col if relative_line == 0 else 0) + column
|
|
165
|
+
|
|
166
|
+
return Span(
|
|
167
|
+
line(self.start_line),
|
|
168
|
+
column(self.start_line, self.start_col),
|
|
169
|
+
line(self.end_line),
|
|
170
|
+
column(self.end_line, self.end_col),
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
def to_shift(self, parent: Span):
|
|
174
|
+
"""Invert :func:`to_span` for a span inside the parent region."""
|
|
175
|
+
|
|
176
|
+
def line(absolute: int):
|
|
177
|
+
return absolute - parent.start_line
|
|
178
|
+
|
|
179
|
+
def column(absolute_line: int, column: int):
|
|
180
|
+
offset = parent.start_col if absolute_line == parent.start_line else 0
|
|
181
|
+
return column - offset
|
|
182
|
+
|
|
183
|
+
return Span(
|
|
184
|
+
line(self.start_line),
|
|
185
|
+
column(self.start_line, self.start_col),
|
|
186
|
+
line(self.end_line),
|
|
187
|
+
column(self.end_line, self.end_col),
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
def extract(self, text: str):
|
|
191
|
+
"""Return the substring this span selects, or ``None`` when out of range.
|
|
192
|
+
|
|
193
|
+
>>> text = "one\\ntwo\\nthree\\n"
|
|
194
|
+
>>> Span.parse("L2").extract(text)
|
|
195
|
+
'two\\n'
|
|
196
|
+
>>> Span.parse("L2C2+1").extract(text)
|
|
197
|
+
'w'
|
|
198
|
+
>>> Span.parse("L1-L2").extract(text)
|
|
199
|
+
'one\\ntwo\\n'
|
|
200
|
+
>>> Span.parse("L9").extract(text) is None
|
|
201
|
+
True
|
|
202
|
+
>>> Span.parse("L2C2+0").extract(text)
|
|
203
|
+
''
|
|
204
|
+
"""
|
|
205
|
+
starts = _starts(text)
|
|
206
|
+
if not 0 <= self.start_line < len(starts):
|
|
207
|
+
return None
|
|
208
|
+
if not 0 <= self.end_line < len(starts):
|
|
209
|
+
return None
|
|
210
|
+
start = starts[self.start_line] + self.start_col
|
|
211
|
+
end = starts[self.end_line] + self.end_col
|
|
212
|
+
if start < 0 or start > end or end > len(text):
|
|
213
|
+
return None
|
|
214
|
+
return text[start:end]
|
|
215
|
+
|
|
216
|
+
def bound(self, starts: list[int]):
|
|
217
|
+
"""The half-open character range a span selects."""
|
|
218
|
+
length = starts[-1]
|
|
219
|
+
if self.start_line >= len(starts) or self.end_line >= len(starts):
|
|
220
|
+
return length, length
|
|
221
|
+
start = starts[self.start_line] + self.start_col
|
|
222
|
+
stop = starts[self.end_line] + self.end_col
|
|
223
|
+
return start, min(stop, length)
|
|
224
|
+
|
|
225
|
+
@classmethod
|
|
226
|
+
def from_bound(cls, start: int, stop: int, starts: list[int]):
|
|
227
|
+
"""The span naming a half-open character range (whole lines when it fits)."""
|
|
228
|
+
first = _line(start, starts)
|
|
229
|
+
if stop <= start:
|
|
230
|
+
return cls.empty(first, start - starts[first])
|
|
231
|
+
last = _line(stop, starts)
|
|
232
|
+
if start == starts[first] and stop == starts[last] and last > first:
|
|
233
|
+
return cls(first, 0, last, 0)
|
|
234
|
+
return cls(first, start - starts[first], last, stop - starts[last])
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _line(offset: int, starts: list[int]):
|
|
238
|
+
return max(bisect.bisect_right(starts, offset) - 1, 0)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _starts(text: str):
|
|
242
|
+
"""The character offset at which each line begins."""
|
|
243
|
+
index = 0
|
|
244
|
+
starts = [index]
|
|
245
|
+
for index, char in enumerate(text, 1):
|
|
246
|
+
if char == "\n":
|
|
247
|
+
starts.append(index)
|
|
248
|
+
if starts[-1] != index:
|
|
249
|
+
starts.append(index)
|
|
250
|
+
return starts
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def timestamp(moment: datetime | None = None):
|
|
254
|
+
"""Return an ISO-8601 UTC timestamp with milliseconds and a ``Z`` suffix."""
|
|
255
|
+
moment = datetime.now(UTC) if moment is None else moment
|
|
256
|
+
moment = moment.astimezone(UTC)
|
|
257
|
+
return moment.strftime("%Y-%m-%dT%H:%M:%S.") + f"{moment.microsecond // 1000:03d}Z"
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def parse_time(value: str):
|
|
261
|
+
"""Parse an ISO-8601 record timestamp, accepting a trailing ``Z``."""
|
|
262
|
+
return datetime.fromisoformat(value.replace("Z", "+00:00")).astimezone(UTC)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
class At(NamedTuple):
|
|
266
|
+
"""A source span paired with its version boundary stamp."""
|
|
267
|
+
|
|
268
|
+
span: Span
|
|
269
|
+
time: str
|
|
270
|
+
|
|
271
|
+
def validate(self):
|
|
272
|
+
if not isinstance(self.span, Span) or not isinstance(self.time, str):
|
|
273
|
+
raise ValueError("at must pair a Span with a timestamp")
|
|
274
|
+
parse_time(self.time)
|
|
275
|
+
|
|
276
|
+
def __str__(self):
|
|
277
|
+
return f"{self.span}@{self.time}"
|
|
278
|
+
|
|
279
|
+
@classmethod
|
|
280
|
+
def parse(cls, text: str):
|
|
281
|
+
"""Parse one archive ``span@stamp`` value."""
|
|
282
|
+
if not isinstance(text, str):
|
|
283
|
+
raise ValueError("at must be a span@stamp string")
|
|
284
|
+
span, separator, stamp = text.partition("@")
|
|
285
|
+
if not separator or not span or not stamp or "@" in stamp:
|
|
286
|
+
raise ValueError("at must be a span@stamp string")
|
|
287
|
+
return cls(Span.parse(span), stamp)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""Read a review source from an archive or page, and choose one.
|
|
2
|
+
|
|
3
|
+
A ``.tmd.py`` or exported page is folded into a :class:`ReviewSource`;
|
|
4
|
+
:func:`reference_source` then picks one by priority and
|
|
5
|
+
:func:`~transcript.review._block.migrated_block` migrates it onto a new body.
|
|
6
|
+
The records themselves and the block grammar live in :mod:`._block`.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
from ..document import parse
|
|
15
|
+
from ..html import extract_parts
|
|
16
|
+
from ..tmd import body_hash
|
|
17
|
+
from ._block import ReviewSource, migrated_block, render_block
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from ..document import AstDocument
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def document_source(document: AstDocument):
|
|
24
|
+
"""The document's own review block as a :class:`ReviewSource`, or ``None``."""
|
|
25
|
+
if document.review is None:
|
|
26
|
+
return None
|
|
27
|
+
return ReviewSource.of(document.review, document.source)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def read_source(path: Path):
|
|
31
|
+
"""Read *path* (a ``.tmd.py`` or exported page) as a :class:`ReviewSource`.
|
|
32
|
+
|
|
33
|
+
Returns ``None`` when it carries no review block. A page is unfolded into
|
|
34
|
+
its body and records first, then stamped with the body hash.
|
|
35
|
+
"""
|
|
36
|
+
text = path.read_text(encoding="utf-8-sig")
|
|
37
|
+
if path.suffix == ".html":
|
|
38
|
+
body, records = extract_parts(text)
|
|
39
|
+
if not records:
|
|
40
|
+
return None
|
|
41
|
+
hash = body_hash(body)
|
|
42
|
+
return ReviewSource(body, records, hash, render_block(records, hash=hash))
|
|
43
|
+
return document_source(parse(text, str(path)))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _reference_source(document: AstDocument, target: Path, previous: Path | None):
|
|
47
|
+
"""Pick the review source by priority.
|
|
48
|
+
|
|
49
|
+
An explicit ``--previous`` wins (it may hold the authoritative annotations
|
|
50
|
+
even when the document carries its own, possibly stale, block). Otherwise
|
|
51
|
+
the document's own block is used, otherwise an already existing target.
|
|
52
|
+
"""
|
|
53
|
+
if previous is not None:
|
|
54
|
+
return read_source(previous)
|
|
55
|
+
own = document_source(document)
|
|
56
|
+
if own is not None:
|
|
57
|
+
return own
|
|
58
|
+
return read_source(target) if target.exists() else None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def review_block(document: AstDocument, target: Path, previous: Path | None, body: str):
|
|
62
|
+
"""Return the review block to append to *body*, or ``""`` when none applies."""
|
|
63
|
+
source = _reference_source(document, target, previous)
|
|
64
|
+
return "" if source is None else migrated_block(body, source)
|
transcript/tmd.py
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""Rebuild archives from captured outputs and write files atomically."""
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
import hashlib
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import tempfile
|
|
8
|
+
import textwrap
|
|
9
|
+
import tokenize
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
from .parse import (
|
|
14
|
+
REPORT_MARKER_COMMENT,
|
|
15
|
+
ReportBlock,
|
|
16
|
+
with_report_marker,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from ._rule.core import Rule
|
|
21
|
+
from .document import AstDocument
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def body_hash(text: str):
|
|
25
|
+
"""Hash the archive body a review block describes.
|
|
26
|
+
|
|
27
|
+
Trailing blank lines are normalized away, so the body written before a
|
|
28
|
+
block and the body read back from the archive hash the same. The block's
|
|
29
|
+
marker carries this value; a hand-edited body no longer matches it and the
|
|
30
|
+
block is dropped instead of being migrated from stale coordinates.
|
|
31
|
+
"""
|
|
32
|
+
normalized = text.rstrip("\n") + "\n"
|
|
33
|
+
digest = hashlib.sha256(normalized.encode("utf-8")).hexdigest()
|
|
34
|
+
return f"sha256:{digest}"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def escape_slot(content: str):
|
|
38
|
+
"""Escape *content* so it can be embedded in a ``\"\"\"...\"\"\"`` literal.
|
|
39
|
+
i.e., `'\"\"\"' + escape_slot(content) + '\"\"\"'` is a valid Python string.
|
|
40
|
+
|
|
41
|
+
- Backslashes, CR, TAB and non-printable controls are escaped.
|
|
42
|
+
- LF is preserved as a literal newline.
|
|
43
|
+
- Quotes are only escaped when they'd close / collide with the
|
|
44
|
+
surrounding triple quotes (runs of 3+, or trailing).
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
content = content.replace("\\", "\\\\")
|
|
48
|
+
content = content.replace("\r", "\\r").replace("\t", "\\t")
|
|
49
|
+
content = re.sub(
|
|
50
|
+
r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]",
|
|
51
|
+
lambda match: f"\\x{ord(match.group()):02x}",
|
|
52
|
+
content,
|
|
53
|
+
)
|
|
54
|
+
return re.sub(
|
|
55
|
+
r'(?:"{3,}|"+$)', lambda match: match.group().replace('"', '\\"'), content
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _slot_literal(output: str, prefix: str):
|
|
60
|
+
"""Render *output* as an indented triple-quoted literal source snippet.
|
|
61
|
+
|
|
62
|
+
The parsed-back value is ``"\\n" + prefix-each-line(output) [+ "\\n" + prefix]``,
|
|
63
|
+
so it does NOT round-trip to *output*.
|
|
64
|
+
"""
|
|
65
|
+
content = escape_slot(output)
|
|
66
|
+
inner = textwrap.indent(content, prefix, lambda line: line.rstrip("\r\n") != "")
|
|
67
|
+
if content.endswith("\n"):
|
|
68
|
+
return f'"""\n{inner}{prefix}"""'
|
|
69
|
+
return f'"""\n{inner}"""'
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _find_line_start(source: str, token: tokenize.TokenInfo):
|
|
73
|
+
lines = source.splitlines(keepends=True)
|
|
74
|
+
return sum(len(line) for line in lines[: token.end[0] - 1])
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _strip_stale_markers(document: AstDocument, blocks: list[ReportBlock]):
|
|
78
|
+
"""Remove ``# transcript.md`` from ``with`` regions that are not report blocks."""
|
|
79
|
+
|
|
80
|
+
confirmed = {id(block.node) for block in blocks}
|
|
81
|
+
for region in document.regions.values():
|
|
82
|
+
node = region.node
|
|
83
|
+
if isinstance(node, ast.If) or id(region.node) in confirmed:
|
|
84
|
+
continue
|
|
85
|
+
start, end, token, is_marker = with_report_marker(
|
|
86
|
+
document.source, node, document.position
|
|
87
|
+
)
|
|
88
|
+
if not is_marker:
|
|
89
|
+
continue
|
|
90
|
+
comment_line = start + _find_line_start(document.source[start:end], token)
|
|
91
|
+
end = len(token.line.rstrip("\r\n"))
|
|
92
|
+
yield (comment_line + token.end[1], comment_line + end, "")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def render(
|
|
96
|
+
self: AstDocument,
|
|
97
|
+
blocks: list[ReportBlock],
|
|
98
|
+
outputs: list[list[str]],
|
|
99
|
+
job: Rule | None = None,
|
|
100
|
+
):
|
|
101
|
+
"""Rebuild output slots and markers using the captured execution results.
|
|
102
|
+
|
|
103
|
+
Returns the archive *body* only. The trailing review block is not part of
|
|
104
|
+
rendering: the caller migrates and appends it explicitly (see
|
|
105
|
+
:func:`transcript.review._block.migrated_block`), so ``render`` never
|
|
106
|
+
silently carries ``self``'s review state into a new archive.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
edits: list[tuple[int, int, str]] = [*_strip_stale_markers(self, blocks)]
|
|
110
|
+
for block, output_block in zip(blocks, outputs):
|
|
111
|
+
if isinstance(block.node, ast.With):
|
|
112
|
+
start, end, token, is_marker = with_report_marker(
|
|
113
|
+
self.source, block.node, self.position
|
|
114
|
+
)
|
|
115
|
+
if not is_marker:
|
|
116
|
+
comment_line = start + _find_line_start(self.source[start:end], token)
|
|
117
|
+
colon_next = comment_line + token.end[1]
|
|
118
|
+
edits.append((colon_next, colon_next, " " + REPORT_MARKER_COMMENT))
|
|
119
|
+
for statement, output in zip(block.statements, output_block):
|
|
120
|
+
indent = self.indent_of(statement.start)
|
|
121
|
+
if statement.slot is not None:
|
|
122
|
+
literal = _slot_literal(output, indent) if output else ""
|
|
123
|
+
edits.append((*statement.slot.literal, literal))
|
|
124
|
+
elif output:
|
|
125
|
+
newline = self.source.find("\n", statement.end)
|
|
126
|
+
if newline == -1:
|
|
127
|
+
newline = len(self.source)
|
|
128
|
+
literal = "\n" + indent + _slot_literal(output, indent)
|
|
129
|
+
edits.append((newline, newline, literal))
|
|
130
|
+
if job is not None:
|
|
131
|
+
edits.extend(job.make_rule_block())
|
|
132
|
+
body = self.source_without_review
|
|
133
|
+
result = body
|
|
134
|
+
for start, end, replacement in sorted(edits, reverse=True):
|
|
135
|
+
result = result[:start] + replacement + result[end:]
|
|
136
|
+
result = result.rstrip("\n") + "\n"
|
|
137
|
+
ast.parse(result) # Never publish a non-Python archive.
|
|
138
|
+
return result
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def write(path: Path, source: str):
|
|
142
|
+
"""Write *source* to *path* via a temporary file and atomic replace."""
|
|
143
|
+
temporary = None
|
|
144
|
+
try:
|
|
145
|
+
with tempfile.NamedTemporaryFile(
|
|
146
|
+
mode="w",
|
|
147
|
+
encoding="utf-8",
|
|
148
|
+
newline="\n",
|
|
149
|
+
dir=path.parent,
|
|
150
|
+
prefix=f".{path.name}.",
|
|
151
|
+
suffix=".tmp",
|
|
152
|
+
delete=False,
|
|
153
|
+
) as stream:
|
|
154
|
+
temporary = Path(stream.name)
|
|
155
|
+
stream.write(source)
|
|
156
|
+
os.replace(temporary, path)
|
|
157
|
+
finally:
|
|
158
|
+
if temporary is not None:
|
|
159
|
+
temporary.unlink(missing_ok=True)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: transcript.md
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Executable Python documents with recorded outputs
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Provides-Extra: datavzrd
|
|
7
|
+
Requires-Dist: markdown-it-py<5,>=3; extra == 'datavzrd'
|
|
8
|
+
Requires-Dist: pyyaml<7,>=6; extra == 'datavzrd'
|
|
9
|
+
Provides-Extra: html
|
|
10
|
+
Requires-Dist: markdown-it-py<5,>=3; extra == 'html'
|
|
11
|
+
Provides-Extra: snakemake
|
|
12
|
+
Requires-Dist: snakemake<10,>=9; extra == 'snakemake'
|
|
13
|
+
Provides-Extra: vegalite
|
|
14
|
+
Requires-Dist: altair<7,>=5; extra == 'vegalite'
|
|
15
|
+
Requires-Dist: markdown-it-py<5,>=3; extra == 'vegalite'
|
|
16
|
+
Requires-Dist: pyyaml<7,>=6; extra == 'vegalite'
|
|
17
|
+
Requires-Dist: vl-convert-python<2,>=1; extra == 'vegalite'
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
transcript/__init__.py,sha256=tS5h0wWE7Oz6mELcY_DQJHtlAwFC3kAgcO4sHrwWz-A,805
|
|
2
|
+
transcript/cli.py,sha256=q1RIs9ecx_wmvt34YXDSL0PG1XnfleybijxSj7Pj4mw,12049
|
|
3
|
+
transcript/document.py,sha256=j2-QIGA2u4WSjwhIUSQEa6BuAKM1EYG2Rfcb6y4WRdw,1725
|
|
4
|
+
transcript/execute.py,sha256=PWEoUUBZVWvh0vh6g5tjUgaKgUqoJ_Pek3f0uNHDBFM,8514
|
|
5
|
+
transcript/output.py,sha256=rwLeUUtevJ-FhKIgBNIgvHlYEUNlDw8EFYKxhCzaRdc,10684
|
|
6
|
+
transcript/parse.py,sha256=BIipu3IqNdX2KPJ7ULHYAzSX6jwvYqDQL0kuZjEXCvg,8977
|
|
7
|
+
transcript/tmd.py,sha256=ox2oBS82nwtr8QHwYbuyodpBIbNk1hz82kiC7FuHmTw,5796
|
|
8
|
+
transcript/_rule/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
transcript/_rule/archive.py,sha256=C3hnADtQ_mU3y0jqdmiODpZHSJzD72sA2QO0vNK7RKg,4808
|
|
10
|
+
transcript/_rule/conda.py,sha256=NvCz5hNXrV4uTHfNVDpV2T7UXNj-cUfnipMTlB9lP6g,1751
|
|
11
|
+
transcript/_rule/container.py,sha256=lghE0_rH9_AqVd029WOqjzA5ooMiJHIYNL5zVdiTMwA,10356
|
|
12
|
+
transcript/_rule/core.py,sha256=TY8MKX3f8kpK1a50VO6r3QfOQI48P_kqgnUAc0ZPYG4,21939
|
|
13
|
+
transcript/_rule/lambdas.py,sha256=a4WEmOgtYA5ClWr3Yvi1hLSrKHSsE73qXBkzkoFQLZM,4526
|
|
14
|
+
transcript/_rule/registry.py,sha256=at5cn_Gj3aGatwY2q_RrDwnwPQ-cjGld-eHkTO1AM94,1618
|
|
15
|
+
transcript/_rule/serialize.py,sha256=nf_0lzFaxablnEinxizRJhFkoAuIVjwyLRIwuinBf3A,782
|
|
16
|
+
transcript/_rule/snakefile.py,sha256=U6V7GmlYbKZa-C2V5bBjW_quuKD6Nwz0mE-3hLpQ1J8,2758
|
|
17
|
+
transcript/_rule/wildcards.py,sha256=X4KAgvKKHl3h_Ot3PxwoPec1Yp3oFHxPCH1Ue5_q6zU,4182
|
|
18
|
+
transcript/html/__init__.py,sha256=pJBDZni3fbJQoEDCS62ISlgfYHQSXbmUwiFlKjT1eE0,774
|
|
19
|
+
transcript/html/_island.py,sha256=Yi1_fI4aZURiXtjF1NkmXK9Z34AIFuiVM8C0ANfn7yM,2611
|
|
20
|
+
transcript/html/_navigate.py,sha256=ol4u-DzlhT6XazuJ4EE2For-kaT3o-GomSVn_yqqp9I,7027
|
|
21
|
+
transcript/html/_output.py,sha256=ZqG0MLjq4fS-DaLhEl4UCF2RpACUhXUriktOIdeVEZk,3538
|
|
22
|
+
transcript/html/_resources.py,sha256=XW2m4cpEzSGsEuiQK7iLwqB2W5CXJs4YFrWi3OBw3_s,5960
|
|
23
|
+
transcript/html/_review.py,sha256=HV5WSGd0kBDkGryZDNBg2LJT3Bf6n4wIduDEAZFfHZU,1306
|
|
24
|
+
transcript/html/markup.py,sha256=8RuJjQU3RnBMgdd02RoA606FH9iW_u78Zt1BHRNOUNE,870
|
|
25
|
+
transcript/html/page.py,sha256=PXYcEm6U-UxKgIjF9Wbt_nP777abJIcrBZ5f2DI1MMg,4213
|
|
26
|
+
transcript/html/_assets/.frontend.sha256,sha256=JlmVctoq5qv1Kh9mslbQ_Za5iRCrI8_Xu_N0Pilpduo,64
|
|
27
|
+
transcript/html/_assets/__init__.py,sha256=FonXW7wSRkUv5nOK_yco_xPhFRdIbBmnpeMqKgkbMdc,638
|
|
28
|
+
transcript/html/_assets/style.min.css,sha256=RwdINRlbxsRVgnvh4mJ2nQaK4udWiAvVhMEfXqnJWAg,16017
|
|
29
|
+
transcript/html/_assets/transcript.min.js,sha256=iAKw4HIaKpd7L00OPvq-NdlVijmzr_FJeA0B1aYXjxo,52014
|
|
30
|
+
transcript/renderers/__init__.py,sha256=406XUydGH0IJvqIRF5xV_dNt9vbz-1dqUBPGYnqUyzw,4210
|
|
31
|
+
transcript/renderers/bitmap.py,sha256=dwPFjySMquNUB_d5RVMCbofMPg624QPYIeX8A7lDtqs,5649
|
|
32
|
+
transcript/renderers/compose.py,sha256=-Slh2OED271z8I3zmdtg5Oob_XpEOqfICOkzY7Gmbeo,7100
|
|
33
|
+
transcript/renderers/compose_runtime.js,sha256=a-9u-t81VlEqftr07rVemJZptldy5nCBMCTOw3uRwP4,8492
|
|
34
|
+
transcript/renderers/datavzrd.py,sha256=RzZ0I5x6eR_DogoIsygmZgEjjnxa3ZoKrYe6MOg1YSs,12397
|
|
35
|
+
transcript/renderers/datavzrd_runtime.js,sha256=POlVe0vi1e6BElbRjT2NTNtcwwQbMwiypNEBsXm2uAg,15948
|
|
36
|
+
transcript/renderers/label.py,sha256=swccRrnuzYAFyhvESbCI7JBHJp0QhjfmXsUmme_9fVE,7212
|
|
37
|
+
transcript/renderers/markdown.py,sha256=YBqhzEAH1ndgzmYO9CNHnCFcUemgwzELj-prJVpI5uI,1290
|
|
38
|
+
transcript/renderers/table.py,sha256=lrouh3M6oU2LUi1_U8C2WmEbFZhCJLAcx0lAPRbt0S4,1173
|
|
39
|
+
transcript/renderers/vegalite.py,sha256=ygpNjmJqj7AeptIKKKPNl3DCmkAEqGWNPWwRkJo3qTw,9084
|
|
40
|
+
transcript/review/__init__.py,sha256=IxvUFTisTfwZoCY8NgmKq5fhFmGx1gom3OjcfDf9ZQU,562
|
|
41
|
+
transcript/review/_block.py,sha256=SzY3_APcaAGgDMIlgAISgqED_iaE7L_eZJQhDU93czA,6271
|
|
42
|
+
transcript/review/_migrate.py,sha256=M7DEK_L6NzMiZOuuxJljwtpS1VPjjaO92XF1Mm_aT9Q,9157
|
|
43
|
+
transcript/review/_records.py,sha256=pZa0F-3nfsgTn7DzdPblsEqcbj5HGSXAujY3NeRWMWM,9155
|
|
44
|
+
transcript/review/_span.py,sha256=imK5v5Dc8MzZUvrz8CyTXU0zVuKpTPfInELyfysDMNs,9859
|
|
45
|
+
transcript/review/load.py,sha256=uVsMgewMeEkZN-id19ACc2nY_TQkyoATYsovorLSJbw,2369
|
|
46
|
+
transcript_md-0.1.0.dist-info/METADATA,sha256=0dIJ60nuWMurlVL6IhA3fXjjmKyLjHWIp68AUFPYdDo,665
|
|
47
|
+
transcript_md-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
48
|
+
transcript_md-0.1.0.dist-info/entry_points.txt,sha256=3iObRAHD4Jz5fr22zvB73l61gkRP33c1-3ZZ8IkT_D8,51
|
|
49
|
+
transcript_md-0.1.0.dist-info/RECORD,,
|