home · contact · privacy
Minor refactoring.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 13 Mar 2025 19:55:33 +0000 (20:55 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 13 Mar 2025 19:55:33 +0000 (20:55 +0100)
src/run.py

index f0991ab6a2772ad66098d6becf52a4288b60b398..b69e0fc52e3a7cb211f175e78d025de4b71b4999 100755 (executable)
@@ -147,18 +147,6 @@ class Account:
             rebuilt_path += (':' if rebuilt_path else '') + step_name
             yield rebuilt_path, step_name
 
-    @classmethod
-    def ensure_in_dict(cls, full_path: str, paths_to_accs: dict[str, Self]
-                       ) -> None:
-        """If full_path not key in paths_to_accs, add it with new Account."""
-        parent_path = ''
-        for path, step_name in cls.path_to_steps(full_path):
-            if path not in paths_to_accs:
-                paths_to_accs[path] = cls(
-                        paths_to_accs[parent_path] if parent_path else None,
-                        step_name)
-            parent_path = path
-
 
 class DatLine(Dictable):
     """Line of .dat file parsed into comments and machine-readable data."""
@@ -382,12 +370,6 @@ class Booking:
             return True
         return False
 
-    def apply_to_account_dict(self, acc_dict: dict[str, Account]) -> None:
-        """Update account directory with data from .account_changes."""
-        for acc_name, wealth in self.account_changes.items():
-            Account.ensure_in_dict(acc_name, acc_dict)
-            acc_dict[acc_name].add_wealth_diff(self.id_, wealth)
-
 
 class Handler(PlomHttpHandler):
     """"Handles HTTP requests."""
@@ -510,7 +492,7 @@ class Handler(PlomHttpHandler):
                     break
                 if already_registered:
                     continue
-                Account.ensure_in_dict(path, self.server.accounts)
+                self.server.ensure_account(path)
                 before = self.server.accounts[path].get_wealth(id_ - 1)
                 after = self.server.accounts[path].get_wealth(id_)
                 direct_target = full_path == path
@@ -582,11 +564,11 @@ class Server(PlomHttpServer):
             else:
                 if booked_lines:
                     booking = Booking(len(self.bookings), booked_lines[:])
-                    booking.apply_to_account_dict(self.accounts)
+                    self._apply_booking_to_accounts(booking)
                     self.bookings += [booking]
                     booked_lines.clear()
             for acc_name, desc in dat_line.comment_instructions.items():
-                Account.ensure_in_dict(acc_name, self.accounts)
+                self.ensure_account(acc_name)
                 self.accounts[acc_name].desc = desc
         self._check_date_order()
         for booking in self.bookings:
@@ -595,6 +577,21 @@ class Server(PlomHttpServer):
             booking.gap_lines = gap_lines[:-1]
         self._recalc_prev_line_empty()
 
+    def _apply_booking_to_accounts(self, booking: Booking) -> None:
+        for acc_name, wealth in booking.account_changes.items():
+            self.ensure_account(acc_name)
+            self.accounts[acc_name].add_wealth_diff(booking.id_, wealth)
+
+    def ensure_account(self, full_path: str) -> None:
+        """If full_path not in self.accounts, add its tree with Accounts."""
+        parent_path = ''
+        for path, step_name in Account.path_to_steps(full_path):
+            if path not in self.accounts:
+                self.accounts[path] = Account(
+                        self.accounts[parent_path] if parent_path else None,
+                        step_name)
+            parent_path = path
+
     def _check_date_order(self) -> None:
         last_date = ''
         err_msg = 'date < previous valid date'