Skip to content

Commit

Permalink
Merge pull request #106 from robberwick/blacken
Browse files Browse the repository at this point in the history
Apply Black formatting to codebase
  • Loading branch information
robberwick authored Nov 17, 2024
2 parents dde480c + 7041962 commit ac20b41
Show file tree
Hide file tree
Showing 13 changed files with 703 additions and 292 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# apply Black to all files
2c4173dbae8564885ece7371e08e220a25ca8dcb
14 changes: 14 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Formatting

on: [push, pull_request]

jobs:
black:
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
steps:
- uses: actions/checkout@v4
- uses: psf/black@stable
with:
options: "--check --verbose"
src: "./src ./tests"
9 changes: 7 additions & 2 deletions src/blinkstick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from importlib.metadata import version, PackageNotFoundError

from .blinkstick import BlinkStick, BlinkStickPro, BlinkStickProMatrix
from .blinkstick import find_all, find_by_serial, find_first, get_blinkstick_package_version
from .blinkstick import (
find_all,
find_by_serial,
find_first,
get_blinkstick_package_version,
)
from .colors import Color, ColorFormat
from .constants import BlinkStickVariant
from .exceptions import BlinkStickException

try:
__version__ = version("blinkstick")
except PackageNotFoundError:
__version__ = "BlinkStick package not installed"
__version__ = "BlinkStick package not installed"
9 changes: 8 additions & 1 deletion src/blinkstick/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ def find_by_serial(serial: str) -> BaseBackend | None:
raise NotImplementedError

@abstractmethod
def control_transfer(self, bmRequestType: int, bRequest: int, wValue: int, wIndex: int, data_or_wLength: bytes | int):
def control_transfer(
self,
bmRequestType: int,
bRequest: int,
wValue: int,
wIndex: int,
data_or_wLength: bytes | int,
):
raise NotImplementedError

@abstractmethod
Expand Down
36 changes: 28 additions & 8 deletions src/blinkstick/backends/unix_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def _refresh_device(self):

@staticmethod
def find_blinksticks(find_all: bool = True):
return usb.core.find(find_all=find_all, idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
return usb.core.find(
find_all=find_all, idVendor=VENDOR_ID, idProduct=PRODUCT_ID
)

@staticmethod
def find_by_serial(serial: str) -> list | None:
Expand All @@ -51,23 +53,37 @@ def find_by_serial(serial: str) -> list | None:
except Exception as e:
print("{0}".format(e))

def control_transfer(self, bmRequestType: int, bRequest: int, wValue: int, wIndex: int,
data_or_wLength: bytes | int):
def control_transfer(
self,
bmRequestType: int,
bRequest: int,
wValue: int,
wIndex: int,
data_or_wLength: bytes | int,
):
try:
return self.device.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, data_or_wLength)
return self.device.ctrl_transfer(
bmRequestType, bRequest, wValue, wIndex, data_or_wLength
)
except usb.USBError:
# Could not communicate with BlinkStick backend
# attempt to find it again based on serial

if self._refresh_device():
return self.device.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, data_or_wLength)
return self.device.ctrl_transfer(
bmRequestType, bRequest, wValue, wIndex, data_or_wLength
)
else:
raise BlinkStickException("Could not communicate with BlinkStick {0} - it may have been removed".format(self.serial))
raise BlinkStickException(
"Could not communicate with BlinkStick {0} - it may have been removed".format(
self.serial
)
)

def get_serial(self) -> str:
return self._usb_get_string(3)

def get_manufacturer(self)-> str:
def get_manufacturer(self) -> str:
return self._usb_get_string(1)

def get_version_attribute(self) -> int:
Expand All @@ -86,4 +102,8 @@ def _usb_get_string(self, index: int) -> str:
if self._refresh_device():
return str(usb.util.get_string(self.device, index, 1033))
else:
raise BlinkStickException("Could not communicate with BlinkStick {0} - it may have been removed".format(self.serial))
raise BlinkStickException(
"Could not communicate with BlinkStick {0} - it may have been removed".format(
self.serial
)
)
33 changes: 22 additions & 11 deletions src/blinkstick/backends/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def __init__(self, device=None):
if device:
self.device.open()
self.reports = self.device.find_feature_reports()
self.serial = self.get_serial()
self.serial = self.get_serial()

@staticmethod
def find_by_serial(serial: str) -> list | None:
devices = [d for d in Win32Backend.find_blinksticks()
if d.serial_number == serial]
devices = [
d for d in Win32Backend.find_blinksticks() if d.serial_number == serial
]

if len(devices) > 0:
return devices


def _refresh_device(self):
# TODO This is weird semantics. fix up return values to be more sensible
if not self.serial:
Expand All @@ -40,27 +40,38 @@ def _refresh_device(self):

@staticmethod
def find_blinksticks(find_all: bool = True):
devices = hid.HidDeviceFilter(vendor_id =VENDOR_ID, product_id =PRODUCT_ID).get_devices()
devices = hid.HidDeviceFilter(
vendor_id=VENDOR_ID, product_id=PRODUCT_ID
).get_devices()
if find_all:
return devices
elif len(devices) > 0:
return devices[0]
else:
return None


def control_transfer(self, bmRequestType, bRequest, wValue, wIndex, data_or_wLength):
def control_transfer(
self, bmRequestType, bRequest, wValue, wIndex, data_or_wLength
):
if bmRequestType == 0x20:
if sys.version_info[0] < 3:
data = (c_ubyte * len(data_or_wLength))(*[c_ubyte(ord(c)) for c in data_or_wLength])
data = (c_ubyte * len(data_or_wLength))(
*[c_ubyte(ord(c)) for c in data_or_wLength]
)
else:
data = (c_ubyte * len(data_or_wLength))(*[c_ubyte(c) for c in data_or_wLength])
data = (c_ubyte * len(data_or_wLength))(
*[c_ubyte(c) for c in data_or_wLength]
)
data[0] = wValue
if not self.device.send_feature_report(data):
if self._refresh_device():
self.device.send_feature_report(data)
else:
raise BlinkStickException("Could not communicate with BlinkStick {0} - it may have been removed".format(self.serial))
raise BlinkStickException(
"Could not communicate with BlinkStick {0} - it may have been removed".format(
self.serial
)
)

elif bmRequestType == 0x80 | 0x20:
return self.reports[wValue - 1].get()
Expand All @@ -75,4 +86,4 @@ def get_version_attribute(self) -> int:
return int(self.device.version_number)

def get_description(self) -> str:
return str(self.device.product_name)
return str(self.device.product_name)
Loading

0 comments on commit ac20b41

Please sign in to comment.