-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse-bank-statement.py
executable file
·83 lines (74 loc) · 2.87 KB
/
parse-bank-statement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
# SPDX-FileCopyrightText: 2019, 2021–2022 Felix Gruber <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
from pathlib import Path
import sys
from typing import TextIO
from parsers import parsers
aparser = argparse.ArgumentParser(
description='parse a bank statement into hledger format')
aparser.add_argument('-o', metavar='OUTFILE', dest='outfile', default=None,
help='write to OUTFILE instead of stdout')
aparser.add_argument('--rules', metavar='RULES_DIR', default=None, type=Path,
help='read cleaning and mapping rules from this dir')
aparser.add_argument('--raw', dest='raw', default=False,
action='store_true',
help='write raw parsing results (useful when creating filters)')
aparser.add_argument('--meta', dest='meta', default=False,
action='store_true',
help='parse only metadata')
aparser.add_argument('--json', dest='json', default=False,
action='store_true',
help='when coupled with meta, write output as JSON dict')
aparser.add_argument('bank', action='store',
help='bank to parse from ({})'.format(', '.join(sorted(parsers))))
aparser.add_argument('infile', action='store',
type=Path,
help='the account statement file downloaded from your bank'
' (probably a pdf or csv file)')
args = aparser.parse_args()
def open_outfile() -> TextIO:
if args.outfile is None:
outfile = sys.stdout
else:
outfile = open(args.outfile, 'w')
return outfile
try:
bank_parsers = parsers[args.bank]
except KeyError:
print(f'Unknown parser: {args.bank}', file=sys.stderr)
print('Please use one of the following parsers:', file=sys.stderr)
print(parsers, file=sys.stderr)
exit(1)
try:
extension = args.infile.suffix.lower()
Parser = bank_parsers[extension]
except KeyError:
print(f"Don't know how to parse file of type {extension}", file=sys.stderr)
exit(1)
transactions_parser = Parser(args.infile)
if args.meta:
try:
metadata = transactions_parser.parse_metadata()
except NotImplementedError as e:
print(f'Warning: couldn\'t parse {args.infile}:', e.args,
file=sys.stderr)
exit(0)
if args.json:
metadata.write_json(open_outfile())
else:
metadata.write(open_outfile())
else:
try:
config = transactions_parser.config_type().load(args.rules)
bank_statement = transactions_parser.parse(config)
except NotImplementedError as e:
print(f'Warning: couldn\'t parse {args.infile}:', e.args,
file=sys.stderr)
exit(0)
if not args.raw:
bank_statement.write_ledger(open_outfile())
else:
bank_statement.write_raw(open_outfile())