def from_device(cls, device): ret = [] for attr in ('iManufacturer', 'iProduct', 'iSerialNumber'): try: ret.append(usb_get_string(device, 255, getattr(device, attr)).strip()) except Exception as e: ret.append('Unknown') return cls(*ret)
def from_device(cls, device): ret = [] for attr in ("iManufacturer", "iProduct", "iSerialNumber"): try: ret.append(usb_get_string(device, 255, getattr(device, attr)).strip()) except Exception as e: ret.append("Unknown") return cls(*ret)
def get_string(cls, device: UsbDevice, stridx: int) -> str: """Retrieve a string from the USB device, dealing with PyUSB API breaks :param device: USB device instance :param stridx: the string identifier :return: the string read from the USB device """ if cls.UsbApi is None: import inspect args, _, _, _ = \ inspect.signature(UsbDevice.read).parameters if (len(args) >= 3) and args[1] == 'length': cls.UsbApi = 1 else: cls.UsbApi = 2 if cls.UsbApi == 2: return usb_get_string(device, stridx) return usb_get_string(device, 64, stridx)
def get_string(cls, device: UsbDevice, stridx: int) -> str: """Retrieve a string from the USB device, dealing with PyUSB API breaks :param device: USB device instance :param stridx: the string identifier :return: the string read from the USB device """ if cls.UsbApi is None: import inspect args, _, _, _ = \ inspect.signature(UsbDevice.read).parameters if (len(args) >= 3) and args[1] == 'length': cls.UsbApi = 1 else: cls.UsbApi = 2 try: if cls.UsbApi == 2: return usb_get_string(device, stridx) return usb_get_string(device, 64, stridx) except UnicodeDecodeError: # do not abort if EEPROM data is somewhat incoherent return ''