-
Notifications
You must be signed in to change notification settings - Fork 8
/
regdel
executable file
·431 lines (378 loc) · 14.3 KB
/
regdel
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/python
# coding=utf-8
# Regdel, a ncurses inteface to ledger
#
# copyright (c) 2016 Guillaume Chereau <guillaume@noctua-software.com>
#
# Regdel is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Regdel 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# regdel. If not, see <http://www.gnu.org/licenses/>.
import itertools
from collections import namedtuple
import curses
import datetime
from math import *
import re
import subprocess
import sys
import csv
import locale
locale.setlocale(locale.LC_ALL, '')
LineInfo = namedtuple('LineInfo', ['color_id', 'color', 'bg', 'attr'])
# XXX: how to clean this up?
LINE_INFOS = {
"bar": LineInfo(0, curses.COLOR_WHITE, curses.COLOR_BLUE, curses.A_BOLD),
"cursor": LineInfo(0, curses.COLOR_WHITE, curses.COLOR_GREEN, curses.A_BOLD),
"date": LineInfo(0, curses.COLOR_BLUE, -1, 0),
"value_pos": LineInfo(0, curses.COLOR_GREEN, -1, 0),
"value_neg": LineInfo(0, curses.COLOR_RED, -1, 0),
}
KEY_MAP = {
ord('k'): "PREV_LINE",
ord('j'): "NEXT_LINE",
curses.KEY_UP: "PREV_LINE",
curses.KEY_DOWN: "NEXT_LINE",
ord('q'): "QUIT",
ord('x'): "COMMODITY",
ord('r'): "REAL",
ord('\n'): "SELECT",
ord('b'): "BALANCE",
curses.KEY_NPAGE: "NEXT_PAGE",
ord(' '): "NEXT_PAGE",
curses.KEY_PPAGE: "PREV_PAGE",
ord('g'): "FIRST_LINE",
curses.KEY_HOME: "FIRST_LINE",
ord('G'): "LAST_LINE",
curses.KEY_END: "LAST_LINE",
}
def clamp(x, a, b): return min(max(x, a), b)
def round_nice(x, n=0):
power = max(0, round(log10(x)) - n)
return ceil(x / (10 ** power)) * (10 ** power)
def ledger(path, cmd, query="", form=None, commodity=None, real=None, options=None):
args = ['ledger', '-f', path]
args += [cmd]
if options: args += options
form_keys = None
if isinstance(form, dict):
form_keys = form.keys()
form = form.values()
if form is not None:
form = ','.join('%(quoted(join({})))'.format(x) for x in form)
form += '\n'
args += ['--format', form]
if commodity is not None:
args += ['-X', commodity]
if real is True:
args += ['--real']
args += [query]
out = subprocess.check_output(args, stderr=subprocess.STDOUT)
out = [x for x in out.split('\n') if x.strip()]
if form is not None:
out = [x for x in csv.reader(out)]
if form_keys is not None:
out = [{k:v for k, v in zip(form_keys, x)} for x in out]
return out
def resample(amounts, size):
"""from a list of tuples: (date, value), generates a pandas timeserie
of the average value per month.
size is the number of values we want in the table.
"""
ts = pd.DataFrame([float(x[1]) for x in amounts],
index=[pd.Timestamp(x[0]) for x in amounts])
freq = (pd.Timedelta(ts.index[-1] - ts.index[0]) / size).round('d') + \
pd.Timedelta('1d')
ts = ts.resample("%dd" % freq.days , how='sum')
ts.fillna(0, inplace=True)
ts = pd.rolling_window(ts, 2 * 30 / freq.days, win_type='hamming',
min_periods=0, center=True)
# Compute the average per month.
ts *= pd.Timedelta('30d') / freq
return ts
class View(object):
def __init__(self, app, win):
self.app = app
self.lineno = 0
h, w = win.getmaxyx()
self.full = win
self.win = curses.newwin(h - 1, w, 1, 0)
self.bar = curses.newwin(1, w, 0, 0)
self.bar.bkgdset(' ', self.get_attr('bar'))
self.w = w
self.h = h - 1
self.offset = 0
self.commodities = None
self.commodity = None
self.real = False
def get_attr(self, line):
if line in LINE_INFOS:
inf = LINE_INFOS[line]
return curses.color_pair(inf.color_id) | inf.attr
else:
return 0
def refresh(self):
self.lineno = clamp(self.lineno, 0, len(self.lines) - 1)
h = self.win.getmaxyx()[0]
# Make sure lineno is visible (ie: offset < lineno < offset + h)
self.offset = min(self.offset, self.lineno)
self.offset = max(self.offset, self.lineno - h + 1)
self.win.clear()
for i in range(h):
self.win.move(i, 0)
self.render(self.win, i + self.offset)
self.bar.clear()
self.bar.move(0, 0)
self.render_bar(self.bar)
self.win.refresh()
self.bar.refresh()
def select(self, i):
return None
def selected_account(self, i):
return None
def toggle_real(self):
self.real = not self.real
self.update()
def toggle_commodity(self):
if self.commodities is None: return
if self.commodity in self.commodities:
i = self.commodities.index(self.commodity) + 1
if i == len(self.commodities):
self.commodity = None
else:
self.commodity = self.commodities[i]
elif self.commodities:
self.commodity = self.commodities[0]
self.update()
def render_bar(self, win):
return
def addstr(self, s, *args, **kargs):
"""Call self.win.addstr with unicode support
The arguments are passed in a 'format' call.
'attr' is a special key argument that can be used to set the style.
"""
# The code is a bit tricky because format needs the strings to be
# in unicode (so that the length are correct) but addstr only
# works with ascii.
def decode(x):
return x.decode('utf-8') if isinstance(x, str) else x
attr = kargs.get('attr', "")
args = [decode(x) for x in args]
kargs = {k: decode(x) for k, x in kargs.items()}
s = decode(s).format(*args, **kargs)
self.win.addstr(s.encode('utf-8'), self.get_attr(attr))
def update(self):
return
class AccountsView(View):
def __init__(self, app, win):
super(AccountsView, self).__init__(app, win)
self.update()
def update(self):
lines = ledger(self.app.path, 'accounts', real=self.real)
accounts = []
for line in lines:
for i in range(len(line.split(':'))):
a = ':'.join(line.split(':')[:i+1])
if a not in accounts: accounts.append(a)
self.lines = accounts
def render(self, win, i):
if i >= len(self.lines): return
line = self.lines[i]
cur = 'cursor' if i == self.lineno else None
parts = line.split(":")
text = " " * (len(parts) - 1) + parts[-1]
win.addstr(text, self.get_attr(cur or 'account'))
def render_bar(self, win):
win.addstr("Accounts")
if self.real is True:
win.addstr(" (REAL)")
def select(self, i):
return RegView(self.app, self.full, self.lines[i])
# So that the 'b' key shows the balance.
# XXX: we could directly show the balance from the start?
def selected_account(self, i):
return ""
class RegView(View):
def __init__(self, app, win, account):
assert isinstance(account, str)
super(RegView, self).__init__(app, win)
h, w = win.getmaxyx()
self.account = account
self.commodity = None
self.commodities = ledger(self.app.path, 'commodities', self.account,
real=self.real)
self.update()
def update(self):
h, w = self.full.getmaxyx()
form = dict(date='date', payee='payee',
amount='scrub(display_amount)',
commodity='commodity(scrub(display_amount))',
total='scrub(display_total)',
account='display_account')
self.lines = ledger(self.app.path, 'reg', self.account,
form=form, commodity=self.commodity, real=self.real,
options=['--effective', '-S', 'date'])
self.win.resize(h - 1, w)
def render(self, win, i):
if i >= len(self.lines): return
line = self.lines[i]
if not line: return
cur = 'cursor' if i == self.lineno else None
self.addstr("{} ", line['date'], attr = cur or 'date')
self.addstr("{:<20.20} ", line['payee'], attr = cur or 'payee')
self.addstr("{:<20.20} ", line['account'], attr = cur or 'payee')
self.addstr("{:>20} ", line['amount'], attr = cur or 'value_pos')
tot = "ERR"
for t in line['total'].split('\\n'):
if line['commodity'] in t:
tot = t
self.addstr("{:>20} ", tot, attr = cur)
def render_bar(self, win):
win.addstr(self.account)
if self.real is True:
win.addstr(" (REAL)")
if self.commodity:
win.addstr(" ({})".format(self.commodity))
win.addstr(" {}".format(self.commodities))
def select(self, i):
line = self.lines[i]
query = '{} @{}'.format(self.account, line['payee'])
date = datetime.datetime.strptime(line['date'], '%Y/%m/%d').date()
enddate = date + datetime.timedelta(1)
out = ledger(self.app.path, 'print', query, real=self.real,
options=['--raw',
'--effective',
'--begin', "{}".format(date),
'--end', "{}".format(enddate)])
return TransactionView(self.app, self.full, out)
def selected_account(self, i):
return self.account
class TransactionView(View):
def __init__(self, app, win, lines):
super(TransactionView, self).__init__(app, win)
self.lines = lines
def render(self, win, i):
if i >= len(self.lines): return
line = self.lines[i]
cur = 'cursor' if i == self.lineno else None
win.addstr(line, self.get_attr(cur))
def select(self, i):
txt = self.lines[i].split()[0]
return RegView(self.app, self.full, txt)
class BalanceView(View):
def __init__(self, app, win, account):
super(BalanceView, self).__init__(app, win)
self.account = account
self.commodity = None
self.update()
self.commodities = ledger(self.app.path, 'commodities', self.account,
real=self.real)
def update(self):
form = dict(account='account',
total='scrub(display_total)')
self.lines = ledger(self.app.path, 'bal', self.account,
form=form, commodity=self.commodity,
real=self.real, options=['--empty', '--no-total'])
def render(self, win, i):
if i >= len(self.lines): return
line = self.lines[i]
if not line: return
cur = 'cursor' if i == self.lineno else None
parts = line['account'].split(":")
text = " " * (len(parts) - 1) + parts[-1]
self.addstr("{:<20.20} ", text, attr = cur or 'payee')
tot = ' + '.join(line['total'].split('\\n'))
self.addstr("{:<60.60} ", tot, attr = cur or 'payee')
def render_bar(self, win):
win.addstr("Balance {}".format(self.account))
if self.real is True:
win.addstr(" (REAL)")
if self.commodity:
win.addstr(" ({})".format(self.commodity))
win.addstr(" {}".format(self.commodities))
def select(self, i):
return RegView(self.app, self.full, self.lines[i]['account'])
class App:
def __init__(self, path, scr):
self.path = path
self.scr = scr
self.view = AccountsView(self, self.scr)
self.views = [self.view]
def process(self, req):
#XXX: auto call refresh if lineno or offset changed.
step = 0
page_height = self.view.win.getmaxyx()[0]
if req == "QUIT":
self.views.pop()
if not self.views: return True
self.view = self.views[-1]
self.view.refresh()
if req == "REAL":
self.view.toggle_real()
self.view.refresh()
if req == "COMMODITY":
self.view.toggle_commodity()
self.view.refresh()
if req == "NEXT_LINE": step = +1
if req == "PREV_LINE": step = -1
if req == "NEXT_PAGE":
step = page_height
if self.view.offset + page_height < len(self.view.lines):
self.view.offset += page_height
if req == "PREV_PAGE":
step = -page_height
self.view.offset = max(0, self.view.offset - page_height)
if req == "FIRST_LINE":
self.view.lineno = 0
self.view.offset = 0
self.view.refresh()
if req == "LAST_LINE":
self.view.lineno = len(self.view.lines)
self.view.refresh()
if req == "SELECT":
view = self.view.select(self.view.lineno)
if view:
self.views.append(view)
self.view = view
self.view.refresh()
if req == "BALANCE":
account = self.view.selected_account(self.view.lineno)
if account is not None:
view = BalanceView(self, self.scr, account)
self.views.append(view)
self.view = view
self.view.refresh()
self.view.lineno += step
if req == "MAIN" or step != 0:
self.view.refresh()
def run(self):
self.process("MAIN")
while True:
c = self.scr.getch()
req = KEY_MAP.get(c)
if self.process(req): break
if len(sys.argv) != 2:
print "USAGE: regdel <ledger-file>"
sys.exit(-1)
curses.initscr()
curses.start_color()
curses.use_default_colors()
def start(scr):
for i, key in enumerate(LINE_INFOS):
inf = LINE_INFOS[key]
color_id = i + 1
curses.init_pair(color_id, inf.color, inf.bg)
LINE_INFOS[key] = inf._replace(color_id=color_id)
app = App(sys.argv[1], scr)
app.run()
try:
curses.wrapper(start)
except subprocess.CalledProcessError as err:
print err.output