Skip to content

Commit

Permalink
feat: refactor protect object subtype bucketing (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 10, 2024
1 parent c8d6c16 commit e4123ac
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions src/uiprotect/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@
from ..data.user import User

try:
from pydantic.v1.typing import DictStrAny, SetStr
from pydantic.v1.typing import DictStrAny
except ImportError:
from pydantic.typing import ( # type: ignore[assignment, no-redef]
DictStrAny,
SetStr,
)


Expand Down Expand Up @@ -88,11 +87,8 @@ class ProtectBaseObject(BaseModel):
_api: ProtectApiClient | None = PrivateAttr(None)

_protect_objs: ClassVar[dict[str, type[ProtectBaseObject]] | None] = None
_protect_objs_set: ClassVar[SetStr | None] = None
_protect_lists: ClassVar[dict[str, type[ProtectBaseObject]] | None] = None
_protect_lists_set: ClassVar[SetStr | None] = None
_protect_dicts: ClassVar[dict[str, type[ProtectBaseObject]] | None] = None
_protect_dicts_set: ClassVar[SetStr | None] = None
_to_unifi_remaps: ClassVar[DictStrAny | None] = None

class Config:
Expand Down Expand Up @@ -192,6 +188,12 @@ def _get_unifi_remaps(cls) -> dict[str, str]:
"""
return {}

@classmethod
@cache
def _get_unifi_remaps_set(self) -> set[str]:
"""Helper method to get set of all child UFP objects."""
return set(self._get_unifi_remaps())

@classmethod
def _get_to_unifi_remaps(cls) -> dict[str, str]:
"""
Expand Down Expand Up @@ -231,55 +233,49 @@ def _set_protect_subtypes(cls) -> None:
pass

@classmethod
@cache
def _get_protect_objs(cls) -> dict[str, type[ProtectBaseObject]]:
"""Helper method to get all child UFP objects"""
if cls._protect_objs is not None:
return cls._protect_objs

cls._set_protect_subtypes()
return cls._protect_objs # type: ignore[return-value]
if cls._protect_objs is None:
cls._set_protect_subtypes()
assert cls._protect_objs is not None
return cls._protect_objs

@classmethod
@cache
def _get_protect_objs_set(cls) -> set[str]:
"""Helper method to get all child UFP objects"""
if cls._protect_objs_set is None:
cls._protect_objs_set = set(cls._get_protect_objs().keys())

return cls._protect_objs_set
return set(cls._get_protect_objs())

@classmethod
@cache
def _get_protect_lists(cls) -> dict[str, type[ProtectBaseObject]]:
"""Helper method to get all child of UFP objects (lists)"""
if cls._protect_lists is not None:
return cls._protect_lists

cls._set_protect_subtypes()
return cls._protect_lists # type: ignore[return-value]
if cls._protect_lists is None:
cls._set_protect_subtypes()
assert cls._protect_lists is not None
return cls._protect_lists

@classmethod
@cache
def _get_protect_lists_set(cls) -> set[str]:
"""Helper method to get all child UFP objects"""
if cls._protect_lists_set is None:
cls._protect_lists_set = set(cls._get_protect_lists().keys())

return cls._protect_lists_set
return set(cls._get_protect_lists())

@classmethod
@cache
def _get_protect_dicts(cls) -> dict[str, type[ProtectBaseObject]]:
"""Helper method to get all child of UFP objects (dicts)"""
if cls._protect_dicts is not None:
return cls._protect_dicts

cls._set_protect_subtypes()
return cls._protect_dicts # type: ignore[return-value]
if cls._protect_dicts is None:
cls._set_protect_subtypes()
assert cls._protect_dicts is not None
return cls._protect_dicts

@classmethod
@cache
def _get_protect_dicts_set(cls) -> set[str]:
"""Helper method to get all child UFP objects"""
if cls._protect_dicts_set is None:
cls._protect_dicts_set = set(cls._get_protect_dicts().keys())

return cls._protect_dicts_set
return set(cls._get_protect_dicts())

@classmethod
def _clean_protect_obj(
Expand Down Expand Up @@ -338,7 +334,7 @@ def unifi_dict_to_dict(cls, data: dict[str, Any]) -> dict[str, Any]:

# remap keys that will not be converted correctly by snake_case convert
remaps = cls._get_unifi_remaps()
for from_key in set(remaps).intersection(data):
for from_key in cls._get_unifi_remaps_set().intersection(data):
data[remaps[from_key]] = data.pop(from_key)

# convert to snake_case and remove extra fields
Expand Down

0 comments on commit e4123ac

Please sign in to comment.