Scope and design
What it is for
An application that sells or grants limited use of its features checks the same things in many places: whether a subject may use a feature, whether it can afford it, which budget pays, and what the use cost. Without a shared component each feature gets its own counter, its own reset job and its own idea of a refund.
allowances puts those checks behind one call. peek answers whether an operation is covered, fulfill charges it and writes a ledger entry, unfulfill reverses it. Grants, consumable budgets, conversion between currencies, sliding-window quotas, overdraft and inherited group rights are resolved by the same engine, and every charge is recorded on one ledger.
What it does not do
- Payments. The library has no invoices, card charges or payment-provider integration. Units are abstract; mapping a cost to a price or a payment provider belongs to the application.
- Feature flags. A flag changes behaviour for every subject at once; the library holds per-subject state.
- Object-level authorisation. It has no per-object permissions and no deny rules.
- User interface. It has no UI for granting rights, funding wallets or showing balances.
grant_permission,credit,plan_switchandadd_memberare the API for changing state.
The consuming application provides:
- the feature and currency definitions, as a settings dict or through its own
DefinitionProvider; - the decision when to grant, fund, switch or revoke, and the calls that do it;
- the UI that shows wallets, balances and the ledger to its users;
- the mapping of costs to real prices and payment providers.
Design decisions
Code comments state each rule where it applies; this section lists the main ones.
- Dependency-free core with a storage port. The engine imports only the standard library and reaches storage through the
Storeinterface (allowances/store.py). Django and adorm are backends; each defines its own archive format behindStore.archive_wallet. - One distribution with optional extras. The
allowancesdistribution shipsallowances(no dependencies),allowances_django(extra[django]) andallowances_adorm(extra[adorm]), released together under one version. - Async-first, with a driven synchronous API. Engine functions and store methods are coroutines;
allowances.syncruns them to completion without an event loop on a store that never suspends. A separate synchronous engine is not provided: it duplicates the engine or adds a thread hop to every call. - Definitions in configuration, state in the store. Features and currencies are names read from a provider; wallets and transactions are rows. A wallet stores its currency as a name, not a foreign key.
- Append-only ledger with storno. A reversal is a new storno entry; no ledger entry is edited.
- Sliding windows without reset jobs. A windowed balance is the allotment plus the ledger entries inside the current window.
- One global lock order. Every operation locks all the wallets it uses in one call, in ascending id order.
- Membership expansion. A subject draws on the wallets of every group it belongs to, after its own; cycles are rejected on write.
- Permissions as rate-0 grants. A right is a grant wallet at conversion rate 0; each use is a zero-amount ledger entry, and Django permissions are
perm:features held this way.