|
| 1 | +"""Builds up a draft by commands, and rebuilds it from the ones it recorded. |
| 2 | +
|
| 3 | +A draft is written by a sequence of commands, some of them chosen by a model. Every |
| 4 | +command that changes one is recorded in the draft's ``log`` as it is applied, and every |
| 5 | +field a command writes carries where it came from, so that :func:`replay` can build the |
| 6 | +same draft again out of the frozen markdown and the log alone, with no model in the loop. |
| 7 | +That is what makes a run reproducible, and a saved run a test. |
| 8 | +
|
| 9 | +Commands reach a draft only through :func:`apply`, which is what keeps the log complete: |
| 10 | +a handler registered with :func:`command` is never called by anything else. |
| 11 | +""" |
| 12 | + |
| 13 | +from collections.abc import Callable # Rather than typing's, which beartype warns on. |
| 14 | +from pathlib import Path |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +from in2lambda.source import ( |
| 18 | + DRAFT, |
| 19 | + SourceError, |
| 20 | + _require_conversion_tools, |
| 21 | + blocks, |
| 22 | + frozen, |
| 23 | + save, |
| 24 | + serialise, |
| 25 | +) |
| 26 | + |
| 27 | +Command = dict[str, Any] |
| 28 | +"""One entry of the log: ``{"command": name, "args": {...}, "by": who}``.""" |
| 29 | + |
| 30 | +Handler = Callable[[dict[str, Any], str, dict[str, Any], str], None] |
| 31 | +"""What a command does: `handler(draft, markdown, args, by)`, changing the draft. |
| 32 | +
|
| 33 | +The frozen markdown is passed in rather than read, so that a handler quoting the source |
| 34 | +by line range quotes the same text on a replay as it did when it first ran. |
| 35 | +""" |
| 36 | + |
| 37 | +_HANDLERS: dict[str, Handler] = {} |
| 38 | +"""Every command there is, by the name a log entry names it with.""" |
| 39 | + |
| 40 | + |
| 41 | +class MalformedCommand(SourceError): |
| 42 | + """A log holds something that is not a command, so nothing can be made of it.""" |
| 43 | + |
| 44 | + |
| 45 | +class UnknownCommand(SourceError): |
| 46 | + """A log names a command that nothing registered, so the draft cannot be rebuilt.""" |
| 47 | + |
| 48 | + |
| 49 | +class NoSuchBlock(SourceError): |
| 50 | + """A command names a block the frozen source has not got.""" |
| 51 | + |
| 52 | + |
| 53 | +class ReplayDiffers(SourceError): |
| 54 | + """Replaying a draft's log does not reproduce the draft.""" |
| 55 | + |
| 56 | + |
| 57 | +def command(name: str) -> Callable[[Handler], Handler]: |
| 58 | + """Registers a handler as the command of that name. |
| 59 | +
|
| 60 | + Args: |
| 61 | + name: What a log entry calls it, as it is typed: ``"mark ignore"``. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + The decorator, which returns the handler unchanged. |
| 65 | + """ |
| 66 | + |
| 67 | + def register(handler: Handler) -> Handler: |
| 68 | + _HANDLERS[name] = handler |
| 69 | + return handler |
| 70 | + |
| 71 | + return register |
| 72 | + |
| 73 | + |
| 74 | +def record( |
| 75 | + draft: dict[str, Any], |
| 76 | + key: str, |
| 77 | + value: Any, |
| 78 | + *, |
| 79 | + layer: int, |
| 80 | + ranges: list[list[int]], |
| 81 | + by: str, |
| 82 | +) -> None: |
| 83 | + """Writes one field of a draft, with where it came from. |
| 84 | +
|
| 85 | + Args: |
| 86 | + draft: The draft to write into. |
| 87 | + key: What the field is called, unique within the draft. |
| 88 | + value: What it is. |
| 89 | + layer: What wrote it: 1 a spec, 2 a predicate, 3 a range taken from the source, |
| 90 | + 4 a literal someone typed. A reader deciding whether to trust a field wants |
| 91 | + to know which of those it was. |
| 92 | + ranges: The line ranges of the frozen source the value was copied from, as |
| 93 | + ``[[start, end], ...]``, and empty where it was not copied from any. |
| 94 | + by: Who ran the command, as a name or a model. |
| 95 | + """ |
| 96 | + draft["fields"][key] = { |
| 97 | + "value": value, |
| 98 | + "layer": layer, |
| 99 | + "ranges": ranges, |
| 100 | + # A field is edited when something replaces the value a command wrote, which is |
| 101 | + # not something a command can do to its own field on the way in. |
| 102 | + "edited": False, |
| 103 | + "by": by, |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +def _fault(entry: Any) -> str: |
| 108 | + """What is wrong with the shape of a log entry, or "" if nothing is.""" |
| 109 | + if not isinstance(entry, dict): |
| 110 | + return "is not an object" |
| 111 | + if missing := sorted({"command", "args", "by"} - entry.keys()): |
| 112 | + return f"has no {' or '.join(missing)}" |
| 113 | + if not isinstance(entry["command"], str): |
| 114 | + return f"gives {entry['command']!r} as its command, which is not a name" |
| 115 | + if not isinstance(entry["args"], dict): |
| 116 | + return f"gives {entry['args']!r} as its args, which is not an object" |
| 117 | + return "" |
| 118 | + |
| 119 | + |
| 120 | +def _argument(args: dict[str, Any], name: str, command: str) -> Any: |
| 121 | + """One argument of a command, given that the log entry gave it. |
| 122 | +
|
| 123 | + Handlers take their arguments through this rather than indexing, so that a log |
| 124 | + entry missing one says which one rather than raising a KeyError at whoever ran it. |
| 125 | +
|
| 126 | + Raises: |
| 127 | + MalformedCommand: the entry has no argument of that name. |
| 128 | + """ |
| 129 | + if name not in args: |
| 130 | + raise MalformedCommand( |
| 131 | + f"{args!r} in the log is not a command {command} can run: it has no " |
| 132 | + f'"{name}" argument.' |
| 133 | + ) |
| 134 | + return args[name] |
| 135 | + |
| 136 | + |
| 137 | +def apply(draft: dict[str, Any], markdown: str, entry: Any) -> None: |
| 138 | + """Runs one command against a draft and records it in the draft's log. |
| 139 | +
|
| 140 | + Args: |
| 141 | + draft: The draft to change, in place. |
| 142 | + markdown: The frozen markdown the draft was written from. |
| 143 | + entry: The command, as it is written in the log. Anything at all, rather than a |
| 144 | + `Command`, because a log is read from a file anyone can edit: what shape it |
| 145 | + has is something to tell the reader about, not something to assume. |
| 146 | +
|
| 147 | + Raises: |
| 148 | + MalformedCommand: the entry is not a command. |
| 149 | + UnknownCommand: nothing is registered under that name. |
| 150 | + """ |
| 151 | + if fault := _fault(entry): |
| 152 | + raise MalformedCommand( |
| 153 | + f"{entry!r} in the log is not a command: it {fault}. A command is an " |
| 154 | + 'object with a "command" naming it, its "args", and who it was run "by".' |
| 155 | + ) |
| 156 | + if (handler := _HANDLERS.get(entry["command"])) is None: |
| 157 | + raise UnknownCommand( |
| 158 | + f"{entry['command']} is not a command this version of in2lambda has, so " |
| 159 | + "the draft cannot be built from its log. It was written by a newer one." |
| 160 | + ) |
| 161 | + handler(draft, markdown, entry["args"], entry["by"]) |
| 162 | + # After the handler, so a command that was refused is not recorded as having run. |
| 163 | + draft["log"].append(entry) |
| 164 | + |
| 165 | + |
| 166 | +def execute(entry: Command, directory: str = ".") -> None: |
| 167 | + """Runs one command against the draft in a directory and writes it back. |
| 168 | +
|
| 169 | + Args: |
| 170 | + entry: The command, as it is written in the log. |
| 171 | + directory: Where the ``draft.json`` to change is. |
| 172 | +
|
| 173 | + Raises: |
| 174 | + SourceError: the draft is missing, is not one of ours, or was written from |
| 175 | + markdown that has changed since; or the command is unknown or refused. |
| 176 | + """ |
| 177 | + draft, markdown = frozen(directory) |
| 178 | + apply(draft, markdown, entry) |
| 179 | + save(Path(directory) / DRAFT, draft) |
| 180 | + |
| 181 | + |
| 182 | +def replay(directory: str = ".") -> None: |
| 183 | + """Rebuilds the draft in a directory from its source and its log, and checks it. |
| 184 | +
|
| 185 | + Nothing is written: the point is to find out whether what is on disk is what its |
| 186 | + commands say it should be, and a replay that wrote the answer could not tell anyone |
| 187 | + it was different. |
| 188 | +
|
| 189 | + Args: |
| 190 | + directory: Where the ``draft.json`` to replay is. |
| 191 | +
|
| 192 | + Raises: |
| 193 | + DraftExists: the markdown has changed since the draft was written from it, so |
| 194 | + the commands would be replayed against lines they were not run against. |
| 195 | + MalformedCommand: the log holds something that is not a command. |
| 196 | + UnknownCommand: the log names a command nothing here registered. |
| 197 | + ReplayDiffers: the rebuilt draft is not the one on disk, byte for byte. |
| 198 | + """ |
| 199 | + _require_conversion_tools() |
| 200 | + draft, markdown = frozen(directory) |
| 201 | + # From the markdown rather than from the draft: the blocks are as much a product of |
| 202 | + # the source as the fields are, and copying them across would not check them. |
| 203 | + rebuilt: dict[str, Any] = { |
| 204 | + "source": draft["source"], |
| 205 | + "hash": draft["hash"], |
| 206 | + "blocks": [block.to_dict() for block in blocks(markdown)], |
| 207 | + "log": [], |
| 208 | + "fields": {}, |
| 209 | + } |
| 210 | + for entry in draft["log"]: |
| 211 | + apply(rebuilt, markdown, entry) |
| 212 | + |
| 213 | + path = Path(directory) / DRAFT |
| 214 | + if serialise(rebuilt) != path.read_bytes(): |
| 215 | + raise ReplayDiffers( |
| 216 | + f"Replaying the log in {DRAFT} does not reproduce it, so what is in it did " |
| 217 | + "not all come from the commands it records - something has changed it since " |
| 218 | + "they ran. Run in2lambda source add --start-over to begin again." |
| 219 | + ) |
| 220 | + |
| 221 | + |
| 222 | +@command("mark ignore") |
| 223 | +def _mark_ignore( |
| 224 | + draft: dict[str, Any], markdown: str, args: dict[str, Any], by: str |
| 225 | +) -> None: |
| 226 | + """Marks one block of the frozen source as nothing to take a question from.""" |
| 227 | + block = _argument(args, "block", "mark ignore") |
| 228 | + if (found := next((b for b in draft["blocks"] if b["id"] == block), None)) is None: |
| 229 | + raise NoSuchBlock( |
| 230 | + f"There is no block {block} in {DRAFT}. Run in2lambda source show to see " |
| 231 | + "the ids of the blocks there are." |
| 232 | + ) |
| 233 | + record( |
| 234 | + draft, |
| 235 | + f"{block}.ignore", |
| 236 | + True, |
| 237 | + layer=3, |
| 238 | + ranges=[[found["start"], found["end"]]], |
| 239 | + by=by, |
| 240 | + ) |
0 commit comments