Skip to content

Commit

Permalink
Merge pull request #104 from robberwick/type-hinting
Browse files Browse the repository at this point in the history
Add type hinting throughout project
  • Loading branch information
robberwick authored Nov 16, 2024
2 parents 32cea1a + 14ffd44 commit dde480c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 76 deletions.
8 changes: 4 additions & 4 deletions src/blinkstick/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ def control_transfer(self, bmRequestType: int, bRequest: int, wValue: int, wInde
raise NotImplementedError

@abstractmethod
def get_serial(self):
def get_serial(self) -> str:
raise NotImplementedError

@abstractmethod
def get_manufacturer(self):
def get_manufacturer(self) -> str:
raise NotImplementedError

@abstractmethod
def get_version_attribute(self):
def get_version_attribute(self) -> int:
raise NotImplementedError

@abstractmethod
def get_description(self):
def get_description(self) -> str:
raise NotImplementedError
18 changes: 9 additions & 9 deletions src/blinkstick/backends/unix_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

class UnixLikeBackend(BaseBackend):

serial: str

def __init__(self, device=None):
self.device = device
super().__init__()
if device:
self.open_device()
self.serial = self.get_serial()

def open_device(self):
def open_device(self) -> None:
if self.device is None:
raise BlinkStickException("Could not find BlinkStick...")

Expand All @@ -27,8 +29,6 @@ def open_device(self):
except usb.core.USBError as e:
raise BlinkStickException("Could not detach kernel driver: %s" % str(e))

return True

def _refresh_device(self):
if not self.serial:
return False
Expand Down Expand Up @@ -67,23 +67,23 @@ def control_transfer(self, bmRequestType: int, bRequest: int, wValue: int, wInde
def get_serial(self) -> str:
return self._usb_get_string(3)

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

def get_version_attribute(self):
return self.device.bcdDevice
def get_version_attribute(self) -> int:
return int(self.device.bcdDevice)

def get_description(self):
return self._usb_get_string(2)

def _usb_get_string(self, index):
def _usb_get_string(self, index: int) -> str:
try:
return usb.util.get_string(self.device, index, 1033)
return str(usb.util.get_string(self.device, index, 1033))
except usb.USBError:
# Could not communicate with BlinkStick backend
# attempt to find it again based on serial

if self._refresh_device():
return usb.util.get_string(self.device, index, 1033)
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))
16 changes: 8 additions & 8 deletions src/blinkstick/backends/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ def control_transfer(self, bmRequestType, bRequest, wValue, wIndex, data_or_wLen
elif bmRequestType == 0x80 | 0x20:
return self.reports[wValue - 1].get()

def get_serial(self):
return self.device.serial_number
def get_serial(self) -> str:
return str(self.device.serial_number)

def get_manufacturer(self):
return self.device.vendor_name
def get_manufacturer(self) -> str:
return str(self.device.vendor_name)

def get_version_attribute(self):
return self.device.version_number
def get_version_attribute(self) -> int:
return int(self.device.version_number)

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

0 comments on commit dde480c

Please sign in to comment.