From: Christian Heller Date: Thu, 12 Feb 2026 17:44:30 +0000 (+0100) Subject: Simplify comment instruction parsing, require zero whitespace before "#". X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/static/templates?a=commitdiff_plain;h=8af095220e5768ed88766a8c1ac90986a79af158;p=ledgplom Simplify comment instruction parsing, require zero whitespace before "#". --- diff --git a/src/ledgplom/ledger.py b/src/ledgplom/ledger.py index a400ccc..faae074 100644 --- a/src/ledgplom/ledger.py +++ b/src/ledgplom/ledger.py @@ -10,8 +10,8 @@ from typing import Any, Callable, Iterator, Optional, Self, Union SPACE = ' ' _INDENT_CHARS = {SPACE, '\t'} SEP_COMMENTS = ';' -_PREFIX_DEF = '#desc ' -_PREFIX_BAL = '#balancer ' +_PREFIX_INSTRUCTION = '#' +_ACC_MOD_INSTRUCTORS = {'description', 'balancer'} class _Wealth(): @@ -64,7 +64,7 @@ class _Wealth(): class _Account: 'Combine name, position in tree of owner, and wealth of self + children.' - desc = '' + description = '' balancer = '' def __init__(self, parent: Optional[Self], basename: str) -> None: @@ -152,10 +152,14 @@ class _DatLine: 'Either IntroLine or TransferLine instructions.' return self._into_parts()[1] + def _comment_unstripped(self) -> str: + 'Anything past ";".' + return self._into_parts()[2][1:] + @property def comment(self) -> str: 'Anything past ";", with whitespace in-between stripped.' - return self._into_parts()[2][1:].lstrip() + return self._comment_unstripped().lstrip() @property def raw(self) -> str: @@ -163,19 +167,14 @@ class _DatLine: return self._raw[:] @property - def comment_instructions(self) -> dict[str, dict[str, str]]: + def comment_instructions(self) -> Optional[tuple[str, str, str]]: 'Parse .comment into _Account modification instructions.' - instructions: dict[str, dict[str, str]] = {} - for prefix in (_PREFIX_DEF, _PREFIX_BAL): - attr_name = prefix[1:].rstrip() - instructions[attr_name] = {} - if self.comment.startswith(prefix): - parts = tuple(part.strip() for part - in self.comment[len(prefix):].split(maxsplit=1)) - if not parts: - continue - instructions[attr_name][parts[0]] = ''.join(parts[1:]) - return instructions + toks = self._comment_unstripped().split(maxsplit=2) + if len(toks) == 3 and toks[0].startswith(_PREFIX_INSTRUCTION): + instructor = toks[0][len(_PREFIX_INSTRUCTION):] + if instructor in _ACC_MOD_INSTRUCTORS: + return (toks[1], instructor, toks[2]) + return None class _BookingLine(_DatLine, ABC): @@ -653,11 +652,10 @@ class Ledger: for block in self.blocks: collect_more_than_names = id_ < 0 or block.id_ <= id_ for line in block.lines: - for attr_name, defs in line.comment_instructions.items(): - for acc_name, definition in defs.items(): - ensure_accounts(acc_name) - if collect_more_than_names and definition: - setattr(accounts[acc_name], attr_name, definition) + if (toks := line.comment_instructions): + ensure_accounts(toks[0]) + if collect_more_than_names: + setattr(accounts[toks[0]], toks[1], toks[2]) if block.booking: for acc_name, wealth in block.booking.diffs_targeted.items(): ensure_accounts(acc_name) diff --git a/src/templates/balance.html b/src/templates/balance.html index 05e9d30..56c805b 100644 --- a/src/templates/balance.html +++ b/src/templates/balance.html @@ -80,7 +80,7 @@ span.indent { {{- account.basename }} {{- ":" if account.children }} {{_()}} -{{_()}} {{ account.desc }} +{{_()}} {{ account.description }} {{_()}} {##}{% for child in account.children_euro_sorted_at(block_id) %} {{- account_with_children(block_id, child, name_indent=name_indent+1) -}} diff --git a/src/tests/full.dat b/src/tests/full.dat index 12f75ef..0eaacbf 100644 --- a/src/tests/full.dat +++ b/src/tests/full.dat @@ -1,4 +1,4 @@ -; #desc bar:x bla bla bla +;#description bar:x bla bla bla 2001-01-01 test ; foo ; in-body comment 1 @@ -22,7 +22,7 @@ bar:x:y -10 € bar:z -1 USD -; #desc bar:x bla foo bla +;#description bar:x bla foo bla 2001-01-03 test foo:x 10 € diff --git a/src/tests/full.edit_raw.0 b/src/tests/full.edit_raw.0 index e63bf9d..9f5bb76 100644 --- a/src/tests/full.edit_raw.0 +++ b/src/tests/full.edit_raw.0 @@ -82,7 +82,7 @@ td.direct_target {
- @@ -144,6 +144,7 @@ to + @@ -169,6 +170,7 @@ to + @@ -194,6 +196,7 @@ to + @@ -219,6 +222,7 @@ to + @@ -244,6 +248,7 @@ to + diff --git a/src/tests/full.edit_structured.3 b/src/tests/full.edit_structured.3 index 17c6699..f897a4d 100644 --- a/src/tests/full.edit_structured.3 +++ b/src/tests/full.edit_structured.3 @@ -143,6 +143,7 @@ to + @@ -168,6 +169,7 @@ to + @@ -193,6 +195,7 @@ to + @@ -218,6 +221,7 @@ to + diff --git a/src/tests/full.edit_structured.4 b/src/tests/full.edit_structured.4 index 1cedd9b..2316dac 100644 --- a/src/tests/full.edit_structured.4 +++ b/src/tests/full.edit_structured.4 @@ -110,7 +110,7 @@ to
Gap:
@@ -151,6 +151,7 @@ to + @@ -176,6 +177,7 @@ to + @@ -201,6 +203,7 @@ to + @@ -226,6 +229,7 @@ to + diff --git a/src/tests/full.ledger_raw b/src/tests/full.ledger_raw index 3765eb8..8cd3a0e 100644 --- a/src/tests/full.ledger_raw +++ b/src/tests/full.ledger_raw @@ -70,7 +70,7 @@ Detected redundant empty lines in gaps, e] - ; #desc bar:x bla bla bla  + ;#description bar:x bla bla bla    2001-01-01 test ; foo  ; in-body comment 1  @@ -154,7 +154,7 @@ Detected redundant empty lines in gaps, ; #desc bar:x bla bla bla  + ;#description bar:x bla bla bla    @@ -242,7 +242,7 @@ Detected redundant empty lines in gaps,   - ; #desc bar:x bla foo bla  + ;#description bar:x bla foo bla