diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-27 09:31:04 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-27 10:10:06 +0800 |
commit | 09bd022f5a713c24ab376f0d0901aba436dc151a (patch) | |
tree | 07aea89c37f1dd0927da9c56df36a7bb4e97932f /jimbrella/admin_log.py | |
parent | cbf419cb79f4a0d06c65167f81f10f503fff391d (diff) |
Separate Lockfile into new class
Diffstat (limited to 'jimbrella/admin_log.py')
-rw-r--r-- | jimbrella/admin_log.py | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/jimbrella/admin_log.py b/jimbrella/admin_log.py index 811d722..4cc9b16 100644 --- a/jimbrella/admin_log.py +++ b/jimbrella/admin_log.py @@ -1,6 +1,7 @@ import csv import os from datetime import datetime +from .lockfile import Lockfile class AdminLog: @@ -33,25 +34,7 @@ class AdminLog: def __init__(self, path: str): self.path = path - - def _lock(self) -> int: - """Create a lockfile for admin log. Operates in the sameway as `Database._lock`.""" - try: - f = open(self.path + ".lock", "x") - f.close() - return 1 - except FileExistsError: - return 0 - - def _unlock(self) -> None: - """Remove lockfile created by `_lock`. - - If there is no lockfile, simply ignore. - """ - try: - os.remove(self.path + ".lock") - except FileNotFoundError: - pass + self.lockfile = Lockfile(self.path) def _read(self) -> list: """Deserialize admin log.""" @@ -102,7 +85,7 @@ class AdminLog: `logs` is a list of log entries, to be appended at the end of the log file. """ # wait until database is locked for this write - while not self._lock(): + while not self.lockfile.lock(): continue with open(self.path, "a") as f: # append only @@ -112,9 +95,21 @@ class AdminLog: line = [event, entry["date"].isoformat()] info = [] if event in ("TAKEAWAY", "GIVEBACK", "OVERDUE"): - info = [entry["serial"], entry["tenant_name"], entry["tenant_id"], entry["tenant_phone"], entry["tenant_email"]] + info = [ + entry["serial"], + entry["tenant_name"], + entry["tenant_id"], + entry["tenant_phone"], + entry["tenant_email"], + ] elif event == "ADMIN_MODIFY_DB": - info = [entry["admin_name"], entry["serial"], entry["column"], entry["past_value"], entry["new_value"]] + info = [ + entry["admin_name"], + entry["serial"], + entry["column"], + entry["past_value"], + entry["new_value"], + ] line.extend(info) line.append(entry["note"]) @@ -122,4 +117,4 @@ class AdminLog: f.close() - self._unlock() + self.lockfile.unlock() |