|
| 1 | +# API reference |
| 2 | + |
| 3 | +This page lists the public symbols exported from the `rebulk` package (its `__all__`). |
| 4 | + |
| 5 | +```python |
| 6 | +from rebulk import ( |
| 7 | + Rebulk, Key, Rule, CustomRule, |
| 8 | + AppendMatch, RemoveMatch, RenameMatch, AppendTags, RemoveTags, |
| 9 | + ConflictSolver, PrivateRemover, |
| 10 | + PRE_PROCESS, POST_PROCESS, REGEX_ENABLED, |
| 11 | +) |
| 12 | +``` |
| 13 | + |
| 14 | +## Rebulk |
| 15 | + |
| 16 | +`class Rebulk(...)` |
| 17 | + |
| 18 | +Main entry point. A `Builder` that holds patterns, rules and nested Rebulk objects, and |
| 19 | +runs the matching pipeline. Key methods: |
| 20 | + |
| 21 | +- `string(*patterns, key=None, **options)` — add one or more string patterns. |
| 22 | +- `regex(*patterns, key=None, **options)` — add one or more regex patterns. |
| 23 | +- `functional(*patterns, key=None, **options)` — add one or more functional patterns. |
| 24 | +- `chain(**options) -> ChainBuilder` — start a chain of patterns (closed with `close()`). |
| 25 | +- `pattern(*patterns)` — add already-built `Pattern` objects. |
| 26 | +- `rules(*rules)` — register rules from a class, instance or module. |
| 27 | +- `rebulk(*rebulks)` — nest other `Rebulk` objects to compose their patterns and rules. |
| 28 | +- `defaults(**kwargs)` / `string_defaults` / `regex_defaults` / `functional_defaults` / |
| 29 | + `chain_defaults` — set default options for subsequently built patterns. |
| 30 | +- `declare_keys(*keys)` — declare typed `Key` objects whose converters are inherited by |
| 31 | + later patterns as per-name formatters. |
| 32 | +- `matches(string, context=None) -> Matches` — run the pipeline and return the results. |
| 33 | +- `check_keys(*, allowed_unused=()) -> list[str]` — declared key names no pattern can |
| 34 | + produce (guards against typos / stale names). |
| 35 | +- `effective_patterns(context=None)`, `effective_rules(context=None)`, |
| 36 | + `effective_keys(context=None)` — the patterns / rules / keys active for a given context. |
| 37 | + |
| 38 | +## Key |
| 39 | + |
| 40 | +`class Key(name, value_type, formatter=None)` |
| 41 | + |
| 42 | +A frozen, generic dataclass binding a match `name` to its `value_type` for type-safe |
| 43 | +retrieval. `formatter`, if given, is the `(str) -> T` converter applied to the matched |
| 44 | +substring; it defaults to `value_type` itself. Passing a key to a builder method (`key=`) |
| 45 | +wires up both the match name and the formatter, so `matches[key]` and `matches.all(key)` |
| 46 | +return precise types instead of `Any`. `value_type` must be scalar — a dataclass or |
| 47 | +`TypedDict` is rejected (assemble those with `Matches.to(...)`). The `converter` property |
| 48 | +returns the effective `(str) -> T` converter. |
| 49 | + |
| 50 | +## Rule / CustomRule |
| 51 | + |
| 52 | +`class Rule` — abstract base for a rule. Implement `when(matches, context)` (return truthy |
| 53 | +to trigger) and either `then(matches, when_response, context)` or a `consequence` class |
| 54 | +attribute. Class attributes `priority` (int, higher first) and `dependency` (another rule |
| 55 | +class) control ordering. `CustomRule` is the generic, typed rule base re-exported for |
| 56 | +building custom rules. |
| 57 | + |
| 58 | +## Consequences |
| 59 | + |
| 60 | +Consequence classes applied when a rule triggers (used as a rule's `consequence`): |
| 61 | + |
| 62 | +- `RemoveMatch` — remove the matched objects from the result. |
| 63 | +- `AppendMatch` — append new matches to the result. |
| 64 | +- `RenameMatch` — rename the matched objects. |
| 65 | +- `AppendTags` — add tags to the matched objects. |
| 66 | +- `RemoveTags` — remove tags from the matched objects. |
| 67 | + |
| 68 | +## Default processors |
| 69 | + |
| 70 | +- `ConflictSolver` — default `Rule` (priority `PRE_PROCESS`) that keeps the longer match |
| 71 | + when matches overlap. |
| 72 | +- `PrivateRemover` — default `Rule` (priority `POST_PROCESS`) that removes matches flagged |
| 73 | + as `private` from the final output. |
| 74 | + |
| 75 | +## Constants |
| 76 | + |
| 77 | +- `PRE_PROCESS` (`2048`) — priority for rules running before the standard rules. |
| 78 | +- `POST_PROCESS` (`-2048`) — priority for rules running after the standard rules. |
| 79 | +- `REGEX_ENABLED` — `True` when the optional [`regex`](https://pypi.python.org/pypi/regex) |
| 80 | + backend is active (`REBULK_REGEX_ENABLED=1` and `regex` importable), otherwise `False`. |
| 81 | + |
| 82 | +## Other useful modules |
| 83 | + |
| 84 | +These are not in `__all__` but are part of the public surface: |
| 85 | + |
| 86 | +- `rebulk.match` — the `Match` and `Matches` classes and the `MatchesDict` mapping. |
| 87 | +- `rebulk.validators` — reusable validator functions (typically wired with |
| 88 | + `functools.partial`), e.g. surrounding-character checks. |
| 89 | +- `rebulk.debug` — debug switches such as `CHECK_DECLARED_KEYS` (declared-key value-type |
| 90 | + contract check, off by default). |
| 91 | +- `rebulk.introspector` — utilities to extract pattern / rule metadata from a configured |
| 92 | + `Rebulk`. |
0 commit comments