Skip to content

feat: implement backend connection checks and add no_backend_required… #117

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

Merged
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
17 changes: 16 additions & 1 deletion src/blinkstick/clients/blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
remap_rgb_value_reverse,
ColorFormat,
)
from blinkstick.enums import BlinkStickVariant
from blinkstick.decorators import no_backend_required
from blinkstick.devices import BlinkStickDevice
from blinkstick.enums import BlinkStickVariant
from blinkstick.exceptions import NotConnected
from blinkstick.utilities import string_to_info_block_data

if sys.platform == "win32":
Expand Down Expand Up @@ -63,6 +65,19 @@ def __init__(
self.backend = USBBackend(device)
self.bs_serial = self.get_serial()

def __getattribute__(self, name):
"""Default all callables to require a backend unless they have the no_backend_required attribute"""
attr = object.__getattribute__(self, name)
if callable(attr) and not getattr(attr, "no_backend_required", False):

def wrapper(*args, **kwargs):
if self.backend is None:
raise NotConnected("No backend set")
return attr(*args, **kwargs)

return wrapper
return attr

def get_serial(self) -> str:
"""
Returns the serial number of backend.::
Expand Down
15 changes: 15 additions & 0 deletions src/blinkstick/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from functools import wraps


def no_backend_required(func):
"""no-op decorator to mark a function as requiring a backend. See BlinkStick.__getattribute__ for usage."""

func.no_backend_required = True

@wraps(func)
def wrapper(self, *args, **kwargs):
return func(self, *args, **kwargs)

return wrapper
7 changes: 7 additions & 0 deletions src/blinkstick/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
from __future__ import annotations


class BlinkStickException(Exception):
pass


class NotConnected(BlinkStickException):
pass
18 changes: 18 additions & 0 deletions tests/clients/test_blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,32 @@
from blinkstick.clients.blinkstick import BlinkStick
from pytest_mock import MockFixture

from blinkstick.exceptions import NotConnected
from tests.conftest import make_blinkstick


def test_instantiate():
"""Test that we can instantiate a BlinkStick object."""
bs = BlinkStick()
assert bs is not None


def test_all_methods_require_backend(make_blinkstick):
"""Test that all methods require a backend."""
bs = make_blinkstick()
bs.backend = None # noqa

class_methods = (
method
for method in dir(BlinkStick)
if callable(getattr(bs, method)) and not method.startswith("__")
)
for method_name in class_methods:
method = getattr(bs, method_name)
with pytest.raises(NotConnected):
method()


@pytest.mark.parametrize(
"serial, version_attribute, expected_variant, expected_variant_value",
[
Expand Down