1 from http.server import BaseHTTPRequestHandler, HTTPServer
7 class HandledException(Exception):
11 def handled_error_exit(msg):
12 print(f"ERROR: {msg}")
16 def apply_booking_to_account_balances(account_sums, account, currency, amount):
17 if not account in account_sums:
18 account_sums[account] = {currency: amount}
19 elif not currency in account_sums[account].keys():
20 account_sums[account][currency] = amount
22 account_sums[account][currency] += amount
27 def __init__(self, date_string, description, booking_lines, start_line):
28 self.date_string = date_string
29 self.description = description
30 self.lines = booking_lines
31 self.start_line = start_line
32 self.validate_booking_lines()
33 self.account_changes = self.parse_booking_lines_to_account_changes()
35 def validate_booking_lines(self):
36 prefix = f"booking at line {self.start_line}"
39 for line in self.lines:
42 _, amount, currency = line
45 raise HandledException(f"{prefix} relates more than one empty value of same currency {currency}")
48 if currency not in sums:
50 sums[currency] += amount
52 for k, v in sums.items():
54 raise HandledException(f"{prefix} does not sum up to zero")
57 for k, v in sums.items():
61 raise HandledException(f"{prefix} has empty value that cannot be filled")
63 def parse_booking_lines_to_account_changes(self):
67 for line in self.lines:
70 account, amount, currency = line
72 sink_account = account
74 apply_booking_to_account_balances(account_changes, account, currency, amount)
75 if currency not in debt:
76 debt[currency] = amount
78 debt[currency] += amount
80 for currency, amount in debt.items():
81 apply_booking_to_account_balances(account_changes, sink_account, currency, -amount)
82 return account_changes
88 def __init__(self, load_from_file=True):
91 self.db_file = db_name + ".json"
92 self.lock_file = db_name+ ".lock"
95 if load_from_file and os.path.exists(self.db_file):
96 with open(self.db_file, "r") as f:
97 self.parse_lines(f.readlines())
99 def parse_lines(self, lines):
102 inside_booking = False
103 date_string, description = None, None
106 for i, line in enumerate(lines):
108 # we start with the case of an utterly empty line
109 self.comments += [""]
110 stripped_line = line.rstrip()
111 if stripped_line == '':
113 # assume we finished a booking, finalize, and commit to DB
114 if len(booking_lines) < 2:
115 raise HandledException(f"{prefix} booking ends to early")
116 booking = Booking(date_string, description, booking_lines, start_line)
117 self.bookings += [booking]
118 # expect new booking to follow so re-zeroall booking data
119 inside_booking = False
120 date_string, description = None, None
123 # if non-empty line, first get comment if any, and commit to DB
124 split_by_comment = stripped_line.split(sep=";", maxsplit=1)
125 if len(split_by_comment) == 2:
126 self.comments[i] = split_by_comment[1]
127 # 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
128 non_comment = split_by_comment[0].rstrip()
129 if non_comment.rstrip() == '':
131 booking_lines += ['']
133 # if we're starting a booking, parse by first-line pattern
134 if not inside_booking:
136 toks = non_comment.split(maxsplit=1)
137 date_string = toks[0]
139 datetime.datetime.strptime(date_string, '%Y-%m-%d')
141 raise HandledException(f"{prefix} bad date string: {date_string}")
143 description = toks[1]
145 raise HandledException(f"{prefix} bad description: {description}")
146 inside_booking = True
148 # otherwise, read as transfer data
149 toks = non_comment.split() # ignore specification's allowance of single spaces in names
151 raise HandledException(f"{prefix} too many booking line tokens: {toks}")
152 amount, currency = None, None
153 account_name = toks[0]
154 if account_name[0] == '[' and account_name[-1] == ']':
155 # ignore specification's differentiation of "virtual" accounts
156 account_name = account_name[1:-1]
157 decimal_chars = ".-0123456789"
161 amount = decimal.Decimal(toks[1])
163 except decimal.InvalidOperation:
165 amount = decimal.Decimal(toks[2])
166 except decimal.InvalidOperation:
167 raise HandledException(f"{prefix} no decimal number in: {toks[1:]}")
168 currency = toks[i_currency]
169 if currency[0] in decimal_chars:
170 raise HandledException(f"{prefix} currency starts with int, dot, or minus: {currency}")
173 inside_amount = False
174 inside_currency = False
178 for i, c in enumerate(value):
180 if c in decimal_chars:
183 inside_currency = True
185 if c in decimal_chars and len(amount_string) == 0:
186 inside_currency = False
192 if c not in decimal_chars:
193 if len(currency) > 0:
194 raise HandledException(f"{prefix} amount has non-decimal chars: {value}")
195 inside_currency = True
196 inside_amount = False
199 if c == '-' and len(amount_string) > 1:
200 raise HandledException(f"{prefix} amount has non-start '-': {value}")
203 raise HandledException(f"{prefix} amount has multiple dots: {value}")
206 if len(amount_string) == 0:
207 raise HandledException(f"{prefix} amount missing: {value}")
208 if len(currency) == 0:
209 raise HandledException(f"{prefix} currency missing: {value}")
210 amount = decimal.Decimal(amount_string)
211 booking_lines += [(account_name, amount, currency)]
213 def ledger_as_html(self):
215 for comment in self.comments:
216 lines += [f"; {comment}" if comment != '' else '']
217 for booking in self.bookings:
218 i = booking.start_line
219 suffix = f" {lines[i]}" if len(lines[i]) > 0 else ""
220 lines[i] = f"{booking.date_string} {booking.description}{suffix}"
221 for booking_line in booking.lines:
223 if booking_line == '':
225 suffix = f" {lines[i]}" if len(lines[i]) > 0 else ""
226 value = f" {booking_line[1]} {booking_line[2]}" if booking_line[1] else ""
227 lines[i] = f"{booking_line[0]}{value}{suffix}"
228 content = "\n".join(lines)
229 return f"<html><meta charset=\"UTF-8\"><pre>{content}</pre></html>"
231 def balance_as_html(self):
233 for booking in self.bookings:
234 for account, changes in booking.account_changes.items():
235 for currency, amount in changes.items():
236 apply_booking_to_account_balances(account_sums, account, currency, amount)
238 def collect_branches(account_name, path):
241 while len(path_copy) > 0:
242 step = path_copy.pop(0)
244 toks = account_name.split(":", maxsplit=1)
246 if parent in node.keys():
252 k, v = collect_branches(toks[1], path + [parent])
253 if k not in child.keys():
258 for account_name in sorted(account_sums.keys()):
259 k, v = collect_branches(account_name, [])
260 if k not in account_tree.keys():
263 account_tree[k].update(v)
264 def collect_totals(parent_path, tree_node):
265 for k, v in tree_node.items():
266 child_path = parent_path + ":" + k
267 for currency, amount in collect_totals(child_path, v).items():
268 apply_booking_to_account_balances(account_sums, parent_path, currency, amount)
269 return account_sums[parent_path]
270 for account_name in account_tree.keys():
271 account_sums[account_name] = collect_totals(account_name, account_tree[account_name])
273 def print_subtree(lines, indent, node, subtree, path):
274 line = f"{indent}{node}"
275 n_tabs = 5 - (len(line) // 8)
276 line += n_tabs * "\t"
277 if "€" in account_sums[path + node].keys():
278 amount = account_sums[path + node]["€"]
279 line += f"{amount:9.2f} €\t"
282 for currency, amount in account_sums[path + node].items():
283 if currency != '€' and amount > 0:
284 line += f"{amount:5.2f} {currency}\t"
287 for k, v in sorted(subtree.items()):
288 print_subtree(lines, indent, k, v, path + node + ":")
289 for k, v in sorted(account_tree.items()):
290 print_subtree(lines, "", k, v, "")
291 content = "\n".join(lines)
292 return f"<html><meta charset=\"UTF-8\"><pre>{content}</pre></html>"
295 class MyServer(BaseHTTPRequestHandler):
298 self.send_response(200)
299 self.send_header("Content-type", "text/html")
302 # page = db.ledger_as_html()
303 page = db.balance_as_html()
304 self.wfile.write(bytes(page, "utf-8"))
308 if __name__ == "__main__":
309 webServer = HTTPServer((hostName, serverPort), MyServer)
310 print(f"Server started http://{hostName}:{serverPort}")
312 webServer.serve_forever()
313 except KeyboardInterrupt:
315 webServer.server_close()
316 print("Server stopped.")