Skip to content

Commit

Permalink
Fix some mypy complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Jul 25, 2023
1 parent b72dacb commit 1778c5f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/zinolib/compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# mypy: ignore-errors
try:
from enum import StrEnum
except ImportError:
Expand Down
11 changes: 6 additions & 5 deletions src/zinolib/event_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta, timezone
from typing import Optional, ClassVar, List, TypeVar, Union, Dict
from typing import Optional, ClassVar, List, TypeVar, Union, Dict, Generic

from pydantic import IPvAnyAddress
from pydantic import BaseModel
Expand Down Expand Up @@ -281,19 +281,20 @@ class EventEngine:
A list of already existing events can be manipulated by an instance of
this class out of the box, but the actual IO is done by subclasses.
"""
events: Dict[str, EventType]
events: Dict[int, Event]

def __init__(self, session=None):
self.session = session
self.events = {}

def _get_event(self, event_or_id: EventOrId) -> EventType:
if isinstance(event_or_id, EventType):
def _get_event(self, event_or_id: EventOrId) -> Event:
if isinstance(event_or_id, Event):
return event_or_id
if isinstance(event_or_id, int):
return self.events[event_or_id]
raise ValueError("Unknown type")

def _set_event(self, event: EventType):
def _set_event(self, event: Event):
self.events[event.id] = event

def check_session(self):
Expand Down
34 changes: 24 additions & 10 deletions src/zinolib/zino1.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,23 @@
"""

from datetime import datetime, timezone
from typing import List
from typing import List, Dict, Union, TypedDict

from .event_types import EventType, Event, EventEngine, HistoryEntry, LogEntry


HistoryDict = TypedDict(
"HistoryDict",
{"date": datetime, "user": str, "log": str},
total=False,
)
LogDict = TypedDict(
"LogDict",
{"log": str, "date": datetime},
total=False,
)


def convert_timestamp(timestamp: int) -> datetime:
return datetime.fromtimestamp(timestamp, timezone.utc)

Expand Down Expand Up @@ -134,20 +146,22 @@ def parse_response(cls, history_data: List[str]):
..
]
"""
history_list = []
history_list: List[HistoryDict] = []
for row in history_data:
if row == " ": # end of history
continue
if row.startswith(" "): # history
history_list[-1]["log"] += row.strip()
else:
timestamp, body = row.split(" ", 1)
dt = convert_timestamp(timestamp)
entry = {"date": dt}
dt = convert_timestamp(int(timestamp))
entry: HistoryDict = {"date": dt}
if " " in body: # server generated
entry = {"log": body, "user": cls.SYSTEM_USER}
entry["user"] = cls.SYSTEM_USER
entry["log"] = body
else:
entry = {"log": "", "user": body}
entry["user"] = body
entry["log"] = ""
history_list.append(entry)

return history_list
Expand All @@ -169,7 +183,7 @@ def get_log(session, event_id: int):
return session.get_raw_log(event_id).data

@staticmethod
def parse_response(log_data: List[dict]):
def parse_response(log_data: List[str]) -> List[LogDict]:
"""
Input:
[
Expand All @@ -186,10 +200,10 @@ def parse_response(log_data: List[dict]):
..
]
"""
log_list = []
log_list: List[LogDict] = []
for row in log_data:
timestamp, log = row.split(" ", 1)
dt = convert_timestamp(timestamp)
dt = convert_timestamp(int(timestamp))
log_list.append({"date": dt, "log": log})
return log_list

Expand Down Expand Up @@ -232,5 +246,5 @@ def get_history_for_id(self, event_id: int):
def get_log_for_id(self, event_id: int):
self.check_session()
raw_log = self._log_adapter.get_log(self.session, event_id)
parsed_log = self._log_adapter.parse_log_response(raw_log)
parsed_log = self._log_adapter.parse_response(raw_log)
return LogEntry.create_list(parsed_log)

0 comments on commit 1778c5f

Please sign in to comment.