Skip to content

Commit

Permalink
move helpers to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdavb committed Oct 30, 2024
1 parent 609c6be commit 6625433
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/evohomeasync2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from . import exceptions as exc
from .controlsystem import ControlSystem
from .location import Location
from .schema import SCH_FULL_CONFIG, SCH_USER_ACCOUNT
from .schema import SCH_FULL_CONFIG, SCH_USER_ACCOUNT, camel_to_snake
from .schema.const import SZ_USER_ID
from .session import AbstractTokenManager, Auth, camel_to_snake
from .session import AbstractTokenManager, Auth

if TYPE_CHECKING:
from .schema import _EvoDictT, _EvoListT, _ScheduleT
Expand Down
3 changes: 1 addition & 2 deletions src/evohomeasync2/controlsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SystemMode,
)
from .hotwater import HotWater
from .schema import SCH_TCS_STATUS
from .schema import SCH_TCS_STATUS, camel_to_snake
from .schema.const import (
SZ_ALLOWED_SYSTEM_MODES,
SZ_DHW,
Expand All @@ -41,7 +41,6 @@
SZ_ZONES,
EntityType,
)
from .session import camel_to_snake
from .zone import ActiveFaultsBase, Zone

if TYPE_CHECKING:
Expand Down
3 changes: 1 addition & 2 deletions src/evohomeasync2/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from . import exceptions as exc
from .controlsystem import ControlSystem
from .schema import SCH_GWY_STATUS
from .schema import SCH_GWY_STATUS, camel_to_snake
from .schema.const import (
SZ_GATEWAY_ID,
SZ_GATEWAY_INFO,
Expand All @@ -19,7 +19,6 @@
SZ_TEMPERATURE_CONTROL_SYSTEMS,
EntityType,
)
from .session import camel_to_snake
from .zone import ActiveFaultsBase

if TYPE_CHECKING:
Expand Down
8 changes: 6 additions & 2 deletions src/evohomeasync2/hotwater.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

from . import exceptions as exc
from .const import API_STRFTIME
from .schema import SCH_DHW_STATUS, SCH_GET_SCHEDULE_DHW, SCH_PUT_SCHEDULE_DHW
from .schema import (
SCH_DHW_STATUS,
SCH_GET_SCHEDULE_DHW,
SCH_PUT_SCHEDULE_DHW,
camel_to_snake,
)
from .schema.const import (
SZ_ALLOWED_MODES,
SZ_DHW_ID,
Expand All @@ -24,7 +29,6 @@
EntityType,
ZoneMode,
)
from .session import camel_to_snake
from .zone import _ZoneBase

if TYPE_CHECKING:
Expand Down
3 changes: 1 addition & 2 deletions src/evohomeasync2/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from . import exceptions as exc
from .gateway import Gateway
from .schema import SCH_LOCN_STATUS
from .schema import SCH_LOCN_STATUS, camel_to_snake
from .schema.const import (
SZ_COUNTRY,
SZ_GATEWAY_ID,
Expand All @@ -21,7 +21,6 @@
SZ_USE_DAYLIGHT_SAVE_SWITCHING,
EntityType,
)
from .session import camel_to_snake
from .zone import EntityBase

