home · contact · privacy
Simplify comment instruction parsing, require zero whitespace before "#".
authorChristian Heller <c.heller@plomlompom.de>
Thu, 12 Feb 2026 17:44:30 +0000 (18:44 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 12 Feb 2026 17:44:30 +0000 (18:44 +0100)
src/ledgplom/ledger.py
src/templates/balance.html
src/tests/full.dat
src/tests/full.edit_raw.0
src/tests/full.edit_structured.0
src/tests/full.edit_structured.3
src/tests/full.edit_structured.4
src/tests/full.ledger_raw
src/tests/full.ledger_structured

index a400cccdb8afcaa4de5346c23256b6bbdc849822..faae0744dc257b0897efa701f0e52ec2354a5bf6 100644 (file)
@@ -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)
index 05e9d30eafe1e6c1501e7c97d70edce42c0570bf..56c805b52e6d3458596a1b35a34bd585407968cc 100644 (file)
@@ -80,7 +80,7 @@ span.indent {
                {{- account.basename }}
                {{- ":" if account.children }}
 {{_()}}    </td>
-{{_()}}    <td>{{ account.desc }}</td>
+{{_()}}    <td>{{ account.description }}</td>
 {{_()}}</tr>
 {##}{% for child in account.children_euro_sorted_at(block_id) %}
       {{- account_with_children(block_id, child, name_indent=name_indent+1) -}}
index 12f75ef91ecdb2d3150891d595c7c8859d16ac50..0eaacbf393bd118e359834ca706bfde82c052edb 100644 (file)
@@ -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 €
index e63bf9da0d1afa28128fc3a9677a14fe0e274d25..9f5bb76e100a4638b37efb69ea6b7000fefbfa47 100644 (file)
@@ -82,7 +82,7 @@ td.direct_target {
 </span>
 <hr>
 
-<textarea name="raw_lines" class="tainter" cols="100" rows="9">; #desc bar:x bla bla bla
+<textarea name="raw_lines" class="tainter" cols="100" rows="9">;#description bar:x bla bla bla
 
 2001-01-01 test  ; foo
     ; in-body comment 1
index 53ef8ac561300c30fe5c7c8eaf3e99c19dbeba94..4f6ae3a4bb8aeecca1abba08ccf2e8c3988398a9 100644 (file)
@@ -103,7 +103,7 @@ to
 </span>
 <hr>
 <div>Gap:</div>
-<textarea id="gap_lines" name="raw_lines" cols="100" rows="3" class="tainter">; #desc bar:x bla bla bla
+<textarea id="gap_lines" name="raw_lines" cols="100" rows="3" class="tainter">;#description bar:x bla bla bla
 
 </textarea>
 <EXPANDED>
@@ -144,6 +144,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -169,6 +170,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -194,6 +196,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -219,6 +222,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -244,6 +248,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
index 17c6699524beaebcb1956e060b34c6e535988b09..f897a4d10e67bf83a07758eb8ee2154c8e00c881 100644 (file)
@@ -143,6 +143,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -168,6 +169,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -193,6 +195,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -218,6 +221,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
index 1cedd9b34067782f661e5ef13c4afbfe922485c9..2316dac9eb6079be586b45c1a133f883b6c3ab3a 100644 (file)
@@ -110,7 +110,7 @@ to
 <hr>
 <div>Gap:</div>
 <textarea id="gap_lines" name="raw_lines" cols="100" rows="4" class="tainter">
-; #desc bar:x bla foo bla
+;#description bar:x bla foo bla
 
 </textarea>
 <EXPANDED>
@@ -151,6 +151,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -176,6 +177,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -201,6 +203,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
@@ -226,6 +229,7 @@ to
         </td>
         <td colspan="1">
             <button type="button">add new</button>
+            <button type="button">balance</button>
             <button type="button">delete</button>
         </td>
     </tr>
index 3765eb80eeb6266a7affd7a7926fa8003ee31659..8cd3a0e93ad6bbbe5f00ed7609c8d44f2339ba02 100644 (file)
@@ -70,7 +70,7 @@ Detected redundant empty lines in gaps, <input type="submit" name="remove_redund
             [<a href="/edit_raw/0">e</a>]
         </td>
     </tr>
-    <tr><td>; #desc bar:x bla bla bla&nbsp;</td></tr>
+    <tr><td>;#description bar:x bla bla bla&nbsp;</td></tr>
     <tr><td>&nbsp;</td></tr>
     <tr><td>2001-01-01 test  ; foo&nbsp;</td></tr>
     <tr><td>    ; in-body comment 1&nbsp;</td></tr>
@@ -154,7 +154,7 @@ Detected redundant empty lines in gaps, <input type="submit" name="remove_redund
         </td>
     </tr>
     <tr><td>&nbsp;</td></tr>
-    <tr><td>; #desc bar:x bla foo bla&nbsp;</td></tr>
+    <tr><td>;#description bar:x bla foo bla&nbsp;</td></tr>
     <tr><td>&nbsp;</td></tr>
     <tr><td>2001-01-03 test&nbsp;</td></tr>
     <tr><td>    foo:x    10 €&nbsp;</td></tr>
index 676eaff4b736c9374817a0ab97e55e8f36da9ed4..6e2774f7ae849f9cdada0d1bf29d96e5adb38d83 100644 (file)
@@ -76,7 +76,7 @@ Detected redundant empty lines in gaps, <input type="submit" name="remove_redund
         </td>
     </tr>
     <tr>
-        <td colspan="4">; #desc bar:x bla bla bla&nbsp;</td>
+        <td colspan="4">;#description bar:x bla bla bla&nbsp;</td>
     </tr>
     <tr>
         <td colspan="4">&nbsp;</td>
@@ -242,7 +242,7 @@ Detected redundant empty lines in gaps, <input type="submit" name="remove_redund
         <td colspan="4">&nbsp;</td>
     </tr>
     <tr>
-        <td colspan="4">; #desc bar:x bla foo bla&nbsp;</td>
+        <td colspan="4">;#description bar:x bla foo bla&nbsp;</td>
     </tr>
     <tr>
         <td colspan="4">&nbsp;</td>