X-Git-Url: https://plomlompom.com/repos/unset_cookie?a=blobdiff_plain;f=ledger.py;h=412c27d44b373e5aa9beeac82ec7e90b0d7b4a24;hb=c018936a82d6caa32affe9c01af8f1d68e568a70;hp=9fa9f14a5ae7d1816175148d96b06de029d8346b;hpb=957b289c36c522e19916ef474f14a70caeab0c17;p=misc
diff --git a/ledger.py b/ledger.py
index 9fa9f14..412c27d 100755
--- a/ledger.py
+++ b/ledger.py
@@ -1,7 +1,8 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import sys
import os
-from urllib.parse import parse_qs
+import html
+from urllib.parse import parse_qs, urlparse
hostName = "localhost"
serverPort = 8082
@@ -75,6 +76,7 @@ def parse_lines(lines):
except IndexError:
raise HandledException(f"{prefix} bad description: {description}")
inside_booking = True
+ booking_lines += [non_comment]
continue
# otherwise, read as transfer data
toks = non_comment.split() # ignore specification's allowance of single spaces in names
@@ -158,7 +160,7 @@ class Booking:
prefix = f"booking at line {self.start_line}"
sums = {}
empty_values = 0
- for line in self.lines:
+ for line in self.lines[1:]:
if line == '':
continue
_, amount, currency = line
@@ -186,7 +188,7 @@ class Booking:
account_changes = {}
debt = {}
sink_account = None
- for line in self.lines:
+ for line in self.lines[1:]:
if line == '':
continue
account, amount, currency = line
@@ -248,13 +250,31 @@ class Database:
class MyServer(BaseHTTPRequestHandler):
- header = '
{booking.date_string} {booking.description}{suffix}'
- for booking_line in booking.lines:
+ for booking_line in booking.lines[1:]:
i += 1
if booking_line == '':
continue
suffix = f' {lines[i]}' if len(lines[i]) > 0 else ''
value = f' {booking_line[1]} {booking_line[2]}' if booking_line[1] else ''
lines[i] = f'{booking_line[0]}{value}{suffix}'
- lines[i] = lines[i][:-len(line_sep)] + f'
edit '
+ lines[i] = lines[i][:-len(line_sep)] + f'edit: structured / free '
return '\n'.join(lines)
- def add(self, db, start=0, end=0):
+ def add_free(self, db, start=0, end=0):
if start == end == 0:
content = ''
else:
- content = ''.join(db.real_lines[start:end])
- return f''
+ content = html.escape(''.join(db.real_lines[start:end]))
+ return f''
+
+ def add_structured(self, db, start=0, end=0, bonus_lines=10):
+ if start == end == 0:
+ lines = []
+ else:
+ lines= db.real_lines[start:end]
+ bookings, comments = parse_lines(lines)
+ if len(bookings) > 1:
+ raise HandledException('can only edit single Booking')
+ input_lines = ''
+ if len(bookings) == 0:
+ input_lines += f' ; '
+ comments = ['']
+ else:
+ booking = bookings[0]
+ if booking.start_line != 0:
+ raise HandledException('need to start on first Booking line')
+ for i, comment in enumerate(comments):
+ if i == 0:
+ safe_date_string = html.escape(booking.date_string)
+ safe_description = html.escape(booking.description)
+ safe_comment = html.escape(comment)
+ input_lines += f' ; '
+ continue
+ safe_account = safe_amount = safe_currency = ''
+ safe_comment = html.escape(comment)
+ if i < len(booking.lines):
+ main = booking.lines[i]
+ if main != '':
+ safe_account = html.escape(main[0])
+ safe_amount = '' if main[1] is None else html.escape(str(main[1]))
+ safe_currency = '' if main[2] is None else html.escape(main[2])
+ input_lines += f' ; '
+ for j in range(bonus_lines):
+ i = j + len(comments)
+ input_lines += f' ; '
+ return f''
db = Database()