Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lalomartins committed Jan 8, 2024
1 parent 14c1180 commit 89a219c
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/event_server/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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)
Expand Down

0 comments on commit 89a219c

Please sign in to comment.