From 89a219ccd61933625cbb25d9db9671a0bcf7a585 Mon Sep 17 00:00:00 2001 From: Lalo Martins Date: Mon, 8 Jan 2024 13:04:41 +0900 Subject: [PATCH] fix lint --- src/event_server/storage.py | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/event_server/storage.py b/src/event_server/storage.py index cfaf9f0..3ab1f2b 100644 --- a/src/event_server/storage.py +++ b/src/event_server/storage.py @@ -18,7 +18,11 @@ class Storage: def __init__(self, application, account) -> None: self.application = application self.account = account - self.path = Path(f"storage/{base64.standard_b64encode(account.encode("utf-8")).decode("ascii").strip()}/{application.replace("/", "-")}") + account_partition = ( + base64.standard_b64encode(account.encode("utf-8")).decode("ascii").strip() + ) + application_partition = application.replace("/", "-") + self.path = Path(f"storage/{account_partition}/{application_partition}") if len(self.application) == 0: self.user_storage = None else: @@ -43,17 +47,22 @@ def find_event(self, id) -> Event: raise KeyError(id) def list(self, max: int = 100, since: Union[datetime, None] = None): - if len(self.application) == 0: return [] + if len(self.application) == 0: + return [] events = [] for year_partition in sorted(self.path.iterdir()): year = int(year_partition.name) - if since is not None and since.year > year: continue + if since is not None and since.year > year: + continue for day_partition in sorted(year_partition.glob("*.jsonl")): is_since_day = False if since is not None and since.year == year: month, day = map(int, day_partition.name.split(".")[0].split("-")) - if since.month > month or (since.month == month and since.day > day): continue + if since.month > month or ( + since.month == month and since.day > day + ): + continue if since.month == month and since.day == day: is_since_day = True with day_partition.open() as jf: @@ -65,14 +74,23 @@ def list(self, max: int = 100, since: Union[datetime, None] = None): if len(events) >= max: return events except ValidationError: - warn("Validation error in storage, `%s` in %s", line, day_partition, exc_info=True) + warn( + "Validation error in storage, `%s` in %s", + line, + day_partition, + exc_info=True, + ) return events def add_event(self, event: Event): event.account = self.account event.application = self.application event.synced = datetime.now() - day_partition = self.path / str(event.synced.year) / f"{event.synced.month:02}-{event.synced.day:02}.jsonl" + day_partition = ( + self.path + / str(event.synced.year) + / f"{event.synced.month:02}-{event.synced.day:02}.jsonl" + ) day_partition.parent.mkdir(parents=True, exist_ok=True) with day_partition.open("a") as jf: jf.write(event.model_dump_json()) @@ -82,7 +100,9 @@ def add_credential(self, credential: AccountCredentials): credentials_file = self.path / "credentials.json" if credentials_file.exists(): with open(credentials_file) as f: - existing_credentials = AccountCredentialsSet.model_validate_json(f.read()) + existing_credentials = AccountCredentialsSet.model_validate_json( + f.read() + ) else: existing_credentials = AccountCredentialsSet([]) existing_credentials.append(credential)