1 from http.server import BaseHTTPRequestHandler, HTTPServer
5 from urllib.parse import parse_qs, urlparse
10 class HandledException(Exception):
14 def handled_error_exit(msg):
15 print(f"ERROR: {msg}")
19 def apply_booking_to_account_balances(account_sums, account, currency, amount):
20 if not account in account_sums:
21 account_sums[account] = {currency: amount}
22 elif not currency in account_sums[account].keys():
23 account_sums[account][currency] = amount
25 account_sums[account][currency] += amount
28 def parse_lines(lines):
31 inside_booking = False
32 date_string, description = None, None
37 lines += [''] # to ensure a booking-ending last line
38 for i, line in enumerate(lines):
40 # we start with the case of an utterly empty line
42 stripped_line = line.rstrip()
43 if stripped_line == '':
45 # assume we finished a booking, finalize, and commit to DB
46 if len(booking_lines) < 2:
47 raise HandledException(f"{prefix} booking ends to early")
48 booking = Booking(date_string, description, booking_lines, start_line)
50 # expect new booking to follow so re-zeroall booking data
51 inside_booking = False
52 date_string, description = None, None
55 # if non-empty line, first get comment if any, and commit to DB
56 split_by_comment = stripped_line.split(sep=";", maxsplit=1)
57 if len(split_by_comment) == 2:
58 comments[i] = split_by_comment[1]
59 # if pre-comment empty: if inside_booking, this must be a comment-only line so we keep it for later ledger-output to capture those comments; otherwise, no more to process for this line
60 non_comment = split_by_comment[0].rstrip()
61 if non_comment.rstrip() == '':
65 # if we're starting a booking, parse by first-line pattern
66 if not inside_booking:
68 toks = non_comment.split(maxsplit=1)
71 datetime.datetime.strptime(date_string, '%Y-%m-%d')
73 raise HandledException(f"{prefix} bad date string: {date_string}")
77 raise HandledException(f"{prefix} bad description: {description}")
79 booking_lines += [non_comment]
81 # otherwise, read as transfer data
82 toks = non_comment.split() # ignore specification's allowance of single spaces in names
84 raise HandledException(f"{prefix} too many booking line tokens: {toks}")
85 amount, currency = None, None
86 account_name = toks[0]
87 if account_name[0] == '[' and account_name[-1] == ']':
88 # ignore specification's differentiation of "virtual" accounts
89 account_name = account_name[1:-1]
90 decimal_chars = ".-0123456789"
94 amount = decimal.Decimal(toks[1])
96 except decimal.InvalidOperation:
98 amount = decimal.Decimal(toks[2])
99 except decimal.InvalidOperation:
100 raise HandledException(f"{prefix} no decimal number in: {toks[1:]}")
101 currency = toks[i_currency]
102 if currency[0] in decimal_chars:
103 raise HandledException(f"{prefix} currency starts with int, dot, or minus: {currency}")
106 inside_amount = False
107 inside_currency = False
111 for i, c in enumerate(value):
113 if c in decimal_chars:
116 inside_currency = True
118 if c in decimal_chars and len(amount_string) == 0:
119 inside_currency = False
125 if c not in decimal_chars:
126 if len(currency) > 0:
127 raise HandledException(f"{prefix} amount has non-decimal chars: {value}")
128 inside_currency = True
129 inside_amount = False
132 if c == '-' and len(amount_string) > 1:
133 raise HandledException(f"{prefix} amount has non-start '-': {value}")
136 raise HandledException(f"{prefix} amount has multiple dots: {value}")
139 if len(amount_string) == 0:
140 raise HandledException(f"{prefix} amount missing: {value}")
141 if len(currency) == 0:
142 raise HandledException(f"{prefix} currency missing: {value}")
143 amount = decimal.Decimal(amount_string)
144 booking_lines += [(account_name, amount, currency)]
146 raise HandledException(f"{prefix} last booking unfinished")
147 return bookings, comments
151 def __init__(self, date_string, description, booking_lines, start_line):
152 self.date_string = date_string
153 self.description = description
154 self.lines = booking_lines
155 self.start_line = start_line
156 self.validate_booking_lines()
157 self.account_changes = self.parse_booking_lines_to_account_changes()
159 def validate_booking_lines(self):
160 prefix = f"booking at line {self.start_line}"
163 for line in self.lines[1:]:
166 _, amount, currency = line
169 raise HandledException(f"{prefix} relates more than one empty value of same currency {currency}")
172 if currency not in sums:
174 sums[currency] += amount
175 if empty_values == 0:
176 for k, v in sums.items():
178 raise HandledException(f"{prefix} does not sum up to zero")
181 for k, v in sums.items():
185 raise HandledException(f"{prefix} has empty value that cannot be filled")
187 def parse_booking_lines_to_account_changes(self):
191 for line in self.lines[1:]:
194 account, amount, currency = line
196 sink_account = account
198 apply_booking_to_account_balances(account_changes, account, currency, amount)
199 if currency not in debt:
200 debt[currency] = amount
202 debt[currency] += amount
204 for currency, amount in debt.items():
205 apply_booking_to_account_balances(account_changes, sink_account, currency, -amount)
206 return account_changes
214 self.db_file = db_name + ".json"
215 self.lock_file = db_name+ ".lock"
219 if os.path.exists(self.db_file):
220 with open(self.db_file, "r") as f:
221 self.real_lines += f.readlines()
222 ret = parse_lines(self.real_lines)
223 self.bookings += ret[0]
224 self.comments += ret[1]
226 def get_lines(self, start, end):
227 return db.real_lines[start:end]
229 def replace(self, start, end, lines):
231 if os.path.exists(self.lock_file):
232 raise HandledException('Sorry, lock file!')
233 if os.path.exists(self.db_file):
234 shutil.copy(self.db_file, self.db_file + ".bak")
235 f = open(self.lock_file, 'w+')
237 text = ''.join(self.real_lines[:start]) + '\n'.join(lines) + ''.join(self.real_lines[end:])
238 with open(self.db_file, 'w') as f:
240 os.remove(self.lock_file)
242 def append(self, lines):
244 if os.path.exists(self.lock_file):
245 raise HandledException('Sorry, lock file!')
246 if os.path.exists(self.db_file):
247 shutil.copy(self.db_file, self.db_file + ".bak")
248 f = open(self.lock_file, 'w+')
250 with open(self.db_file, 'a') as f:
251 f.write('\n' + '\n'.join(lines) + '\n');
252 os.remove(self.lock_file)
255 class MyServer(BaseHTTPRequestHandler):
257 <meta charset="UTF-8">
259 <a href="/ledger">ledger</a>
260 <a href="/balance">balance</a>
261 <a href="/add_free">add free</a>
262 <a href="/add_structured">add structured</a>
265 footer = "</body>\n<html>"
268 length = int(self.headers['content-length'])
269 postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1)
270 parsed_url = urlparse(self.path)
271 if '/add_structured' == parsed_url.path:
272 n_lines = int(len(postvars) / 4)
273 date = postvars['date'][0]
274 description = postvars['description'][0]
275 start_comment = postvars['line_0_comment'][0]
276 lines = [f'{date} {description} ; {start_comment}']
277 for i in range(1, n_lines):
278 account = postvars[f'line_{i}_account'][0]
279 amount = postvars[f'line_{i}_amount'][0]
280 currency = postvars[f'line_{i}_currency'][0]
281 comment = postvars[f'line_{i}_comment'][0]
282 new_main = f'{account} {amount} {currency}'
283 if '' == new_main.rstrip() == comment.rstrip(): # don't write empty lines
286 if comment.rstrip() != '':
287 new_line += f' ; {comment}'
289 elif '/add_free' == parsed_url.path:
290 lines = postvars['booking'][0].splitlines()
291 start = int(postvars['start'][0])
292 end = int(postvars['end'][0])
294 _, _ = parse_lines(lines)
295 if start == end == 0:
298 db.replace(start, end, lines)
299 self.send_response(301)
300 self.send_header('Location', '/')
302 except HandledException as e:
303 self.send_response(400)
305 page = f'{self.header}ERROR: {e}{self.footer}'
306 self.wfile.write(bytes(page, "utf-8"))
309 self.send_response(200)
310 self.send_header("Content-type", "text/html")
313 parsed_url = urlparse(self.path)
314 page = self.header + ''
315 params = parse_qs(parsed_url.query)
316 start = int(params.get('start', ['0'])[0])
317 end = int(params.get('end', ['0'])[0])
318 bonus_lines = int(params.get('bonus_lines', ['0'])[0])
319 if parsed_url.path == '/balance':
320 page += self.balance_as_html(db)
321 elif parsed_url.path == '/add_free':
322 page += self.add_free(db, start, end)
323 elif parsed_url.path == '/add_structured':
324 page += self.add_structured(db, start, end)
325 elif parsed_url.path == '/copy_free':
326 page += self.add_free(db, start, end, copy=True)
327 elif parsed_url.path == '/copy_structured':
328 page += self.add_structured(db, start, end, copy=True)
330 page += self.ledger_as_html(db)
332 self.wfile.write(bytes(page, "utf-8"))
334 def balance_as_html(self, db):
336 for booking in db.bookings:
337 for account, changes in booking.account_changes.items():
338 for currency, amount in changes.items():
339 apply_booking_to_account_balances(account_sums, account, currency, amount)
341 def collect_branches(account_name, path):
344 while len(path_copy) > 0:
345 step = path_copy.pop(0)
347 toks = account_name.split(":", maxsplit=1)
349 if parent in node.keys():
355 k, v = collect_branches(toks[1], path + [parent])
356 if k not in child.keys():
361 for account_name in sorted(account_sums.keys()):
362 k, v = collect_branches(account_name, [])
363 if k not in account_tree.keys():
366 account_tree[k].update(v)
367 def collect_totals(parent_path, tree_node):
368 for k, v in tree_node.items():
369 child_path = parent_path + ":" + k
370 for currency, amount in collect_totals(child_path, v).items():
371 apply_booking_to_account_balances(account_sums, parent_path, currency, amount)
372 return account_sums[parent_path]
373 for account_name in account_tree.keys():
374 account_sums[account_name] = collect_totals(account_name, account_tree[account_name])
376 def print_subtree(lines, indent, node, subtree, path):
377 line = f"{indent}{node}"
378 n_tabs = 5 - (len(line) // 8)
379 line += n_tabs * "\t"
380 if "€" in account_sums[path + node].keys():
381 amount = account_sums[path + node]["€"]
382 line += f"{amount:9.2f} €\t"
385 for currency, amount in account_sums[path + node].items():
386 if currency != '€' and amount > 0:
387 line += f"{amount:5.2f} {currency}\t"
390 for k, v in sorted(subtree.items()):
391 print_subtree(lines, indent, k, v, path + node + ":")
392 for k, v in sorted(account_tree.items()):
393 print_subtree(lines, "", k, v, "")
394 content = "\n".join(lines)
395 return f"<pre>{content}</pre>"
397 def ledger_as_html(self, db):
400 for comment in db.comments:
401 line = f' ; {comment}' if comment != '' else ''
402 lines += [line + line_sep]
403 for booking in db.bookings:
404 i = booking.start_line
406 lines[i] = f'<p>{booking.date_string} {booking.description}{suffix}'
407 for booking_line in booking.lines[1:]:
409 if booking_line == '':
411 suffix = f' {lines[i]}' if len(lines[i]) > 0 else ''
412 value = f' {booking_line[1]} {booking_line[2]}' if booking_line[1] else ''
413 lines[i] = f'{booking_line[0]}{value}{suffix}'
414 lines[i] = lines[i][:-len(line_sep)] + f"""</p>
416 <a href="/add_structured?start={booking.start_line}&end={i+1}">structured</a>
417 / <a href="/add_free?start={booking.start_line}&end={i+1}">free</a>
419 <a href="/copy_structured?start={booking.start_line}&end={i+1}">structured</a>
420 / <a href="/copy_free?start={booking.start_line}&end={i+1}">free</a>
422 return '\n'.join(lines)
424 def header_add_form(self, action):
425 return f"<form method=\"POST\" action=\"/{action}\">\n"
427 def footer_add_form(self, start, end, copy):
431 <input type="hidden" name="start" value={start} />
432 <input type="hidden" name="end" value={end} />
433 <input type="submit">
436 def add_free(self, db, start=0, end=0, copy=False):
437 content = html.escape(''.join(db.get_lines(start, end)))
438 return f'{self.header_add_form("add_free")}<textarea name="booking" rows="8" cols="80">{content}</textarea>{self.footer_add_form(start, end, copy)}'
440 def add_structured(self, db, start=0, end=0, bonus_lines=10, copy=False):
442 lines = db.get_lines(start, end)
443 bookings, comments = parse_lines(lines)
444 if len(bookings) > 1:
445 raise HandledException('can only edit single Booking')
448 def inpu(name, val="", datalist=""):
449 val = val if val is not None else ""
450 safe_val = html.escape(str(val))
451 datalist_string = '' if datalist == '' else f'list="{datalist}"'
452 return f'<input name="{name}" value="{safe_val}" {datalist_string}/>'
453 today = str(datetime.datetime.now())[:10]
454 if len(bookings) == 0:
455 input_lines += f'{inpu("date", today)} {inpu("description", "", "descriptions")} ; {inpu("line_0_comment")}<br />'
458 booking = bookings[0]
459 last_line = len(comments)
460 date_string = today if copy else booking.date_string
461 input_lines += f'{inpu("date", date_string)} {inpu("description", booking.description, "descriptions")} ; {inpu("line_0_comment", comments[0])}<br />'
462 for i in range(1, len(comments)):
463 account = amount = currency = ''
464 if i < len(booking.lines) and booking.lines[i] != '':
465 account = booking.lines[i][0]
466 amount = booking.lines[i][1]
467 currency = booking.lines[i][2]
468 input_lines += f'{inpu(f"line_{i}_account", account, "accounts")} {inpu(f"line_{i}_amount", amount)} {inpu(f"line_{i}_currency", currency, "currencies")} ; {inpu(f"line_{i}_comment", comments[i])}<br />'
469 for j in range(bonus_lines):
471 input_lines += f'{inpu(f"line_{i}_account", "", "accounts")} {inpu(f"line_{i}_amount", "", "amounts")} {inpu(f"line_{i}_currency", "", "currencies")} ; {inpu(f"line_{i}_comment")}<br />'
472 datalist_sets = {'descriptions': set(), 'accounts': set(), 'currencies': set()}
473 for b in db.bookings:
474 datalist_sets['descriptions'].add(b.description)
475 for account, moneys in b.account_changes.items():
476 datalist_sets['accounts'].add(account)
477 for currency in moneys.keys():
478 datalist_sets['currencies'].add(currency)
479 def build_datalist(name):
480 datalist = f'<datalist id="{name}">' + "\n"
481 for item in datalist_sets[name]:
482 safe_item = html.escape(item)
483 datalist += f'<option value="{safe_item}">{safe_item}</option>' + "\n"
484 return f"{datalist}</datalist>\n"
485 datalists = build_datalist('descriptions')
486 datalists += build_datalist('accounts')
487 datalists += build_datalist('currencies')
488 return f'{self.header_add_form("add_structured")}{input_lines}{datalists}{self.footer_add_form(start, end, copy)}'
492 if __name__ == "__main__":
493 webServer = HTTPServer((hostName, serverPort), MyServer)
494 print(f"Server started http://{hostName}:{serverPort}")
496 webServer.serve_forever()
497 except KeyboardInterrupt:
499 webServer.server_close()
500 print("Server stopped.")