-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathledger.py
executable file
·390 lines (300 loc) · 9.95 KB
/
ledger.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python3
# Ledger: Double-entry accounting system
# Copyright (C) 2020 Gokberk Yaltirakli
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from collections import defaultdict
from decimal import Decimal
import os
# File parser
class Block:
def __init__(self, lines):
self.lines = lines
def __bool__(self):
return len(self.lines) != 0
@property
def is_comment(self):
try:
return self.lines[0][0] in ";#%|*"
except Exception:
return False
@property
def date(self):
try:
return self.lines[0].split(" ", 2)[0]
except Exception:
return "1970-01-01"
@property
def name(self):
try:
return self.lines[0].split(" ", 2)[1].lower()
except Exception:
return ""
@property
def arg(self):
try:
return self.lines[0].split(" ", 2)[2]
except Exception:
return ""
@property
def children(self):
return self.lines[1:]
def __repr__(self):
if self:
return f"[{self.name}] ({self.arg}) {self.children}"
return "Block(Empty)"
class BlockLexer:
def __init__(self, reader):
self.reader = reader
@staticmethod
def indented(line):
return line.startswith(" ")
@property
def lines(self):
for line in self.reader:
line = line.rstrip()
if not line:
continue
yield line
def __iter__(self):
block = []
for line in self.lines:
ind = BlockLexer.indented(line)
if ind:
block.append(line.strip())
else:
yield Block(block)
block = [line]
yield Block(block)
def read_blocks(path):
with open(path) as f:
yield from BlockLexer(f)
# Ledger and bookkeeping logic
def warn_strict(message, condition=True):
if args.strict and condition:
print(f"[WARNING] {message}")
class Ledger:
def __init__(self):
self.commodities = set()
self.accounts = defaultdict(lambda: defaultdict(lambda: Decimal(0)))
self.rates = defaultdict(lambda: Decimal(0))
def process_block(self, block):
if not block:
return
if block.is_comment:
return
if block.name == "nop":
# No operation
return
if block.name == "commodity":
com = block.arg.split(" ", 1)[0]
warn_strict(
f"Duplicate commodity '{com}'", com in self.commodities
)
self.commodities.add(com)
elif block.name == "txn":
self.process_transaction(block)
elif block.name == "include":
for bl in read_blocks(block.arg):
self.process_block(bl)
elif block.name == "assert-balance":
name, amt, com = block.arg.split()
amt = Decimal(amt)
bal = self.accounts[name][com]
warn_strict(
f"Expected balance for {name} is {amt}, found {bal}",
bal != amt,
)
elif block.name == "balance":
name, amt, com = block.arg.split()
amt = Decimal(amt)
bal = self.accounts[name][com]
if bal == amt:
return
if bal > amt:
self.accounts["Expenses:ForceBalance"][com] += bal - amt
self.accounts[name][com] -= bal - amt
elif amt > bal:
self.accounts["Income:ForceBalance"][com] -= amt - bal
self.accounts[name][com] += amt - bal
elif block.name == "price":
com1, rate, com2 = block.arg.split()
self.rates[(com1, com2)] = Decimal(rate)
else:
warn_strict(f"Unknown directive '{block.name}'")
def process_transaction(self, block):
total = defaultdict(lambda: Decimal())
for line in block.children:
if len(line.split()) == 1:
account = line
for com in total:
self.accounts[account][com] -= total[com]
total[com] = 0
else:
account, amount, commodity = line.split(" ", 2)
warn_strict(
f"Unknown commodity '{commodity}'",
commodity not in self.commodities,
)
total[commodity] += Decimal(amount)
self.accounts[account][commodity] += Decimal(amount)
# print(block)
# self.consistency_check()
def consistency_check(self):
total = defaultdict(lambda: Decimal())
for name in self.accounts:
for com in self.accounts[name]:
amt = self.accounts[name][com]
total[com] += amt
for com in total:
assert total[com] == 0, f"{total[com]} {com}"
# Command line interface
class Arguments:
def __init__(self, args=None):
if args is None:
args = sys.argv[1:]
self.args = list(args)
self.file = self.args.pop(0)
self.action = self.args.pop(0)
@property
def accounts(self):
accounts = []
for arg in self.args:
a = arg.startswith("-")
if not a:
accounts.append(arg)
return accounts
def filtered_accounts(self, ledger):
accounts = sorted(ledger.accounts)
if args.accounts:
act = []
for account in accounts:
if any(map(lambda x: account.startswith(x), self.accounts)):
act.append(account)
return act
return accounts
@property
def strict(self):
return "--strict" in self.args
subcommands = {}
def subcommand(name=None):
def decorator(func):
subcommands[name or func.__name__] = func
return decorator
@subcommand()
def check():
ledger.consistency_check()
@subcommand()
def accounts():
for name in args.filtered_accounts(ledger):
print(name)
@subcommand()
def balance():
total = defaultdict(lambda: Decimal())
accounts = args.filtered_accounts(ledger)
w = max(accounts, key=lambda x: len(x))
w = len(w)
for name in accounts:
for com in sorted(ledger.accounts[name]):
amt = ledger.accounts[name][com]
total[com] += amt
if amt != 0:
print(f"{name: <{w}} {amt} {com}")
print("\nTotal:")
for com in total:
amt = total[com]
if amt != 0:
print(f" {amt} {com}")
def print_table(table, fmt=None):
table = [[str(x) for x in row] for row in table]
col_num = len(max(table, key=len))
col_w = [0 for _ in range(col_num)]
if not fmt:
fmt = [">" for _ in range(col_num)]
for col in range(col_num):
col_w[col] = len(max(map(lambda x: x[col], table), key=len))
for row in table:
for i, col in enumerate(row):
print(f"{col: {fmt[i]}{col_w[i]}}", end=" ")
print()
@subcommand("converted-balance")
def convertedbalance():
accounts = args.filtered_accounts(ledger)
w = max(accounts, key=len)
w = len(w)
rates = ledger.rates
for _ in range(10):
for com1, com2 in dict(rates):
rel = (com1, com2)
rev = (com2, com1)
if rates[rev] == 0:
rates[rev] = 1 / rates[rel]
def conv_rate(com1, com2):
q = []
discovered = set()
parents = {}
amt = 1
discovered.add(com1)
q.append(com1)
while len(q):
v = q.pop()
if v == com2:
amt = 1
n = com2
while n in parents:
amt *= rates[(parents[n], n)]
n = parents[n]
return amt
for _com1, _com2 in filter(lambda x: x[0] == v, rates):
if _com2 not in discovered:
discovered.add(_com2)
q.append(_com2)
parents[_com2] = _com1
return 0
for target in os.environ.get("CURRENCY", "EUR,TRY,USD").split(","):
grand_total = 0
totals = {}
for name in accounts:
total = 0
for com in ledger.accounts[name]:
amt = ledger.accounts[name][com]
if amt != 0:
conv = conv_rate(com, target)
total += amt * conv
grand_total += total
if total != 0:
totals[name] = total
table = []
table.append(["Name", "Total", "Currency", "Percentage %"])
table.append(["----", "-----", "--------", "------------"])
for name in totals:
total = totals[name]
row = []
row.append(name)
row.append(f"{total:.2f}")
row.append(target)
perc = total / grand_total * 100
row.append(f"{perc:.2f}")
table.append(row)
table.append(["----", "-----", "--------", "------------"])
table.append(["Total", f"{grand_total:.2f}", target, "100"])
print_table(table, ["<", ">", "<", ">"])
print("\n")
args = Arguments()
ledger = Ledger()
blocks = read_blocks(args.file)
for block in sorted(blocks, key=lambda x: x.date):
ledger.process_block(block)
subcommands[args.action]()