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

feat: refactor protect object subtype bucketing #18

Merged
merged 2 commits into from
Jun 10, 2024
Merged
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
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 @@
_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 @@
"""
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 @@
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

Check warning on line 256 in src/uiprotect/data/base.py

View check run for this annotation

Codecov / codecov/patch

src/uiprotect/data/base.py#L255-L256

Added lines #L255 - L256 were not covered by tests
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

Check warning on line 271 in src/uiprotect/data/base.py

View check run for this annotation

Codecov / codecov/patch

src/uiprotect/data/base.py#L270-L271

Added lines #L270 - L271 were not covered by tests
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 @@

# 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
Loading