Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tools module #373

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions asusrouter/modules/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from typing import Optional

from asusrouter.modules.wlan import Wlan
from asusrouter.tools import get_enum_key_by_value
from asusrouter.tools.converters import safe_int
from asusrouter.tools.converters import get_enum_key_by_value, safe_int


class ConnectionState(IntEnum):
Expand Down Expand Up @@ -69,4 +68,4 @@ def get_connection_type(value: Optional[int]) -> ConnectionType:
# Check that it's actually an int
value = safe_int(value) or 0

return CONNECTION_TYPE.get(value, ConnectionType.WIRED)
return get_enum_key_by_value(ConnectionType, value, ConnectionType.WIRED)
10 changes: 0 additions & 10 deletions asusrouter/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
"""Tools module"""


def get_enum_key_by_value(enum, value, default=None):
"""Get the enum key by value"""

for key, enum_value in enum.__members__.items():
if enum_value.value == value:
return enum[key]

return default
19 changes: 18 additions & 1 deletion asusrouter/tools/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from datetime import datetime, timedelta, timezone
from enum import Enum
from typing import Any, Callable, Iterable, Optional, TypeVar, cast
from typing import Any, Callable, Iterable, Optional, Type, TypeVar, cast

from dateutil.parser import parse as dtparse

Expand All @@ -20,6 +20,7 @@


_T = TypeVar("_T")
_E = TypeVar("_E", bound=Enum)


def clean_input(func: Callable[..., Any]) -> Callable[..., Any]:
Expand Down Expand Up @@ -82,6 +83,22 @@ def flatten_dict(
return dict(items)


def get_enum_key_by_value(
enum: Type[_E], value: Any, default: Optional[_E] = None
) -> _E:
"""Get the enum key by value"""

if issubclass(enum, Enum):
for enum_value in enum:
if enum_value.value == value:
return enum_value

if default is not None:
return default

raise ValueError(f"Invalid value: {value}")


def handle_none_content(content: Optional[_T], default: Optional[_T]) -> Optional[_T]:
"""Return the default value if content is None, else return the content."""

Expand Down
23 changes: 23 additions & 0 deletions tests/tools/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ def test_flatten_dict():
assert converters.flatten_dict(nested_dict, exclude="b") == expected_output


class EnumForTest(Enum):
"""Enum class."""

A = 1
B = 2


def test_get_enum_key_by_value():
"""Test get_enum_key_by_value method."""

assert (
converters.get_enum_key_by_value(EnumForTest, 1, EnumForTest.B) == EnumForTest.A
)
assert (
converters.get_enum_key_by_value(EnumForTest, 2, EnumForTest.A) == EnumForTest.B
)
assert (
converters.get_enum_key_by_value(EnumForTest, 3, EnumForTest.A) == EnumForTest.A
)
with pytest.raises(ValueError):
converters.get_enum_key_by_value(EnumForTest, 3)


def test_is_enum():
"""Test is_enum method."""

Expand Down