From 169d6b20b9f3d63508180971426db3bc195a3581 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sat, 4 Nov 2023 06:14:35 +0100 Subject: [PATCH] Improve ledger.py. --- ledger.py | 207 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 133 insertions(+), 74 deletions(-) diff --git a/ledger.py b/ledger.py index 6e0a482..b93b202 100755 --- a/ledger.py +++ b/ledger.py @@ -4,6 +4,7 @@ import os import html import jinja2 import decimal +import datetime from urllib.parse import parse_qs, urlparse hostName = "localhost" serverPort = 8082 @@ -80,6 +81,7 @@ def parse_lines(lines, validate_bookings=True): bookings = [] comments = [] lines = lines.copy() + [''] # to ensure a booking-ending last line + last_date = '' for i, line in enumerate(lines): prefix = f"line {i}" # we start with the case of an utterly empty line @@ -116,6 +118,9 @@ def parse_lines(lines, validate_bookings=True): datetime.datetime.strptime(date_string, '%Y-%m-%d') except ValueError: raise HandledException(f"{prefix} bad date string: {date_string}") + if last_date > date_string: + raise HandledException(f"{prefix} out-of-order-date") + last_date = date_string try: description = toks[1] except IndexError: @@ -181,11 +186,10 @@ def parse_lines(lines, validate_bookings=True): raise HandledException(f"{prefix} amount has multiple dots: {value}") dots_counted += 1 amount_string += c - if len(amount_string) == 0: - raise HandledException(f"{prefix} amount missing: {value}") if len(currency) == 0: raise HandledException(f"{prefix} currency missing: {value}") - amount = decimal.Decimal(amount_string) + if len(amount_string) > 0: + amount = decimal.Decimal(amount_string) booking_lines += [(account_name, amount, currency)] if inside_booking: raise HandledException(f"{prefix} last booking unfinished") @@ -275,31 +279,72 @@ class Database: def get_lines(self, start, end): return self.real_lines[start:end] - def replace(self, start, end, lines): + def write_db(self, text, mode='w'): import shutil if os.path.exists(self.lock_file): raise HandledException('Sorry, lock file!') - if os.path.exists(self.db_file): - shutil.copy(self.db_file, self.db_file + ".bak") f = open(self.lock_file, 'w+') f.close() - total_lines = self.real_lines[:start] + lines + self.real_lines[end:] - text = '\n'.join(total_lines) - with open(self.db_file, 'w') as f: + + # always back up most recent to .bak + bakpath = f'{self.db_file}.bak' + shutil.copy(self.db_file, bakpath) + + # collect modification times of numbered .bak files + bak_prefix = f'{bakpath}.' + backup_dates = [] + i = 0 + bak_as = f'{bak_prefix}{i}' + while os.path.exists(bak_as): + mod_time = os.path.getmtime(bak_as) + backup_dates += [str(datetime.datetime.fromtimestamp(mod_time))] + i += 1 + bak_as = f'{bak_prefix}{i}' + + # collect what numbered .bak files to save + to_save = [] + datetime_len = 19 + now = str(datetime.datetime.now())[:datetime_len] + while datetime_len > 2: + for i, date in reversed(list(enumerate(backup_dates))): + if date[:datetime_len] == now: + if i not in to_save: + to_save += [i] + break + datetime_len -= 1 + now = now[:datetime_len] + + # remove redundant backup files + j = 0 + for i in reversed(to_save): + if i != j: + source = f'{bak_prefix}{i}' + target = f'{bak_prefix}{j}' + shutil.move(source, target) + else: + print("keeping", i) + j += 1 + for i in range(j, len(backup_dates)): + print("removing", i) + try: + os.remove(f'{bak_prefix}{i}') + except FileNotFoundError: + pass + + # + shutil.copy(self.db_file, f'{bak_prefix}{j}') + with open(self.db_file, mode) as f: f.write(text); os.remove(self.lock_file) + def replace(self, start, end, lines): + total_lines = self.real_lines[:start] + lines + self.real_lines[end:] + text = '\n'.join(total_lines) + self.write_db(text) + def append(self, lines): - import shutil - if os.path.exists(self.lock_file): - raise HandledException('Sorry, lock file!') - if os.path.exists(self.db_file): - shutil.copy(self.db_file, self.db_file + ".bak") - f = open(self.lock_file, 'w+') - f.close() - with open(self.db_file, 'a') as f: - f.write('\n\n' + '\n'.join(lines) + '\n\n'); - os.remove(self.lock_file) + text = '\n\n' + '\n'.join(lines) + '\n\n' + self.write_db(text, 'a') def add_taxes(self, lines, finish=False): ret = [] @@ -318,6 +363,7 @@ class Database: buffer_expenses = 0 kk_expenses = 0 est_expenses = 0 + months_passed = -int(finish) for b in self.bookings: if date == b.date_string: break @@ -330,11 +376,13 @@ class Database: kk_expenses += b.account_changes[acc_kk]['€'] if acc_est in acc_keys: est_expenses += b.account_changes[acc_est]['€'] - if finish and acc_kk_add in acc_keys and acc_kk_minimum in acc_keys: - last_monthbreak_kk_add = b.account_changes[acc_kk_add]['€'] - last_monthbreak_est = b.account_changes[acc_est]['€'] - last_monthbreak_kk_minimum = b.account_changes[acc_kk_minimum]['€'] - last_monthbreak_assets = b.account_changes[acc_buffer]['€'] + if acc_kk_add in acc_keys and acc_kk_minimum in acc_keys: + months_passed += 1 + if finish: + last_monthbreak_kk_add = b.account_changes[acc_kk_add]['€'] + last_monthbreak_est = b.account_changes[acc_est]['€'] + last_monthbreak_kk_minimum = b.account_changes[acc_kk_minimum]['€'] + last_monthbreak_assets = b.account_changes[acc_buffer]['€'] old_needed_income = last_monthbreak_assets + last_monthbreak_kk_add + last_monthbreak_kk_minimum + last_monthbreak_est if finish: ret += [f' {acc_est} {-last_monthbreak_est}€ ; for old assumption of needed income: {old_needed_income}€'] @@ -349,7 +397,6 @@ class Database: E1 = decimal.Decimal(15999) E2 = decimal.Decimal(62809) E3 = decimal.Decimal(277825) - months_passed = int(date[5:7]) - 1 while True: zvE = buffer_expenses - kk_expenses + (12 - months_passed) * needed_income_before_kk if finish: @@ -401,7 +448,8 @@ class Database: final_minus = expenses_so_far + old_needed_income + diff ret += [f' {acc_assets} {-diff} €'] ret += [f' {acc_assets} {final_minus} €'] - ret += [f' {acc_buffer} {-final_minus} €'] + year_needed = buffer_expenses + final_minus + (12 - months_passed - 1) * final_minus + ret += [f' {acc_buffer} {-final_minus} € ; assume as to earn in year: {acc_buffer} + {12 - months_passed - 1} * this = {-year_needed}'] return ret @@ -447,50 +495,61 @@ input[type=number] { text-align: right; font-family: monospace; } footer = "\n" def do_POST(self): - db = Database() - length = int(self.headers['content-length']) - postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1) - parsed_url = urlparse(self.path) - lines = [] - add_empty_line = None - start = int(postvars['start'][0]) - end = int(postvars['end'][0]) - if '/add_structured' == parsed_url.path and not 'revert' in postvars.keys(): - date = postvars['date'][0] - description = postvars['description'][0] - start_comment = postvars['line_0_comment'][0] - lines = [f'{date} {description} ; {start_comment}'] - if 'line_0_add' in postvars.keys(): - add_empty_line = 0 - i = j = 1 - while f'line_{i}_comment' in postvars.keys(): - if f'line_{i}_delete' in postvars.keys(): - i += 1 - continue - elif f'line_{i}_delete_after' in postvars.keys(): - break - elif f'line_{i}_add' in postvars.keys(): - add_empty_line = j - account = postvars[f'line_{i}_account'][0] - amount = postvars[f'line_{i}_amount'][0] - currency = postvars[f'line_{i}_currency'][0] - comment = postvars[f'line_{i}_comment'][0] - i += 1 - new_main = f'{account} {amount} {currency}' - if '' == new_main.rstrip() == comment.rstrip(): # don't write empty lines - continue - j += 1 - new_line = new_main - if comment.rstrip() != '': - new_line += f' ; {comment}' - lines += [new_line] - if 'add_taxes' in postvars.keys(): - lines += db.add_taxes(lines, finish=False) - elif 'add_taxes2' in postvars.keys(): - lines += db.add_taxes(lines, finish=True) - elif '/add_free' == parsed_url.path: - lines = postvars['booking'][0].splitlines() try: + db = Database() + length = int(self.headers['content-length']) + postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=1) + parsed_url = urlparse(self.path) + lines = [] + add_empty_line = None + start = int(postvars['start'][0]) + end = int(postvars['end'][0]) + if '/add_structured' == parsed_url.path and not 'revert' in postvars.keys(): + date = postvars['date'][0] + description = postvars['description'][0] + start_comment = postvars['line_0_comment'][0] + lines = [f'{date} {description} ; {start_comment}'] + if 'line_0_add' in postvars.keys(): + add_empty_line = 0 + i = j = 1 + while f'line_{i}_comment' in postvars.keys(): + if f'line_{i}_delete' in postvars.keys(): + i += 1 + continue + elif f'line_{i}_delete_after' in postvars.keys(): + break + elif f'line_{i}_add' in postvars.keys(): + add_empty_line = j + account = postvars[f'line_{i}_account'][0] + amount = postvars[f'line_{i}_amount'][0] + currency = postvars[f'line_{i}_currency'][0] + comment = postvars[f'line_{i}_comment'][0] + i += 1 + new_main = f' {account} {amount}' + if '' == new_main.rstrip() == comment.rstrip(): # don't write empty lines, ignore currency if nothing else set + continue + if len(amount.rstrip()) > 0: + new_main += f' {currency}' + j += 1 + new_line = new_main + if comment.rstrip() != '': + new_line += f' ; {comment}' + lines += [new_line] + if 'add_sink' in postvars.keys(): + temp_lines = lines.copy() + ['_'] + try: + temp_bookings, _ = parse_lines(temp_lines) + for currency in temp_bookings[0].sink: + amount = temp_bookings[0].sink[currency] + lines += [f'Assets {amount:.2f} {currency}'] + except HandledException: + pass + if 'add_taxes' in postvars.keys(): + lines += db.add_taxes(lines, finish=False) + elif 'add_taxes2' in postvars.keys(): + lines += db.add_taxes(lines, finish=True) + elif '/add_free' == parsed_url.path: + lines = postvars['booking'][0].splitlines() if ('save' in postvars.keys()) or ('check' in postvars.keys()): _, _ = parse_lines(lines) if 'save' in postvars.keys(): @@ -582,7 +641,7 @@ input[type=number] { text-align: right; font-family: monospace; } content = "\n".join(lines) return f"
{content}
" - def ledger_as_html(self, db): + def ledger2_as_html(self, db): elements_to_write = [] account_sums = {} ## nth_of_same_date = 0 @@ -639,7 +698,7 @@ input[type=number] { text-align: right; font-family: monospace; } booking_lines = booking_lines)] return '\n'.join(elements_to_write) - def ledger2_as_html(self, db): + def ledger_as_html(self, db): single_c_tmpl = jinja2.Template('{{c|e}}
') elements_to_write = [] last_i = i = 0 ## @@ -701,6 +760,7 @@ input[type=number] { text-align: right; font-family: monospace; } +
@@ -729,7 +789,6 @@ input[type=number] { text-align: right; font-family: monospace; } """) - import datetime lines = temp_lines if len(''.join(temp_lines)) > 0 else db.get_lines(start, end) bookings, comments = parse_lines(lines, validate_bookings=False) if len(bookings) > 1: @@ -754,7 +813,7 @@ input[type=number] { text-align: right; font-family: monospace; } desc = head_comment = '' if len(bookings) == 0: for i in range(1, 3): - booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': '', 'comment': ''}] + booking_lines += [{'i': i, 'acc': '', 'amt': '', 'curr': '€', 'comment': ''}] date=today else: booking = bookings[0] @@ -772,7 +831,7 @@ input[type=number] { text-align: right; font-family: monospace; } 'i': i, 'acc': account, 'amt': amount, - 'curr': currency if currency else '', + 'curr': currency if currency else '€', 'comment': comments[i], 'comm_cols': len(comments[i])}] content += tmpl.render( -- 2.30.2