From: Christian Heller Date: Mon, 20 Jan 2025 20:47:11 +0000 (+0100) Subject: Add per-line error messages. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7Bprefix%7D%7D/edit?a=commitdiff_plain;h=HEAD;p=plomledger Add per-line error messages. --- diff --git a/ledger.py b/ledger.py index 24e8311..8c2d48b 100755 --- a/ledger.py +++ b/ledger.py @@ -25,15 +25,20 @@ class DatLine: self.booking_line: Optional[BookingLine] = None @property - def type(self) -> str: - """Provide categorization of .code part.""" - return self.booking_line.type if self.booking_line else 'no_data' + def is_intro(self) -> bool: + """Return if intro line of a Booking.""" + return self.booking_line.is_intro if self.booking_line else False @property def booking_id(self) -> int: """If .booking_line, its .booking_id, else -1.""" return self.booking_line.booking_id if self.booking_line else -1 + @property + def error(self) -> str: + """Return error if registered on attempt to parse into BookingLine.""" + return self.booking_line.error if self.booking_line else '' + @property def is_empty(self) -> bool: """Return if both .code and .comment are empty.""" @@ -50,25 +55,31 @@ class DatLine: class BookingLine: """Parsed code part of a DatLine belonging to a Booking.""" - def __init__(self, booking_id: int, code: str) -> None: + def __init__(self, booking_id: int, code: str, as_intro: bool = False + ) -> None: + self.error = '' self.booking_id = booking_id - self.type = 'invalid' + self.is_intro = as_intro + if self.is_intro: + if code[0].isspace(): + self.error = 'intro line indented' + return self.acc, self.amt, self.curr = '', '', '' - if code[0].isspace(): - toks = code.lstrip().split() - self.acc = toks[0] - if 1 == len(toks): - self.type = 'value' - elif 3 == len(toks): - amt_dec = Decimal(toks[1]) - exp = amt_dec.as_tuple().exponent - assert isinstance(exp, int) - self.amt = (f'{amt_dec:.1f}…' if exp < -2 - else f'{amt_dec:.2f}') - self.curr = toks[2] - self.type = 'value' - else: - self.type = 'intro' + if not code[0].isspace(): + self.error = 'non-intro line not indented' + return + toks = code.lstrip().split() + self.acc = toks[0] + if len(toks) not in {1, 3}: + self.error = 'illegal number of tokens' + return + if 3 == len(toks): + amt_dec = Decimal(toks[1]) + exp = amt_dec.as_tuple().exponent + assert isinstance(exp, int) + self.amt = (f'{amt_dec:.1f}…' if exp < -2 + else f'{amt_dec:.2f}') + self.curr = toks[2] class Booking: @@ -78,7 +89,9 @@ class Booking: def __init__(self, id_: int, dat_lines: list[DatLine]) -> None: self.id_ = id_ self.dat_lines = dat_lines - for dat_line in self.dat_lines: + self.dat_lines[0].booking_line = BookingLine( + self.id_, self.dat_lines[0].code, as_intro=True) + for dat_line in self.dat_lines[1:]: dat_line.booking_line = BookingLine(self.id_, dat_line.code) diff --git a/templates/_base.tmpl b/templates/_base.tmpl index 2953be2..2206d51 100644 --- a/templates/_base.tmpl +++ b/templates/_base.tmpl @@ -6,7 +6,7 @@ diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl index 4df4fc6..7ba713f 100644 --- a/templates/_macros.tmpl +++ b/templates/_macros.tmpl @@ -7,36 +7,39 @@ td.curr { text-align: center; } {% macro table_dat_lines(dat_lines, single, raw) %} {% for dat_line in dat_lines %} - {% if (not (raw or single)) and dat_line.type == "intro" and loop.index > 1 %} + {% if (not (raw or single)) and dat_line.is_intro and loop.index > 1 %} {% endif %} - + {% if not single %} - {% if dat_line.type == "intro" %} + {% if dat_line.is_intro %} {% else %} {% endif %} {% endif %} {% if raw %} - {% if dat_line.type == "intro" %} + {% if dat_line.is_intro %} {% else %} {% endif %} {% else %} - {% if dat_line.type == "intro" %} - - {% elif dat_line.type == "value" %} + {% if dat_line.is_intro %} + + {% elif not dat_line.error %} {% else %} - + {% endif %} {% endif %} + {% if dat_line.error and not raw %} + + {% endif %} {% endfor %}
 
#{{dat_line.raw_nbsp}}{{dat_line.raw_nbsp}}{{dat_line.code}}{{dat_line.code}}{{dat_line.booking_line.amt}} {{dat_line.booking_line.curr|truncate(4,true,"…")}} {{dat_line.booking_line.acc}}{{dat_line.code}}{{dat_line.code}}{{dat_line.comment}}
{{dat_line.error}}
{% endmacro %}