if TYPE_CHECKING:
Expand Down
1 change: 1 addition & 0 deletions src/evohomeasync2/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
SystemMode,
ZoneMode,
)
from .helpers import camel_to_snake, convert_keys_to_snake_case # noqa: F401
from .schedule import ( # noqa: F401
SCH_GET_SCHEDULE,
SCH_GET_SCHEDULE_DHW,
Expand Down
26 changes: 26 additions & 0 deletions src/evohomeasync2/schema/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from __future__ import annotations

import re
from typing import TypeVar


def camel_case(s: str) -> str:
"""Convert a PascalCase string to camelCase."""
Expand All @@ -12,3 +15,26 @@ def camel_case(s: str) -> str:
def pascal_case(s: str) -> str:
"""Convert a camelCase string to PascalCase."""
return s[:1].upper() + s[1:]


_T = TypeVar("_T")


def camel_to_snake(value: str) -> str:
"""Convert a camelCase string to snake_case."""
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", value)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()


assert camel_to_snake("camelCase") == "camel_case"
assert camel_to_snake("CamelCase") == "camel_case"


def convert_keys_to_snake_case(data: _T) -> _T:
if isinstance(data, list):
return [convert_keys_to_snake_case(item) for item in data] # type: ignore[return-value]

if not isinstance(data, dict):
return data

return {camel_to_snake(k): convert_keys_to_snake_case(v) for k, v in data.items()} # type: ignore[return-value]
22 changes: 2 additions & 20 deletions src/evohomeasync2/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
from __future__ import annotations

import logging
import re
from abc import ABC, abstractmethod
from datetime import datetime as dt, timedelta as td
from http import HTTPMethod, HTTPStatus
from typing import TYPE_CHECKING, Any, Final, TypedDict, TypeVar
from typing import TYPE_CHECKING, Any, Final, TypedDict

import aiohttp
import voluptuous as vol
Expand All @@ -30,6 +29,7 @@
SZ_ACCESS_TOKEN_EXPIRES,
SZ_EXPIRES_IN,
SZ_REFRESH_TOKEN,
convert_keys_to_snake_case,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -379,21 +379,3 @@ async def put(
)

return content


_T = TypeVar("_T")


def camel_to_snake(name: str) -> str:
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()


def convert_keys_to_snake_case(data: _T) -> _T:
if isinstance(data, list):
return [convert_keys_to_snake_case(item) for item in data] # type: ignore[return-value]

if not isinstance(data, dict):
return data

return {camel_to_snake(k): convert_keys_to_snake_case(v) for k, v in data.items()} # type: ignore[return-value]
2 changes: 1 addition & 1 deletion src/evohomeasync2/zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SCH_GET_SCHEDULE_ZONE,
SCH_PUT_SCHEDULE_ZONE,
SCH_ZONE_STATUS,
camel_to_snake,
convert_to_put_schedule,
)
from .schema.const import (
Expand Down Expand Up @@ -49,7 +50,6 @@
ZoneModelType,
ZoneType,
)
from .session import camel_to_snake

if TYPE_CHECKING:
import logging
Expand Down
7 changes: 5 additions & 2 deletions tests/tests/test_schemas_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import pytest

from evohomeasync2 import Location
from evohomeasync2.schema import SCH_LOCN_STATUS
from evohomeasync2.schema import (
SCH_LOCN_STATUS,
camel_to_snake,
convert_keys_to_snake_case,
)
from evohomeasync2.schema.config import SCH_TEMPERATURE_CONTROL_SYSTEM, SCH_TIME_ZONE
from evohomeasync2.schema.const import (
SZ_GATEWAY_ID,
Expand All @@ -21,7 +25,6 @@
SZ_TEMPERATURE_CONTROL_SYSTEMS,
SZ_TIME_ZONE,
)
from evohomeasync2.session import camel_to_snake, convert_keys_to_snake_case

from .helpers import TEST_DIR

Expand Down
2 changes: 1 addition & 1 deletion tests/tests_rf/test_v2_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SCH_TCS_STATUS,
SCH_USER_ACCOUNT,
SCH_ZONE_STATUS,
camel_to_snake,
)
from evohomeasync2.schema.const import (
SZ_DAILY_SCHEDULES,
Expand All @@ -35,7 +36,6 @@
SZ_USER_ID,
)
from evohomeasync2.schema.schedule import convert_to_put_schedule
from evohomeasync2.session import camel_to_snake

from . import faked_server as faked
from .conftest import _DBG_USE_REAL_AIOHTTP
Expand Down

0 comments on commit 6625433

Please sign in to comment.