Skip to main content

beancount-hooks

Rule-based beangulp import hooks for Beancount v3. A Ruleset asserts what a transaction is; predictors then fill only the blanks from the existing ledger. No ML, no runtime dependency beyond Beancount.

Ruleset.hook                 # facts — first match wins
RulesPayeePredictor.hook     # fill an empty payee from the narration
RulesPostingsPredictor.hook  # fill the other leg(s) from history
RulesTagsPredictor.hook      # add tags the payee usually carries

That order is the contract. Reverse it and a guess lands before the rule that should have won. See ADR-001.

Install

Requires Python 3.11+ and Beancount 3. After the first release (v0.6.1):

uv add beancount-hooks

Until that tag is on PyPI, install from a checkout:

uv add --editable /path/to/beancount-hooks

Use

from decimal import Decimal as D

from beancount.core.amount import Amount

from beancount_hooks import (
    Actions,
    Match,
    Rule,
    Ruleset,
    RulesPayeePredictor,
    RulesPostingsPredictor,
    RulesTagsPredictor,
)

RULES = [
    Rule(
        Match(payee='Coop'),
        Actions(post='Expenses:Groceries'),
        name='coop',
    ),
    Rule(
        Match(payee='Landlord', sign='debit'),
        Actions(
            post=(
                ('Expenses:Housing:Utilities', Amount(D('150.00'), 'CHF')),
                'Expenses:Housing:Rent',
            ),
            tags=('recurring',),
        ),
        name='rent',
    ),
]

# Limit the ruleset to the importers it was written for.  A card that
# already assigns its own accounts is left alone.
LEDGER_RULES = Ruleset(RULES, label='LedgerRules', accounts=('Assets:Bank',))

HOOKS = [
    LEDGER_RULES.hook,
    RulesPayeePredictor().hook,
    RulesPostingsPredictor().hook,
    RulesTagsPredictor().hook,
]

Pass HOOKS to beangulp.Ingest(CONFIG, HOOKS). The same list is what Fava ingest runs.

Match amounts are absolute; direction is sign='debit' or 'credit'. amount=D("10.00") without a sign matches both the charge and the refund — add sign if you mean only one. See ADR-003.

Ruleset.shadowed() returns (earlier, unreachable) pairs for rules that can never fire. Ruleset.explain(txn, account) lists every match in precedence order.

Fava

Fava sends the beangulp 4-tuple only to hooks whose annotations contain the literal text Importer. Every hook in this package does. If you wrap hook, keep that word in the signature or Fava will call you with (filename, entries), the importer account will be missing, and map_transactions will raise. See ADR-002.

Documentation

Doc What it is
docs/PRD.md Scope, users, non-goals
docs/SAD.md Components, data flow, the posting ladder
docs/glossary.md Shared vocabulary
docs/adr/ Binding decisions

Develop

make install
uv run pre-commit install
make check
make test

make install syncs the lockfile into .venv. pre-commit formats staged files; pre-push runs make check (the same gate CI uses).

make format   # rewrite the tree
make build    # sdist + wheel, no local path sources
make audit    # zizmor over the workflows

Release

uv version --bump patch   # or minor / major
git add pyproject.toml uv.lock
git commit -m "Release 0.6.2"
git tag v0.6.2
git push origin main --tags

The tag must equal v plus the version in pyproject.toml. A mismatch fails before any PyPI contact. That tag push is the only publish trigger.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

beancount_hooks-0.6.1.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beancount_hooks-0.6.1-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file beancount_hooks-0.6.1.tar.gz.

File metadata

  • Download URL: beancount_hooks-0.6.1.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for beancount_hooks-0.6.1.tar.gz
Algorithm Hash digest
SHA256 a72b572ba0fced7567b9708085db8a8abf171e7db7ca3bc9ee106a87ece9e1b8
MD5 fd6887420a1027f4f4d9cc0f65ff0646
BLAKE2b-256 5e5757d6c31342f2dc38333be911a0a33cbf870c4cd40baa4ba100634824412f

See more details on using hashes here.

Provenance

The following attestation bundles were made for beancount_hooks-0.6.1.tar.gz:

Publisher: release.yml on mekanics/beancount-hooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file beancount_hooks-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: beancount_hooks-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for beancount_hooks-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1c36bb7b52b556ca6d1cd69ff908503b5d2f865e59c4c6c7d6e39b9cac148a0
MD5 7f2f362798f4a057cc4444f277d4805d
BLAKE2b-256 63e1f9ded1c42c29a2c0bfa6737f623a5217a9e8068fdde762f9b80b0d0cdc8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for beancount_hooks-0.6.1-py3-none-any.whl:

Publisher: release.yml on mekanics/beancount-hooks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.7.0

2 files

This release

0.6.1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page