Skip to content

Commit

Permalink
Typing cleanup.
Browse files Browse the repository at this point in the history
  - RecordingEntry is given proper type.
  - TimeInfo in RecordingEntry is given proper type.
  - Correct typing declarations during init.
  • Loading branch information
terjekv committed Dec 11, 2023
1 parent 605365c commit 70462ba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
31 changes: 16 additions & 15 deletions mreg_cli/outputmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import requests

from mreg_cli.exceptions import CliError
from mreg_cli.types import RecordingEntry, TimeInfo


# These functions are for generic output usage, but can't be in util.py
Expand Down Expand Up @@ -122,26 +123,26 @@ def __new__(cls):

def clear(self) -> None:
"""Clears the object."""
self._output: [str] = []
self._filter_re = None
self._filter_negate = False
self._command_executed = None
self._command_issued = None
self._output: List[str] = []
self._filter_re: Any = None # should be re.Pattern, but python 3.6 doesn't have that
self._filter_negate: bool = False
self._command_executed: str = None
self._command_issued: str = None

self._ok: [str] = [] # This is typically commands that went OK but returned no content
self._warnings: [str] = []
self._errors: [str] = []
self._ok: List[str] = [] # This is typically commands that went OK but returned no content
self._warnings: List[str] = []
self._errors: List[str] = []

self._api_requests: [str] = []
self._api_requests: List[str] = []

self._time_started: datetime.datetime = datetime.datetime.now()

def recording_clear(self) -> None:
"""Clears the recording data."""
self._recorded_data = []
self._recording = False
self._filename = None
self._record_timestamps = True
self._recorded_data: List[RecordingEntry] = []
self._recording: bool = False
self._filename: str = None
self._record_timestamps: bool = True

def record_timestamps(self, state: bool) -> None:
"""Set whether to record timestamps in the recording.
Expand Down Expand Up @@ -186,13 +187,13 @@ def recording_stop(self) -> None:

self.recording_clear()

def recording_entry(self) -> Dict[str, Any]:
def recording_entry(self) -> RecordingEntry:
"""Create a recording entry."""

now = datetime.datetime.now()
start = self._time_started

time: Dict[str, Union[str, int]] = {
time: TimeInfo = {
"timestamp": start.isoformat(sep=" ", timespec="seconds"),
"timestamp_as_epoch": int(start.timestamp()),
"runtime_in_ms": int((now - start).total_seconds() * 1000),
Expand Down
25 changes: 24 additions & 1 deletion mreg_cli/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List, Optional, TypedDict

if TYPE_CHECKING:
from typing import Any
Expand All @@ -22,3 +22,26 @@ def reason(self) -> str:

def json(self, **kwargs: Any) -> Any:
...


class TimeInfo(TypedDict):
"""Type definition for time-related information in the recording entry."""

timestamp: str
timestamp_as_epoch: int
runtime_in_ms: int


class RecordingEntry(TypedDict):
"""Type definition for a recording entry."""

command: str
command_filter: Optional[str]
command_filter_negate: bool
command_issued: str
ok: List[str]
warning: List[str]
error: List[str]
output: List[str]
api_requests: List[str]
time: Optional[TimeInfo]

0 comments on commit 70462ba

Please sign in to comment.