-
Notifications
You must be signed in to change notification settings - Fork 0
/
billy
executable file
·366 lines (294 loc) · 8.53 KB
/
billy
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#! /bin/perl
# SPDX-License-Identifier: GPL-2.0+
# Copyright (C) 2020 Abinav Puthan Purayil
use strict;
use warnings;
use feature "say";
use v5.10;
use Data::Dumper;
use Getopt::Long;
sub print_heading {
my ($heading) = @_;
say $heading;
say "=" x length($heading);
}
sub num_commify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text;
}
sub atoi {
my ($num) = @_;
if ($num =~ /^\s*$/) {
return 0;
}
$num =~ s/[,\s]//g;
return $num;
}
sub is_blank_or_comment_line {
my ($line) = @_;
if ($line =~ /^\s*#.*$/ || $line =~ /^\s*$/) {
return 1;
}
return 0;
}
sub trans_struct_set {
my ($path_trans, $trans_struct) = @_;
open(my $fh_trans, "<", $path_trans) || die("can't open $path_trans");
while (my $line = <$fh_trans>) {
if (is_blank_or_comment_line($line)) {
;
# <bank_name> {
} elsif ($line =~ /^\s*([a-zA-Z0-9_]+)\s*\{\s*$/) {
my $bank_name = $1;
while (($line = <$fh_trans>)) {
if (is_blank_or_comment_line($line)) {
next;
}
if ($line !~ m/^\s*\}\s*$/) {
$line =~ /^\s*([a-zA-Z0-9_]+)\s*(.*)\s*$/;
my $regex_frag_name = $1;
my $regex_frag = $2;
if ($regex_frag_name =~ /^(date|narr|debit|credit)$/) {
$$trans_struct{$bank_name}{regex} .= "(?<$regex_frag_name>"
. $regex_frag . ")";
} else {
$$trans_struct{$bank_name}{regex} .= $regex_frag;
}
next;
} else {
last;
}
die ("parsing error: bank not closed at $line");
}
# unrecognized
} else {
die ("parsing error: Unrecognized line at $.: $line");
}
}
}
sub trans_struct_verify {
my ($trans_struct) = @_;
foreach my $bank(keys %{$trans_struct}) {
if (!defined $$trans_struct{$bank}{regex} || !$$trans_struct{$bank}{regex}) {
die "undefined regex for bank: $bank";
}
}
}
sub cat_struct_set {
my ($path_cat, $cat_struct) = @_;
open(my $fh_cat, "<", $path_cat) || die("can't open $path_cat");
while (my $line = <$fh_cat>) {
if (is_blank_or_comment_line($line)) {
;
# cat-def start (ie. <cat_name> [hook_type] {)
# espace {} to reduce headache for vim
} elsif ($line =~ /^\s*([a-zA-Z0-9_]+)\s*([a-z_]*)\s*\{\s*$/) {
my $cat_name = $1;
my $hook_type = "";
if (my @cat_def = $line =~ /^\s*([a-zA-Z0-9_]+)\s+([a-z_]+)\s*\{\s*$/) {
# this cat has a hook :D
$cat_name = $1;
$hook_type = $2;
}
my $i = 0;
while (($line = <$fh_cat>)) {
if (is_blank_or_comment_line($line)) {
next;
}
# cat's regex list
if ($line !~ m/^\s*\}\s*$/) {
$line =~ /^\s*(.*)\s*$/;
if ($hook_type eq "") { # regex-list cat-def
$$cat_struct{$cat_name}{arr_regex}[$i++] = $1;
} else { # hooks's ins
$$cat_struct{$cat_name}{hooks}{$hook_type} .= $1 . "\n";
}
next;
# cat-def end (ie. '}')
} else {
$$cat_struct{$cat_name}{narr} = "";
$$cat_struct{$cat_name}{count} = 0;
$$cat_struct{$cat_name}{credit_total} = 0;
$$cat_struct{$cat_name}{debit_total} = 0;
last;
}
# cat def not closed
die ("parsing error: def not closed at $line");
}
} else {
die ("parsing error: Unrecognized line at $.: $line");
}
}
}
sub cat_struct_insert {
my ($cat_struct, $cat_name, %words) = @_;
$$cat_struct{$cat_name}{narr} .= "$words{date}\t$words{narr}\t"
. "$words{debit}\t$words{credit}\n";
$$cat_struct{$cat_name}{debit_total} += atoi($words{debit});
$$cat_struct{$cat_name}{credit_total} += atoi($words{credit});
$$cat_struct{$cat_name}{count}++;
}
sub cat_struct_fill {
my ($cat_struct, $trans_struct) = @_;
foreach my $bank(keys %{$trans_struct}) {
foreach my $path_stmt(@{$$trans_struct{$bank}{path_stmts}}) {
open(my $fh_stmt, "<", $path_stmt) || die("can't open $path_stmt");
while (my $line = <$fh_stmt>) {
if ($line !~ /$$trans_struct{$bank}{regex}/) {
next;
} # else
my %words = %+;
my $found_cat = 0;
foreach my $cat_name (keys %{$cat_struct}) {
foreach (@{$$cat_struct{$cat_name}{arr_regex}}) {
# ignore case for regex
if ($words{narr} !~ /$_/i) {
next;
} # else
$found_cat = 1;
cat_struct_insert($cat_struct, $cat_name, %words);
}
}
if (!$found_cat) { # uncategorized entries
cat_struct_insert($cat_struct, '__uncat__', %words);
}
}
}
}
}
sub cat_print {
my ($cat_struct) = @_;
foreach my $cat_name (sort keys %{$cat_struct}) {
if ($$cat_struct{$cat_name}{hide}) { next; }
print_heading("category: $cat_name");
print $$cat_struct{$cat_name}{narr};
print "\n";
say "num of transactions: $$cat_struct{$cat_name}{count}";
say "debit_total: "
. num_commify($$cat_struct{$cat_name}{debit_total});
say "credit_total: "
. num_commify($$cat_struct{$cat_name}{credit_total});
print "\n\n\n";
}
}
sub cathook_overall {
my ($cat_struct, $cat_name) = @_;
my @lines = split(/\n+/, $$cat_struct{$cat_name}{hooks}{overall});
foreach my $line (@lines) {
my @words = split(/\s+/, $line);
my $cmd = $words[0];
if ($cmd eq "donate") {
my $cat_from = $cat_name;
my $cat_to = $words[1];
my $cat_attr = $words[2];
my $curr_val_from = $$cat_struct{$cat_from}{$cat_attr};
my $curr_val_to = $$cat_struct{$cat_to}{$cat_attr};
my $delta_val = defined $words[3] ? $words[3] : $curr_val_from;
$$cat_struct{$cat_to}{$cat_attr} += $delta_val;
$$cat_struct{$cat_from}{$cat_attr} -= $delta_val;
} elsif ($cmd eq "del") {
my $cat_attr = $words[1];
my $delta_val = defined $words[2] ? $words[2] :
$$cat_struct{$cat_name}{$cat_attr};
$$cat_struct{$cat_name}{$cat_attr} -= $delta_val;
}
}
}
sub cathook_local {
my ($cat_struct, $cat_name) = @_;
my @lines = split(/\n+/, $$cat_struct{$cat_name}{hooks}{local});
foreach my $line (@lines) {
my @words = split(/\s+/, $line);
my $cmd = $words[0];
if ($cmd eq "hide") {
$$cat_struct{$cat_name}{hide} = 1;
}
}
}
sub cathook_do {
my ($cat_struct, $hook_type_arg) = @_;
my %hook_handle_map = (
"overall" => \&cathook_overall,
"local" => \&cathook_local
);
foreach my $cat_name (sort keys %$cat_struct) {
foreach my $hook_type (sort keys %{$$cat_struct{$cat_name}{hooks}}) {
if ($hook_type eq $hook_type_arg) {
$hook_handle_map{$hook_type}($cat_struct, $cat_name);
}
}
}
}
sub cat_print_overall {
my ($cat_struct, $x_total) = @_;
my $grand_total = 0;
print_heading($x_total);
foreach my $cat_name (sort keys %$cat_struct) {
if (!$$cat_struct{$cat_name}{$x_total}) {
next;
}
$grand_total += $$cat_struct{$cat_name}{$x_total};
printf("%15s: %10s\n\n", $cat_name,
num_commify($$cat_struct{$cat_name}{$x_total}));
}
print "\n";
printf("%15s: %10s\n", "grand total",
num_commify($grand_total));
}
sub usage() {
print
"Usage: billy [OPTIONS]
billy categorizes bank statements
-c <path/to/cat>
-t <path/to/trans>
-b <foobank> sets bank as foobank for the _following_ -s
-s <foobank_stmt1> [foo_bank_stmt2] ...
-h --help to view this
-v --version\n";
}
my %trans_struct;
my %cat_struct;
my $opt_bank = "";
my $opt_path_cat = "";
my $opt_path_trans = "";
sub opt_handler_path_stmts {
my ($opt, $value) = @_;
if (!$opt_bank) {
die "Bank not set";
}
push(@{$trans_struct{$opt_bank}{path_stmts}}, $value);
}
sub opt_handler_help {
usage();
exit 0;
}
sub opt_handler_version {
say "billy v0.1";
say "(C) 2020 Abinav Puthan Purayil";
exit 0;
}
GetOptions(
"c=s" => \$opt_path_cat,
"t=s" => \$opt_path_trans,
"b=s" => \$opt_bank,
"s=s{1,}" => \&opt_handler_path_stmts,
"help" => \&opt_handler_help,
"version" => \&opt_handler_version
) or die "error parsing cmdline";
if (!$opt_path_cat || !$opt_path_trans) {
print STDERR "invalid cmdline\n";
usage();
exit 1;
}
trans_struct_set($opt_path_trans, \%trans_struct);
trans_struct_verify(\%trans_struct);
cat_struct_set($opt_path_cat, \%cat_struct);
cat_struct_fill(\%cat_struct, \%trans_struct);
cathook_do(\%cat_struct, 'local');
cat_print(\%cat_struct);
cathook_do(\%cat_struct, 'overall');
cat_print_overall(\%cat_struct, 'credit_total');
print("\n\n\n");
cat_print_overall(\%cat_struct, 'debit_total');
exit 0;