Merge pull request #109 from qiaoccolato/main

transport: make libusb_package optional
This commit is contained in:
Lucas Abel
2023-01-18 14:48:05 -08:00
committed by GitHub
2 changed files with 20 additions and 9 deletions

View File

@@ -20,7 +20,6 @@ import logging
import threading import threading
import time import time
import libusb_package
import usb.core import usb.core
import usb.util import usb.util
from colors import color from colors import color
@@ -205,16 +204,24 @@ async def open_pyusb_transport(spec):
await self.sink.stop() await self.sink.stop()
usb.util.release_interface(self.device, 0) usb.util.release_interface(self.device, 0)
usb_find = usb.core.find
try:
import libusb_package
except ImportError:
logger.debug('libusb_package is not available')
else:
usb_find = libusb_package.find
# Find the device according to the spec moniker # Find the device according to the spec moniker
if ':' in spec: if ':' in spec:
vendor_id, product_id = spec.split(':') vendor_id, product_id = spec.split(':')
device = libusb_package.find( device = usb_find(
idVendor=int(vendor_id, 16), idProduct=int(product_id, 16) idVendor=int(vendor_id, 16), idProduct=int(product_id, 16)
) )
else: else:
device_index = int(spec) device_index = int(spec)
devices = list( devices = list(
libusb_package.find( usb_find(
find_all=1, find_all=1,
bDeviceClass=USB_DEVICE_CLASS_WIRELESS_CONTROLLER, bDeviceClass=USB_DEVICE_CLASS_WIRELESS_CONTROLLER,
bDeviceSubClass=USB_DEVICE_SUBCLASS_RF_CONTROLLER, bDeviceSubClass=USB_DEVICE_SUBCLASS_RF_CONTROLLER,

View File

@@ -22,7 +22,6 @@ import collections
import ctypes import ctypes
import platform import platform
import libusb_package
import usb1 import usb1
from colors import color from colors import color
@@ -45,11 +44,16 @@ def load_libusb():
If the library does not exists, do nothing and usb1 will search default system paths If the library does not exists, do nothing and usb1 will search default system paths
when usb1.USBContext is created. when usb1.USBContext is created.
''' '''
if libusb_path := libusb_package.get_library_path(): try:
logger.debug(f'loading libusb library at {libusb_path}') import libusb_package
dll_loader = ctypes.WinDLL if platform.system() == 'Windows' else ctypes.CDLL except ImportError:
libusb_dll = dll_loader(str(libusb_path), use_errno=True, use_last_error=True) logger.debug('libusb_package is not available')
usb1.loadLibrary(libusb_dll) else:
if libusb_path := libusb_package.get_library_path():
logger.debug(f'loading libusb library at {libusb_path}')
dll_loader = ctypes.WinDLL if platform.system() == 'Windows' else ctypes.CDLL
libusb_dll = dll_loader(str(libusb_path), use_errno=True, use_last_error=True)
usb1.loadLibrary(libusb_dll)
async def open_usb_transport(spec): async def open_usb_transport(spec):