srdatalog.viz.patch

Patch a rule’s .with_plan(...) kwargs in a source file.

Strategy: use find_rule_locations to get the byte-offset span of each with_plan(var_order=VALUE, clause_order=VALUE) kwarg value, then slice

  • reassemble the source. This preserves surrounding formatting — the user’s quote style, line breaks, comments outside the kwarg VALUE all stay intact.

For kwargs we want to introduce that don’t exist yet (e.g. the rule has .with_plan(var_order=[...]) and we want to add clause_order=[...] too), we append them inside the existing .with_plan(...) call’s paren range. For rules that have no .with_plan(...) at all, we append one after the .named(...) call.

Not a general refactoring tool — intended specifically for the viz extension’s “drag reorder → write back” loop.

Module Contents

Functions

patch_rule_plan

Return source with rule rule_name’s plan updated.

API

exception srdatalog.viz.patch.PlanPatchError[source]

Bases: ValueError

Raised when the source doesn’t contain the target rule, or the rule has no .named(...) call to patch against.

Initialization

Initialize self. See help(type(self)) for accurate signature.

srdatalog.viz.patch.patch_rule_plan(source: str, rule_name: str, *, var_order: list[str] | None = None, clause_order: list[int] | None = None, delta: int = -1) str[source]

Return source with rule rule_name’s plan updated.

Args: source: full source text. rule_name: the string inside .named("..."). var_order: new variable order. None = leave unchanged. clause_order: new clause index order. None = leave unchanged. delta: which PlanEntry to target (when multiple .with_plan chained). -1 = the first one / append a new one. Non-negative values match an existing entry with delta=N, if present.

At least one of var_order / clause_order must be non-None.