Drop counterparty, the generic parsers and .money/

counterparty was a structured field only nlb could fill honestly. revolut
and traderepublic invented one by running an IBAN-shaped regex over the
description they had just built, and the two spellings disagreed --
SI56 1234 5678 9012 345 against SI56123456789012345 -- so a literal rule
pattern that worked on one account silently matched nothing on another. It
is gone from the model, the index, the rule keys, ls --wide and the rules
screen. nlb now appends its IBAN column to the end of the description,
where the other two already keep theirs, so match = "*SI56*" works
everywhere. That changes those descriptions and with them their
fingerprints, so a statement overlapping an already-imported period will
re-add rather than dedupe those rows until the index is rebuilt. An index
built by an older binary drops the column when it is opened.

The index itself moves from .money/index.db up to index.db beside
rules.toml. Nothing looks in the old location, so an existing one has to be
moved by hand -- otherwise the tool quietly starts a fresh index and the
manual tags in the old file, the only thing statements cannot reproduce,
stay behind in it.

The csv and cmd parsers are gone along with the [csv] and [cmd] config they
carried. cmd shelled out to the Python extractors, which were ported to Go
and deleted, so it bridged to nothing; csv was a generic column-mapped
fallback that no account used, and between them they were the largest
configuration surface in the tool. A bank is now described in Go, where it
can be tested. The importer tests register their own three-column parser
rather than borrow a bank's, so they stay about the directory walk, dedupe
and per-file error reporting.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-11 22:51:20 +02:00
co-authored by Claude Opus 5
parent 2a2c0c1db9
commit 16c2585637
23 changed files with 182 additions and 627 deletions
-124
View File
@@ -1,124 +0,0 @@
package parser
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"os/exec"
"strings"
"time"
"git.petrovv.com/nikola/money/internal/config"
)
func init() {
Register("cmd", newCmdParser)
}
// FileToken is replaced with the statement's absolute path in a cmd parser's argv.
const FileToken = "{{file}}"
// cmdRunTimeout bounds an extractor run so a hung script cannot wedge an import.
const cmdRunTimeout = 2 * time.Minute
// cmdParser runs an external extractor and reads normalised CSV from its
// stdout: date,description,amount with any further columns ignored. This is
// the bridge that lets the existing Python extractors be used unchanged.
type cmdParser struct {
cfg config.CmdConfig
digits int
}
func newCmdParser(acc *config.Account) (Parser, error) {
if acc.Cmd == nil || len(acc.Cmd.Argv) == 0 {
return nil, fmt.Errorf("account %s: parser \"cmd\" requires [cmd] with a non-empty argv", acc.Slug)
}
cfg := *acc.Cmd
if cfg.Layout == "" {
cfg.Layout = "2006-01-02"
}
if !hasFileToken(cfg.Argv) {
return nil, fmt.Errorf("account %s: [cmd] argv must contain %s so the script knows which file to read",
acc.Slug, FileToken)
}
return &cmdParser{cfg: cfg, digits: acc.Digits()}, nil
}
func hasFileToken(argv []string) bool {
for _, a := range argv {
if strings.Contains(a, FileToken) {
return true
}
}
return false
}
func (p *cmdParser) Parse(path string, acc *config.Account) ([]RawTxn, error) {
argv := make([]string, len(p.cfg.Argv))
for i, a := range p.cfg.Argv {
argv[i] = strings.ReplaceAll(a, FileToken, path)
}
ctx, cancel := context.WithTimeout(context.Background(), cmdRunTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
cmd.Dir = acc.Dir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
msg := strings.TrimSpace(stderr.String())
if ctx.Err() == context.DeadlineExceeded {
return nil, fmt.Errorf("extractor %v timed out after %s", argv, cmdRunTimeout)
}
if msg != "" {
return nil, fmt.Errorf("extractor %v failed: %w: %s", argv, err, msg)
}
return nil, fmt.Errorf("extractor %v failed: %w", argv, err)
}
return p.parseOutput(&stdout, argv)
}
func (p *cmdParser) parseOutput(out io.Reader, argv []string) ([]RawTxn, error) {
r := csv.NewReader(out)
r.FieldsPerRecord = -1
r.LazyQuotes = true
var txns []RawTxn
for row := 0; ; row++ {
rec, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("extractor %v: output row %d: %w", argv, row+1, err)
}
if row < p.cfg.SkipRows || isBlank(rec) {
continue
}
if len(rec) < 3 {
return nil, fmt.Errorf("extractor %v: output row %d has %d columns, want date,description,amount",
argv, row+1, len(rec))
}
d, err := time.Parse(p.cfg.Layout, strings.TrimSpace(rec[0]))
if err != nil {
return nil, fmt.Errorf("extractor %v: output row %d: date %q does not match layout %q",
argv, row+1, rec[0], p.cfg.Layout)
}
amount, err := ParseAmount(rec[2], ".", "", p.digits)
if err != nil {
return nil, fmt.Errorf("extractor %v: output row %d: %w", argv, row+1, err)
}
txns = append(txns, RawTxn{
Date: d.Format("2006-01-02"),
Description: strings.TrimSpace(rec[1]),
AmountMinor: amount,
})
}
return txns, nil
}
-175
View File
@@ -1,175 +0,0 @@
package parser
import (
"encoding/csv"
"fmt"
"io"
"os"
"strings"
"time"
"git.petrovv.com/nikola/money/internal/config"
)
func init() {
Register("csv", newCSVParser)
}
// csvParser reads a delimited statement using column positions from
// account.toml. Amounts come either from one signed column, or from a
// debit/credit pair.
type csvParser struct {
cfg config.CSVConfig
digits int
}
func newCSVParser(acc *config.Account) (Parser, error) {
if acc.CSV == nil {
return nil, fmt.Errorf("account %s: parser \"csv\" requires a [csv] section", acc.Slug)
}
cfg := *acc.CSV
if cfg.Amount == nil && cfg.Debit == nil && cfg.Credit == nil {
return nil, fmt.Errorf("account %s: [csv] needs either amount or debit/credit columns", acc.Slug)
}
if cfg.Amount != nil && (cfg.Debit != nil || cfg.Credit != nil) {
return nil, fmt.Errorf("account %s: [csv] sets both amount and debit/credit; pick one", acc.Slug)
}
if cfg.Date.Layout == "" {
return nil, fmt.Errorf("account %s: [csv] date needs a layout, e.g. layout = \"02.01.2006\"", acc.Slug)
}
return &csvParser{cfg: cfg, digits: acc.Digits()}, nil
}
func (p *csvParser) Parse(path string, acc *config.Account) ([]RawTxn, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
r := csv.NewReader(f)
r.FieldsPerRecord = -1 // statements are ragged more often than not
r.LazyQuotes = true
if d := p.cfg.Delimiter; d != "" {
runes := []rune(d)
if len(runes) != 1 {
return nil, fmt.Errorf("delimiter %q must be a single character", d)
}
r.Comma = runes[0]
}
var out []RawTxn
for row := 0; ; row++ {
rec, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("%s: row %d: %w", path, row+1, err)
}
if row < p.cfg.SkipRows {
continue
}
if isBlank(rec) {
continue
}
txn, err := p.row(rec)
if err != nil {
return nil, fmt.Errorf("%s: row %d: %w", path, row+1, err)
}
out = append(out, txn)
}
return out, nil
}
func (p *csvParser) row(rec []string) (RawTxn, error) {
var t RawTxn
raw, err := field(rec, p.cfg.Date.Col)
if err != nil {
return t, fmt.Errorf("date column: %w", err)
}
d, err := time.Parse(p.cfg.Date.Layout, strings.TrimSpace(raw))
if err != nil {
return t, fmt.Errorf("date %q does not match layout %q", raw, p.cfg.Date.Layout)
}
t.Date = d.Format("2006-01-02")
desc, err := field(rec, p.cfg.Description.Col)
if err != nil {
return t, fmt.Errorf("description column: %w", err)
}
t.Description = strings.TrimSpace(desc)
switch {
case p.cfg.Amount != nil:
raw, err := field(rec, p.cfg.Amount.Col)
if err != nil {
return t, fmt.Errorf("amount column: %w", err)
}
v, err := ParseAmount(raw, p.cfg.Amount.Decimal, p.cfg.Amount.Thousands, p.digits)
if err != nil {
return t, err
}
t.AmountMinor = v
default:
// Debit/credit pair: exactly one of the two carries a value, and both
// are written as positive numbers.
debit, err := p.optional(rec, p.cfg.Debit)
if err != nil {
return t, fmt.Errorf("debit column: %w", err)
}
credit, err := p.optional(rec, p.cfg.Credit)
if err != nil {
return t, fmt.Errorf("credit column: %w", err)
}
if debit != 0 && credit != 0 {
return t, fmt.Errorf("both debit (%d) and credit (%d) are set", debit, credit)
}
t.AmountMinor = credit - abs(debit)
}
if p.cfg.Invert {
t.AmountMinor = -t.AmountMinor
}
return t, nil
}
// optional parses a column that may legitimately be blank, as debit/credit
// columns always are for half the rows.
func (p *csvParser) optional(rec []string, c *config.Column) (int64, error) {
if c == nil {
return 0, nil
}
raw, err := field(rec, c.Col)
if err != nil {
return 0, err
}
if strings.TrimSpace(raw) == "" {
return 0, nil
}
return ParseAmount(raw, c.Decimal, c.Thousands, p.digits)
}
func field(rec []string, i int) (string, error) {
if i < 0 || i >= len(rec) {
return "", fmt.Errorf("index %d out of range, row has %d columns", i, len(rec))
}
return rec[i], nil
}
func isBlank(rec []string) bool {
for _, f := range rec {
if strings.TrimSpace(f) != "" {
return false
}
}
return true
}
func abs(v int64) int64 {
if v < 0 {
return -v
}
return v
}
-112
View File
@@ -1,112 +0,0 @@
package parser
import (
"os"
"path/filepath"
"testing"
"git.petrovv.com/nikola/money/internal/config"
)
func writeFile(t *testing.T, name, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), name)
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
return path
}
func TestCSVSignedAmountColumn(t *testing.T) {
path := writeFile(t, "st.csv", `Date,Ref,Description,Amount
02.01.2026,X1,LIDL SOFIA 1234,"-45,20"
03.01.2026,X2,ACME PAYROLL,"1.500,00"
04.01.2026,X3,COFFEE,"-3,50"
`)
acc := &config.Account{Slug: "checking", Currency: "EUR", Parser: "csv", CSV: &config.CSVConfig{
SkipRows: 1,
Date: config.Column{Col: 0, Layout: "02.01.2006"},
Description: config.Column{Col: 2},
Amount: &config.Column{Col: 3, Decimal: ",", Thousands: "."},
}}
p, err := For(acc)
if err != nil {
t.Fatal(err)
}
got, err := p.Parse(path, acc)
if err != nil {
t.Fatal(err)
}
want := []RawTxn{
{Date: "2026-01-02", Description: "LIDL SOFIA 1234", AmountMinor: -4520},
{Date: "2026-01-03", Description: "ACME PAYROLL", AmountMinor: 150000},
{Date: "2026-01-04", Description: "COFFEE", AmountMinor: -350},
}
if len(got) != len(want) {
t.Fatalf("got %d txns, want %d: %+v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("txn %d = %+v, want %+v", i, got[i], want[i])
}
}
}
func TestCSVDebitCreditPair(t *testing.T) {
path := writeFile(t, "st.csv", `2026-02-01;RENT;800.00;
2026-02-05;SALARY;;2500.00
`)
acc := &config.Account{Slug: "checking", Currency: "EUR", Parser: "csv", CSV: &config.CSVConfig{
Delimiter: ";",
Date: config.Column{Col: 0, Layout: "2006-01-02"},
Description: config.Column{Col: 1},
Debit: &config.Column{Col: 2},
Credit: &config.Column{Col: 3},
}}
p, err := For(acc)
if err != nil {
t.Fatal(err)
}
got, err := p.Parse(path, acc)
if err != nil {
t.Fatal(err)
}
if len(got) != 2 {
t.Fatalf("got %d txns, want 2: %+v", len(got), got)
}
if got[0].AmountMinor != -80000 {
t.Errorf("debit row = %d, want -80000", got[0].AmountMinor)
}
if got[1].AmountMinor != 250000 {
t.Errorf("credit row = %d, want 250000", got[1].AmountMinor)
}
}
func TestCSVConfigErrors(t *testing.T) {
cases := map[string]*config.Account{
"no csv section": {Slug: "a", Parser: "csv"},
"no amount columns": {Slug: "a", Parser: "csv", CSV: &config.CSVConfig{
Date: config.Column{Layout: "2006-01-02"},
}},
"amount and debit together": {Slug: "a", Parser: "csv", CSV: &config.CSVConfig{
Date: config.Column{Layout: "2006-01-02"},
Amount: &config.Column{Col: 1},
Debit: &config.Column{Col: 2},
}},
"no date layout": {Slug: "a", Parser: "csv", CSV: &config.CSVConfig{
Amount: &config.Column{Col: 1},
}},
}
for name, acc := range cases {
if _, err := For(acc); err == nil {
t.Errorf("%s: expected an error", name)
}
}
}
func TestUnknownParser(t *testing.T) {
if _, err := For(&config.Account{Slug: "a", Parser: "nope"}); err == nil {
t.Error("expected an error for an unregistered parser")
}
}
+19 -12
View File
@@ -18,8 +18,8 @@ func init() {
// nlbParser reads an NLB izpisek PDF.
//
// A transaction starts on a line beginning with a dd.mm.yy date and ends with
// the signed amount and the running balance. Long descriptions and the
// counterparty account wrap onto indented continuation lines below.
// the signed amount and the running balance. Long descriptions and the other
// side's account number wrap onto indented continuation lines below.
type nlbParser struct {
digits int
}
@@ -37,7 +37,9 @@ var (
`(?P<balance>[-\x{2212}]?[\d.,]+[-\x{2212}]?)\s*$`)
// Columns inside a line are separated by four or more spaces.
nlbFieldSplit = regexp.MustCompile(`\s{4,}`)
// A Slovenian IBAN, which is the counterparty account when present.
// A Slovenian IBAN, which NLB prints in a column of its own. It is split
// out only so that its wrapped fragments can be rejoined; the result goes
// onto the end of the description, where every other parser keeps it.
nlbAccount = regexp.MustCompile(`^SI\d{2}(?:\s?\d{4}){3}\s?\d{3}$`)
)
@@ -63,9 +65,12 @@ func (p *nlbParser) Parse(path string, acc *config.Account) ([]RawTxn, error) {
// be tested against captured pdftotext output.
func parseNLBText(text string, digits int) ([]RawTxn, error) {
var (
txns []RawTxn
accts []string // counterparty per transaction, built up alongside
descAt int // description column of the last transaction line
txns []RawTxn
// The account column per transaction, built up alongside because it
// wraps in fragments of its own and has to be rejoined before it can
// be appended to the description.
trailing []string
descAt int // description column of the last transaction line
)
for _, page := range pages(text) {
@@ -84,7 +89,7 @@ func parseNLBText(text string, digits int) ([]RawTxn, error) {
return nil, fmt.Errorf("line %d: %w", i+1, err)
}
txns = append(txns, txn)
accts = append(accts, account)
trailing = append(trailing, account)
descAt = col
continue
}
@@ -104,20 +109,22 @@ func parseNLBText(text string, digits int) ([]RawTxn, error) {
last := len(txns) - 1
txns[last].Description += " " + parts[0]
if len(parts) > 1 {
accts[last] = strings.TrimSpace(accts[last] + " " + parts[1])
trailing[last] = strings.TrimSpace(trailing[last] + " " + parts[1])
}
}
}
// The account column goes last rather than in the position it occupied on
// the page, so that an IBAN wrapped over several lines stays contiguous
// and a glob can match it.
for i := range txns {
txns[i].Counterparty = accts[i]
txns[i].Description = strings.Join(strings.Fields(txns[i].Description), " ")
txns[i].Description = strings.Join(strings.Fields(txns[i].Description+" "+trailing[i]), " ")
}
return txns, nil
}
// parseNLBLine parses one transaction line, returning the transaction, the
// counterparty account found among its columns, and the column at which the
// account number found among its columns, and the column at which the
// description starts, which is where its wrapped lines will sit.
func parseNLBLine(line string, digits int) (RawTxn, string, int, error) {
trimmed := strings.TrimRight(line, " \t\r")
@@ -154,7 +161,7 @@ func parseNLBLine(line string, digits int) (RawTxn, string, int, error) {
return RawTxn{}, "", 0, fmt.Errorf("balance %q: %w", balance, err)
}
// The middle holds the description and, sometimes, the counterparty IBAN.
// The middle holds the description and, sometimes, the IBAN.
var (
account string
desc []string
+8 -10
View File
@@ -4,7 +4,7 @@ import "testing"
// A page as pdftotext -layout renders it: a header, transaction lines ending
// in amount and balance, and indented continuation lines carrying wrapped
// descriptions and the counterparty IBAN.
// descriptions and the other side's IBAN.
// The description column sits at 15, which is what the script's
// CONTINUATION_INDENT tells us about the real layout.
const nlbPage = ` NLB d.d.
@@ -35,14 +35,12 @@ func TestParseNLBText(t *testing.T) {
if first.Date != "2026-01-02" {
t.Errorf("date = %q, want 2026-01-02", first.Date)
}
// The continuation line is appended to the description.
if first.Description != "PLACILO S KARTICO LIDL SOFIA 4412" {
// The continuation line is appended to the description, and the IBAN in
// its second column goes last so that it stays contiguous however many
// lines it wrapped over.
if first.Description != "PLACILO S KARTICO LIDL SOFIA 4412 SI56 1234 5678 9012 345" {
t.Errorf("description = %q", first.Description)
}
// ...and the IBAN in its second column becomes the counterparty.
if first.Counterparty != "SI56 1234 5678 9012 345" {
t.Errorf("counterparty = %q", first.Counterparty)
}
if first.AmountMinor != -4520 {
t.Errorf("amount = %d, want -4520", first.AmountMinor)
}
@@ -57,9 +55,9 @@ func TestParseNLBText(t *testing.T) {
t.Errorf("description = %q", txns[1].Description)
}
// A transaction with no continuation line keeps an empty counterparty.
if txns[2].Counterparty != "" {
t.Errorf("counterparty = %q, want empty", txns[2].Counterparty)
// A transaction with no continuation line gets nothing appended.
if txns[2].Description != "PRENOS NA VARCEVALNI" {
t.Errorf("description = %q, want no trailing account number", txns[2].Description)
}
// A trailing minus marks a negative balance.
+4 -8
View File
@@ -1,11 +1,10 @@
// Package parser turns a statement file into raw transactions.
//
// Every bank needs its own extraction logic, so parsers are looked up by name
// from a registry. Two are built in: "csv" for delimited exports with
// configurable columns, and "cmd" for shelling out to an external extractor
// (which is how the existing Python scripts are used until they are ported).
// Ported extractors register themselves here and become usable by putting
// their name in an account.toml.
// from a registry that each one joins from an init. A parser becomes usable by
// putting its name in an account.toml; there is no generic column-mapped
// parser, because a statement layout is better described in Go, where it can
// be tested, than in a table of column indexes.
package parser
import (
@@ -23,9 +22,6 @@ type RawTxn struct {
Description string
AmountMinor int64 // signed; negative is an outflow
// Counterparty is the other side's account number, where the statement
// gives one. Optional.
Counterparty string
// Type is the bank's own classification of the transaction. Optional.
Type string
// BalanceMinor is the running balance after this transaction, when the
+11 -7
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"regexp"
"sort"
"strings"
"time"
@@ -36,9 +35,6 @@ type revolutParser struct {
warnings []string
}
// revolutIBAN finds a counterparty account inside a description.
var revolutIBAN = regexp.MustCompile(`\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b`)
// Columns the parser needs; a missing one is a hard error rather than a
// silently empty field.
var revolutColumns = []string{
@@ -164,18 +160,26 @@ func (p *revolutParser) row(get func(string) string, digits int) (RawTxn, error)
desc += fmt.Sprintf(" (fee %s)", model.FormatMinor(fee, digits))
}
counterparty := revolutIBAN.FindString(desc)
return RawTxn{
Date: d.Format("2006-01-02"),
Description: desc,
AmountMinor: amount - fee,
Counterparty: counterparty,
Type: strings.TrimSpace(get("Type")),
BalanceMinor: &balance,
}, nil
}
// isBlank reports whether a CSV record holds nothing but whitespace, which is
// what a trailing newline in the export reads as.
func isBlank(rec []string) bool {
for _, f := range rec {
if strings.TrimSpace(f) != "" {
return false
}
}
return true
}
// headerIndex maps a Revolut column name to its position.
type headerIndex map[string]int
+12 -3
View File
@@ -1,12 +1,24 @@
package parser
import (
"os"
"path/filepath"
"strings"
"testing"
"git.petrovv.com/nikola/money/internal/config"
)
// writeFile drops a statement in a temporary directory and returns its path.
func writeFile(t *testing.T, name, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), name)
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
return path
}
// A Revolut export: out of date order, mixed currencies, a pending row, a row
// with a fee, and a transfer carrying an IBAN.
const revolutCSV = `Type,Product,Started Date,Completed Date,Description,Amount,Fee,Currency,State,Balance
@@ -60,9 +72,6 @@ func TestRevolutParse(t *testing.T) {
if !strings.Contains(transfer.Description, "(fee 0.35)") {
t.Errorf("description = %q, want a fee note", transfer.Description)
}
if transfer.Counterparty != "SI56123456789012345" {
t.Errorf("counterparty = %q, want the IBAN from the description", transfer.Counterparty)
}
if transfer.BalanceMinor == nil || *transfer.BalanceMinor != 73441 {
t.Errorf("balance = %v, want 73441", transfer.BalanceMinor)
}
-2
View File
@@ -32,7 +32,6 @@ var (
trAmount = regexp.MustCompile(`[-\x{2212}]?€\s?[-\x{2212}]?[\d,]+\.\d{2}`)
trToken = regexp.MustCompile(`\S+`)
trFullDate = regexp.MustCompile(`^\d{2} [A-Z][a-z]{2} \d{4}$`)
trIBAN = regexp.MustCompile(`\b[A-Z]{2}\d{2}[A-Z0-9]{11,30}\b`)
)
var (
@@ -197,7 +196,6 @@ func parseTradeRepublicBlock(lines []string, cols map[string]int, digits int) (R
Date: d.Format("2006-01-02"),
Description: desc,
AmountMinor: amount,
Counterparty: trIBAN.FindString(desc),
Type: strings.Join(words["TYPE"], " "),
BalanceMinor: amounts["BALANCE"],
}, true, nil
-3
View File
@@ -120,9 +120,6 @@ func TestParseTradeRepublicText(t *testing.T) {
if transfer.Description != "Standing order to sav-ings SI56123456789012345" {
t.Errorf("description = %q, want the wrapped fragment joined without a space", transfer.Description)
}
if transfer.Counterparty != "SI56123456789012345" {
t.Errorf("counterparty = %q", transfer.Counterparty)
}
if transfer.AmountMinor != -50000 {
t.Errorf("amount = %d, want -50000", transfer.AmountMinor)
}