From 36bcd11eb567a27e218654b56a1acfe14ea19ce3 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 25 Mar 2025 13:34:04 +0100 Subject: [PATCH] Enforce standard formatting for comment whitespacing more strongly. --- src/ledgplom/ledger.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/ledgplom/ledger.py b/src/ledgplom/ledger.py index 40081d0..f44f2da 100644 --- a/src/ledgplom/ledger.py +++ b/src/ledgplom/ledger.py @@ -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: -- 2.30.2