home · contact · privacy
Enforce standard formatting for comment whitespacing more strongly.
authorChristian Heller <c.heller@plomlompom.de>
Tue, 25 Mar 2025 12:34:04 +0000 (13:34 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Tue, 25 Mar 2025 12:34:04 +0000 (13:34 +0100)
src/ledgplom/ledger.py

index 40081d08fa8e91448b9155332e7a850d8f66fb94..f44f2da718a7f2ae29fb1ecca7845fd2352b18ad 100644 (file)
@@ -136,36 +136,31 @@ class DatLine(_Dictable):
             self,
             code: str,
             comment: str,
-            add_indent: bool = False,
-            raw: Optional[str] = None
+            add_indent: bool = False
             ) -> None:
         self.comment = comment
         self.code = f'{_DEFAULT_INDENT}{code}' if add_indent else code
-        if raw:
-            self.raw = raw
-        else:
-            self.raw = self.code
-            if self.comment:
-                self.raw += f' ; {self.comment}'
+        self.raw = self.code + ' ; '.join([''] + [s for s in [self.comment]
+                                                  if s])
         self.booking: Optional['_Booking'] = None
         self.booked: Optional[BookingLine] = None
 
     @classmethod
     def new_empty(cls) -> Self:
         """Create empty DatLine."""
-        return cls('', '', raw='')
+        return cls('', '')
 
     def copy_unbooked(self) -> 'DatLine':
-        """Create DatLine of .code, .comment, .raw, but no Booking ties yet."""
-        return DatLine(self.code, self.comment, raw=self.raw)
+        """Create DatLine of .code and .comment, but no Booking ties yet."""
+        return DatLine(self.code, self.comment)
 
     @classmethod
     def from_raw(cls, line: str) -> Self:
         """Parse line into new DatLine."""
         halves = [t.rstrip() for t in line.split(';', maxsplit=1)]
-        comment = halves[1] if len(halves) > 1 else ''
+        comment = halves[1].lstrip() if len(halves) > 1 else ''
         code = halves[0]
-        return cls(code, comment, raw=line)
+        return cls(code, comment)
 
     @property
     def comment_instructions(self) -> dict[str, str]:
@@ -377,12 +372,12 @@ class _Booking:
 
     @property
     def gap_lines_copied(self) -> list[DatLine]:
-        """Return new DatLines generated from .raw's of .gap_lines."""
+        """Return new DatLines generated from .gap_lines."""
         return [dat_line.copy_unbooked() for dat_line in self.gap_lines]
 
     @property
     def booked_lines_copied(self) -> list[DatLine]:
-        """Return new DatLines generated from .raw's of .booked_lines."""
+        """Return new DatLines generated from .booked_lines."""
         return [dat_line.copy_unbooked() for dat_line in self.booked_lines]
 
     @property
@@ -498,8 +493,8 @@ class Ledger:
 
     def save(self) -> None:
         """Save current state to ._path_dat."""
-        self._path_dat.write_text(
-            '\n'.join([line.raw for line in self.dat_lines]), encoding='utf8')
+        text = '\n'.join([line.raw for line in self.dat_lines])
+        self._path_dat.write_text(text, encoding='utf8')
         self.load()
 
     def _hash_dat_lines(self) -> int: