Skip to content

Commit

Permalink
fix: circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Michsior14 committed Oct 26, 2024
1 parent dee52ae commit e1f9805
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
53 changes: 53 additions & 0 deletions custom_components/venta/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Set of utilities to work with json strings."""

from __future__ import annotations

from collections.abc import Callable
from json import JSONDecodeError, JSONDecoder, loads
from re import Pattern
from typing import Any, Generator


class RawJSONDecoder(JSONDecoder):
"""JSON decoder that stops at the first valid JSON object."""

end: int | None = None
index: int

def __init__( # noqa: PLR0913
self,
index: int,
*,
object_hook: Callable[[dict[str, Any]], Any] | None = None,
parse_float: Callable[[str], Any] | None = None,
parse_int: Callable[[str], Any] | None = None,
parse_constant: Callable[[str], Any] | None = None,
strict: bool = True,
object_pairs_hook: Callable[[list[tuple[str, Any]]], Any] | None = None,
) -> None:
"""Initialize the decoder."""
super().__init__(
object_hook=object_hook,
parse_float=parse_float,
parse_int=parse_int,
parse_constant=parse_constant,
strict=strict,
object_pairs_hook=object_pairs_hook,
)
self.index = index

def decode(self, s: str, *_: type[Pattern.match]) -> dict[str, Any]:
"""Decode the JSON string."""
data, self.__class__.end = self.raw_decode(s, self.index)
return data


def extract_json(value: str, index: int = 0) -> Generator[dict[str, Any]]:
"""Extract JSON from any string."""
while (index := value.find("{", index)) != -1:
try:
decoder = RawJSONDecoder(index)
yield loads(value, cls=decoder)
index = decoder.end
except JSONDecodeError:
index += 1
50 changes: 0 additions & 50 deletions custom_components/venta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

from __future__ import annotations

from collections.abc import Callable
from json import JSONDecodeError, JSONDecoder, loads
from re import Pattern
from typing import Any, Generator

from .venta import VentaDataUpdateCoordinator, VentaDeviceType


Expand Down Expand Up @@ -45,48 +40,3 @@ def needs_maintenance(value: int | None, max_days: int) -> bool | None:
if value is None:
return None
return venta_time_to_days_left(value, max_days) <= 0


class RawJSONDecoder(JSONDecoder):
"""JSON decoder that stops at the first valid JSON object."""

end: int | None = None
index: int

def __init__( # noqa: PLR0913
self,
index: int,
*,
object_hook: Callable[[dict[str, Any]], Any] | None = None,
parse_float: Callable[[str], Any] | None = None,
parse_int: Callable[[str], Any] | None = None,
parse_constant: Callable[[str], Any] | None = None,
strict: bool = True,
object_pairs_hook: Callable[[list[tuple[str, Any]]], Any] | None = None,
) -> None:
"""Initialize the decoder."""
super().__init__(
object_hook=object_hook,
parse_float=parse_float,
parse_int=parse_int,
parse_constant=parse_constant,
strict=strict,
object_pairs_hook=object_pairs_hook,
)
self.index = index

def decode(self, s: str, *_: type[Pattern.match]) -> dict[str, Any]:
"""Decode the JSON string."""
data, self.__class__.end = self.raw_decode(s, self.index)
return data


def extract_json(value: str, index: int = 0) -> Generator[dict[str, Any]]:
"""Extract JSON from any string."""
while (index := value.find("{", index)) != -1:
try:
decoder = RawJSONDecoder(index)
yield loads(value, cls=decoder)
index = decoder.end
except JSONDecodeError:
index += 1
4 changes: 3 additions & 1 deletion custom_components/venta/venta_strategy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Venta API strategies definitions."""

from __future__ import annotations

import logging
import select
import socket
Expand All @@ -10,7 +12,7 @@

from aiohttp import ClientSession

from .utils import extract_json
from .json import extract_json

_LOGGER = logging.getLogger(__name__)

Expand Down

0 comments on commit e1f9805

Please sign in to comment.