-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathledgerparse.py
More file actions
executable file
·472 lines (358 loc) · 12.7 KB
/
ledgerparse.py
File metadata and controls
executable file
·472 lines (358 loc) · 12.7 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# coding=utf-8
import re, datetime, os
# decimal seperator for string output
dec_sep = ','
# general ledger regex
PAT_TRANSACTION = re.compile(r'(\d{4,}.+(?:\n[^\S\n\r]{1,}.+)+)')
PAT_TRANSACTION_DATA = re.compile(r'(?P<year>\d{4})[/|-](?P<month>\d{2})[/|-](?P<day>\d{2})(?:=(?P<year_aux>\d{4})[/|-](?P<month_aux>\d{2})[/|-](?P<day_aux>\d{2}))? (?P<state>[\*|!])?[ ]?(\((?P<code>[^\)].+)\) )?(?P<payee>.+)')
PAT_COMMENT = re.compile(r'[^\S\n\r]{1,};(.+)')
PAT_ACCOUNT = re.compile(r'[^\S\n\r]{1,}(?P<account>[^;].+)(?:[^\S\n\r]{2,})(:?(?P<commodity_front>[^\d].*?)?[^\S\n\r]{1,})?(?P<amount>[-+]?\d+[\,|\.]?(?:\d+)?)?(?:[^\S\n\r]{1,}(?P<commodity_back>[^\d].*?))?')
PAT_ACCOUNT_ONLY = re.compile(r'[^\S\n\r]{1,}(?P<account>[^;].+)')
# ledgerparse classes
class Money(object):
def __init__(self, amount='0', real_amount=None, dec_sep=','):
self.dec_sep = dec_sep
self.amount = self.get_amount(amount) if real_amount == None else real_amount
def get_amount(self, amount):
# amount is zero, if amount is not a matching string
if not type(amount) == str:
amount = '0'
# get rid of the thousand seperator
if self.dec_sep == ',':
amount = amount.replace('.', '')
else:
amount = amount.replace(',', '')
# return integer from amount string
# there is something behind the decimal seperator
if self.dec_sep in amount:
behind = amount.split(self.dec_sep)[1]
if len(behind) == 1:
behind += '000'
elif len(behind) == 2:
behind += '00'
elif len(behind) == 3:
behind += '0'
elif len(behind) > 4:
last_digit = int(behind[3])
after_last_digit = int(behind[4])
if after_last_digit >= 5:
last_digit += 1
behind = behind[:3] + str(last_digit)
return int( amount.split(self.dec_sep)[0] + behind )
# there is no decimal seperator at all
else:
return int( amount ) * 10000
def str_amount(self):
# amount is positive
if self.amount > 0:
# return something like 0,NNNN
if self.amount < 10000:
return '0' + self.dec_sep + self.behind_decimal( str(self.amount)[-4:] )
# return something like N,NNN
else:
return str(self.amount)[:-4] + self.dec_sep + self.behind_decimal( str(self.amount)[-4:] )
# amount is negative
elif self.amount < 0:
# return something like -0,NNNN
if self.amount > -10000:
return '-0' + self.dec_sep + self.behind_decimal( str(self.amount)[-4:] )
# return something like -N,NNN
else:
return str(self.amount)[:-4] + self.dec_sep + self.behind_decimal( str(self.amount)[-4:] )
# amount is zero
else:
return '0' + self.dec_sep + '00'
def behind_decimal(self, value_string):
# get rid of negative sign, which can occur for amounts
# lower than 0,10 (e.g. 0,07)
value_string = value_string.replace('-', '')
# generate leading zeros
if len(value_string) == 3:
value_string = '0' + value_string
elif len(value_string) == 2:
value_string = '00' + value_string
elif len(value_string) == 1:
value_string = '000' + value_string
# get rid of last zero at the end - two times
if value_string[-1:] == '0':
value_string = value_string[:-1]
if value_string[-1:] == '0':
value_string = value_string[:-1]
return value_string
def __lt__(self, other):
return self.amount < other.amount
def __le__(self, other):
return self.amount <= other.amount
def __gt__(self, other):
return self.amount > other.amount
def __ge__(self, other):
return self.amount >= other.amount
def __eq__(self, other):
return self.amount == other.amount
def __str__(self):
return self.str_amount()
def __int__(self):
return self.amount
def __repr__(self):
return self.str_amount()
def __add__(self, other):
return Money(real_amount=self.amount+other.amount)
def __sub__(self, other):
return Money(real_amount=self.amount-other.amount)
def __div__(self, other):
return Money(real_amount=self.amount/other)
def __mul__(self, other):
return Money(real_amount=self.amount*other)
class ledger_transaction(object):
def __init__(self, date, aux_date, state, code, payee, original):
self.date = date
self.aux_date = aux_date
self.state = state
self.code = code
self.payee = payee
self.comments = [] # [ str: comment ]
self.accounts = [] # [ ledger_account ]
self.original = original
def __str__(self):
# generat a transaction output string
output = ''
# get date
tmp_date = self.date.strftime('%Y-%m-%d')
# get aux date
tmp_aux_date = '=' + self.aux_date.strftime('%Y-%m-%d') if self.aux_date != self.date else ''
# get state
tmp_state = ' ' + self.state if self.state else ''
# get code
tmp_code = ' (' + self.code + ')' if self.code else ''
# get payee
tmp_payee = ' ' + self.payee
# get comments
if len(self.comments) > 0:
tmp_comments = '\n ;' + '\n ;'.join(self.comments)
else:
tmp_comments = ''
# get accounts with its comments
tmp_accounts = '\n' + '\n'.join( [str(x) for x in self.accounts] )
# generate the output string
output = tmp_date + tmp_aux_date + tmp_state + tmp_code + tmp_payee + tmp_comments + tmp_accounts
return output
def get_original(self):
return self.original
def add_account(self, name, commodity, amount):
self.accounts.append( ledger_account(name, commodity, amount) )
def add_comment_to_last_account(self, text):
if len(self.accounts) > 0:
self.accounts[ len(self.accounts)-1 ].add_comment(text)
def add_comment(self, text):
self.comments.append(text)
def balance_account(self, id, negative=False):
# get negatvie multiplicator
multi = -1 if negative else 1
# returns a Money object with the amount of the account with the given id
own_amount = self.accounts[id].amount.amount
if own_amount != 0:
return Money(real_amount=self.accounts[id].amount.amount * multi)
else:
others = 0
for which, acc in enumerate(self.accounts):
if not which == id:
others += acc.amount.amount
return Money(real_amount=(own_amount - others) * multi )
def balance_accountname(self, accountname, negative=False):
for i, account in enumerate(self.accounts):
if accountname.lower() in account.name.lower():
return self.balance_account(i, negative)
return Money()
class ledger_account(object):
def __init__(self, name, commodity, amount):
self.name = name
self.commodity = commodity
self.amount = Money(amount, dec_sep=dec_sep)
self.comments = [] # [ str: comment ]
def __str__(self):
# generate an account output string
output = ''
# get the name
tmp_name = self.name
# get the commodity
tmp_commodity = ' ' + self.commodity + ' ' if self.commodity else ''
# get amount
tmp_amount = str(self.amount) if self.amount.amount != 0 else ''
# get comments
tmp_comments = '\n ;' + '\n ;'.join(self.comments) if len(self.comments) > 0 else ''
# return string for this account
return ' ' + tmp_name + tmp_commodity + tmp_amount + tmp_comments
def add_comment(self, text):
self.comments.append(text)
# functions
def string_to_non_transactions(text):
# returns a string with all the stuff in a ledger journal, which are no transactions
# get original
output = text
# iterate every transaction
for trans in PAT_TRANSACTION.findall(text):
# delet this transaction from the output
output = output.replace( trans, '' )
# strip and return the output string
return output.strip()
def string_to_ledger(text, aliases=False):
# returns an array of [ledger_transaction]s from the given string (ledger-journal)
# get aliases from file, if enabled
if aliases:
ALIAS = get_aliases_from_string(text)
else:
ALIAS = {}
# init the output array
output = []
# iterate through all transaction-regex matches
for trans in PAT_TRANSACTION.findall(text):
output.append( string_to_transaction(trans, ALIAS) )
# output the result
return output
def string_to_transaction(text, aliases={}):
# returns a [ledger_transaction] object from the given string
# init variables
last = 'None'
# iterate through the lines of the string
for line in text.splitlines():
# do the matches
m_trans = PAT_TRANSACTION_DATA.match(line)
m_comment = PAT_COMMENT.match(line)
m_account = PAT_ACCOUNT.match(line)
m_account_only = PAT_ACCOUNT_ONLY.match(line)
# the line is the transaction header
if m_trans:
# get the date
tmp_date = datetime.datetime( int(m_trans.group('year')), int(m_trans.group('month')), int(m_trans.group('day')) )
# get the aux date
if m_trans.group('year_aux') != None and m_trans.group('month_aux') != None and m_trans.group('day_aux') != None:
tmp_date_aux = datetime.datetime( int(m_trans.group('year_aux')), int(m_trans.group('month_aux')), int(m_trans.group('day_aux')) )
else:
tmp_date_aux = tmp_date
# get the state
if m_trans.group('state') != None:
tmp_state = m_trans.group('state')
else:
tmp_state = ''
# get the code
if m_trans.group('code') != None:
tmp_code = m_trans.group('code')
else:
tmp_code = ''
# get the payee
tmp_payee = m_trans.group('payee')
# generate ledger_transaction output
output = ledger_transaction(tmp_date, tmp_date_aux, tmp_state, tmp_code, tmp_payee, text)
# set last to trans
last = 'Trans'
# the line is an account with commodity and an amount
elif m_account and not last == 'None':
# get its name
tmp_name = m_account.group('account')
# aliase it, if enabled / aliases exists
if len(aliases) != 0:
tmp_name = replace_alias(tmp_name, aliases)
# get the commodity
if m_account.group('commodity_front') != None:
tmp_commodity = m_account.group('commodity_front')
elif m_account.group('commodity_back') != None:
tmp_commodity = m_account.group('commodity_back')
else:
tmp_commodity = ''
# get the amount
tmp_amount = m_account.group('amount')
# add this account to the output ledger_transaction
output.add_account(tmp_name, tmp_commodity, tmp_amount)
# set last to Acc
last = 'Acc'
# the line is an account with only the account name
elif m_account_only and not last == 'None':
# get its name
tmp_name = m_account_only.group('account')
# aliase it, if enabled / aliases exists
if len(aliases) != 0:
tmp_name = replace_alias(tmp_name, aliases)
# get the commodity
tmp_commodity = ''
# get the amount
tmp_amount = '0'
# add this account to the output ledger_transaction
output.add_account(tmp_name, tmp_commodity, tmp_amount)
# set last to Acc
last = 'Acc'
# the line is a comment
elif m_comment and not last == 'None':
# it belongs to the transaction
if last == 'Trans':
output.add_comment(m_comment.group(1))
# it belongs to the last added account
elif last == 'Acc':
output.add_comment_to_last_account(m_comment.group(1))
# output the ledger_transaction object
return output if not last == 'None' else None
def ledger_file_to_string(ledger_file):
# init the output variable
OUT = ''
# check if file exists and load it
if os.path.isfile(ledger_file):
f = open(ledger_file, 'r')
FILE = f.readlines()
f.close()
# and get its path
PATH = os.path.dirname( os.path.abspath(ledger_file) )
else:
return ''
# append it to the final output, till "include" occurs
for line in FILE:
if line.lower().find('include ') != 0:
OUT += line
# otherwise try to load the given file
else:
# get filename
include_me = line.replace('include ', '').replace('Include ', '').strip()
# cycle through the files, if there are some and append their content
# also replace wildcard with regex-wildcard
regex = include_me.replace('.', '\.').replace('*', '.*') + '$'
got_a_file = 0
for filename in os.listdir(PATH):
if re.match(regex, filename):
if got_a_file != 0:
OUT += '\n\n'
got_a_file = 1
f = open(os.path.join(PATH, filename), 'r')
OUT += f.read()
f.close()
# newline at the end of the found "include ..." if there is one
OUT += '\n' if '\n' in line else ''
# return the final ledger journal string - bam!
return OUT
def get_aliases_from_string(ledger_journal_string):
# init output dict
OUT = {}
# cycle through ledger journal lines and find "alias" in the beginning of the line
for l in ledger_journal_string.splitlines():
if l[0:5] == 'alias':
# get the alias
tmp = l.replace('alias ', '').split('=')
if len(tmp) == 2:
OUT[tmp[0].strip()] = tmp[1].strip()
# output the result
return OUT
def replace_alias(original, replace_dict, second_pass=False):
# TODO:
# aliases, which use other aliases are not being
# replaced.
# check if there are subaccounts and use these first
if ':' in original:
work_with_me = original[ 0:original.find(':') ]
append = original[ original.find(':'): ]
# or just use the original string
else:
work_with_me = original
append = ''
# check if the account is in the dict
if work_with_me in replace_dict:
return replace_dict[ work_with_me ] + append
# or just return the original
else:
return original