From 5b72fdfed9b6433b7f09dabafd80bde8927f1af6 Mon Sep 17 00:00:00 2001 From: Andrew Hawker Date: Tue, 8 Sep 2020 13:49:03 -0700 Subject: [PATCH 1/3] Add initial type stubs (.pyi) for package --- usb1/__init__.pyi | 545 ++++++++++++++++++++++++++++++++++++++++++++++ usb1/libusb1.pyi | 468 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 1013 insertions(+) create mode 100644 usb1/__init__.pyi create mode 100644 usb1/libusb1.pyi diff --git a/usb1/__init__.pyi b/usb1/__init__.pyi new file mode 100644 index 0000000..d499530 --- /dev/null +++ b/usb1/__init__.pyi @@ -0,0 +1,545 @@ +import collections +import threading + +from types import TracebackType +from typing import Any, Callable, Dict, FrozenSet, Iterator, List, Optional, Tuple, Type, Union + +from . import libusb1 + +Version = collections.namedtuple('Version', ['major', 'minor', 'micro', 'nano', 'rc', 'describe']) + +EVENT_CALLBACK_SET: FrozenSet[int] + +class USBError(Exception): + value: int + +USBErrorAccess: USBError +USBErrorBusy: USBError +USBErrorIO: USBError +USBErrorInterrupted: USBError +USBErrorInvalidParam: USBError +USBErrorNoDevice: USBError +USBErrorNoMem: USBError +USBErrorNotFound: USBError +USBErrorNotSupported: USBError +USBErrorOther: USBError +USBErrorOverflow: USBError +USBErrorPipe: USBError +USBErrorTimeout: USBError + +class DoomedTransferError(Exception): ... + +# Type Aliases +Buffer = Union[bytes, bytearray] +BufferList = List[Buffer] +BufferOrLength = Union[Buffer, int] +Endpoint = int +UserData = object +Timeout = int +PollTimeout = Union[float, int, None] +TransferType = int +TransferStatus = int +TransferCallback = Callable[['USBTransfer'], None] +ErrorCallback = Callable[[int], None] +FileDescriptor = int +Events = Any +FDNotifierCallback = Callable[[int, int, Any], None] +RegisterCallback = Callable[['USBContext', 'USBDevice', int], None] + +class USBTransfer: + def __init__(self, + handle: libusb1.libusb_device_handle_p_alias, + iso_packets: int, + before_submit: TransferCallback, + after_completion: TransferCallback) -> None: ... + def close(self) -> None: ... + def doom(self) -> None: ... + def __del__(self) -> None: ... + def setCallback(self, callback: TransferCallback) -> None: ... + def getCallback(self) -> TransferCallback: ... + def setControl(self, + request_type: int, + request: int, + value: int, + index: int, + buffer_or_len: BufferOrLength, + callback: Optional[TransferCallback] = ..., + user_data: Optional[UserData] = ..., + timeout: Timeout = ...) -> None: ... + def setBulk(self, + endpoint: Endpoint, + buffer_or_len: BufferOrLength, + callback: Optional[TransferCallback] = ..., + user_data: Optional[UserData] = ..., + timeout: Timeout = ...) -> None: ... + def setInterrupt(self, + endpoint: Endpoint, + buffer_or_len: BufferOrLength, + callback: Optional[TransferCallback] = ..., + user_data: Optional[UserData] = ..., + timeout: Timeout = ...) -> None: ... + def setIsochronous(self, + endpoint: Endpoint, + buffer_or_len: BufferOrLength, + callback: Optional[TransferCallback] = ..., + user_data: Optional[UserData] = ..., + timeout: Timeout = ..., + iso_transfer_length_list: Optional[List[int]] = ...) -> None: ... + def getType(self) -> TransferType: ... + def getEndpoint(self) -> Endpoint: ... + def getStatus(self) -> TransferStatus: ... + def getActualLength(self) -> int: ... + def getBuffer(self) -> Buffer: ... + def getUserData(self) -> UserData: ... + def setUserData(self, user_data: UserData) -> None: ... + def getISOBufferList(self) -> BufferList: ... + def getISOSetupList(self) -> List[Dict[str, int]]: ... + def iterISO(self) -> None: ... + def setBuffer(self, + buffer_or_len: BufferOrLength) -> None: ... + def isSubmitted(self) -> bool: ... + def submit(self) -> None: ... + def cancel(self) -> None: ... + +class USBTransferHelper: + def __init__(self, + transfer: Optional[USBTransfer] = ...) -> None: ... + def submit(self) -> None: ... + def cancel(self) -> None: ... + def setEventCallback(self, + event: int, + callback: TransferCallback) -> None: ... + def setDefaultCallback(self, + callback: TransferCallback) -> None: ... + def getEventCallback(self, + event: int, + default: Optional[TransferCallback] = ...) -> Optional[TransferCallback]: ... + def __call__(self, + transfer: USBTransfer) -> None: ... + def isSubmited(self) -> bool: ... + +class USBPollerThread(threading.Thread): + daemon: bool = ... + + def __init__(self, + context: USBContext, + poller: USBPoller, + exc_callback: Optional[ErrorCallback] = ...) -> None: ... + def stop(self) -> None: ... + @staticmethod + def exceptionHandler(exc: BaseException) -> None: ... + def run(self) -> None: ... + +class USBPoller: + def __init__(self, + context: USBContext, + poller: USBPoller) -> None: ... + def __del__(self) -> None: ... + def poll(self, + timeout: PollTimeout = ...) -> List[Tuple[int, int]]: ... + def register(self, + fd: FileDescriptor, events: Events) -> None: ... + def unregister(self, + fd: FileDescriptor) -> None: ... + +class _ReleaseInterface: + def __init__(self, + handle: USBDeviceHandle, + interface: int) -> None: ... + def __enter__(self) -> None: ... + def __exit__(self, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType]) -> None: ... + +class USBDeviceHandle: + def __init__(self, + context: USBContext, + handle: libusb1.libusb_device_handle_p_alias, + device: USBDevice) -> None: ... + def __del__(self) -> None: ... + def close(self) -> None: ... + def getDevice(self) -> USBDevice: ... + def getConfiguration(self) -> int: ... + def setConfiguration(self, + configuration: int) -> None: ... + def getManufacturer(self) -> str: ... + def getProduct(self) -> str: ... + def getSerialNumber(self) -> str: ... + def claimInterface(self, + interface: int) -> _ReleaseInterface: ... + def releaseInterface(self, + interface: int) -> None: ... + def setInterfaceAltSetting(self, + interface: int, + alt_setting: int) -> None: ... + def clearHalt(self, + endpoint: int) -> None: ... + def resetDevice(self) -> None: ... + def kernelDriverActive(self, + interface: int) -> bool: ... + def detachKernelDriver(self, + interface: int) -> None: ... + def attachKernelDriver(self, + interface: int) -> None: ... + def setAutoDetachKernelDriver(self, + enable: bool) -> None: ... + def getSupportedLanguageList(self) -> List[int]: ... + def getStringDescriptor(self, + descriptor: int, + lang_id: int, + errors: str = ...) -> str: ... + def getASCIIStringDescriptor(self, + descriptor: int, + errors: str = ...) -> str: ... + def controlWrite(self, + request_type: int, + request: int, + value: int, + index: int, + data: bytes, + timeout: int = ...) -> int: ... + def controlRead(self, + request_type: int, + request: int, + value: int, + index: int, + length: int, + timeout: int = ...) -> bytes: ... + def bulkWrite(self, + endpoint: int, + data: bytes, + timeout: int = ...) -> int: ... + def bulkRead(self, + endpoint: int, + length: int, + timeout: int = ...) -> bytes: ... + def interruptWrite(self, + endpoint: int, + data: bytes, + timeout: int = ...) -> int: ... + def interruptRead(self, + endpoint: int, + length: int, + timeout: int = ...) -> bytes: ... + def getTransfer(self, + iso_packets: int = ...) -> USBTransfer: ... + +class USBConfiguration: + def __init__(self, + context: USBContext, + config: libusb1.libusb_config_descriptor, + device_speed: int) -> None: ... + def __iter__(self) -> Iterator[USBInterface]: ... + def __getitem__(self, + interface: int) -> USBInterface: ... + def getNumInterfaces(self) -> int: ... + def getConfigurationValue(self) -> int: ... + def getDescriptor(self) -> int: ... + def getAttributes(self) -> int: ... + def getMaxPower(self) -> int: ... + def getExtra(self) -> List[bytearray]: ... + def iterInterfaces(self) -> Iterator[USBInterface]: ... + +class USBInterface: + def __init__(self, + context: USBContext, + interface: int) -> None: ... + def __len__(self)-> int: ... + def __iter__(self) -> Iterator[USBInterfaceSetting]: ... + def __getitem__(self, + alt_setting: int) -> USBInterfaceSetting: ... + def getNumSettings(self) -> int: ... + def iterSettings(self) -> Iterator[USBInterfaceSetting]: ... + +class USBInterfaceSetting: + def __init__(self, + context: USBContext, + alt_setting: libusb1.libusb_interface_descriptor_p_alias) -> None: ... + def __iter__(self) -> Iterator[USBEndpoint]: ... + def __len__(self) -> int: ... + def __getitem__(self, + endpoint: int) -> USBEndpoint: ... + def getNumber(self) -> int: ... + def getAlternateSetting(self) -> int: ... + def getNumEndpoints(self) -> int: ... + def getClass(self) -> int: ... + def getSubClass(self) -> int: ... + def getClassTuple(self) -> Tuple[int, int]: ... + def getClassTupple(self) -> Tuple[int, int]: ... + def getProtocol(self) -> int: ... + def getDescriptor(self) -> int: ... + def getExtra(self) -> List[bytearray]: ... + def iterEndpoints(self) -> Iterator[USBEndpoint]: ... + +class USBEndpoint: + def __init__(self, + context: USBContext, + endpoint: libusb1.libusb_endpoint_descriptor_p_alias) -> None: ... + def getAddress(self) -> int: ... + def getAttributes(self) -> int: ... + def getMaxPacketSize(self) -> int: ... + def getInterval(self) -> int: ... + def getRefresh(self) -> int: ... + def getSyncAddress(self) -> int: ... + def getExtra(self) -> List[bytearray]: ... + +class USBDevice: + device_p: libusb1.libusb_device_p_alias = ... + device_descriptor: Any = ... + + def __init__(self, + context: USBContext, + device_p: libusb1.libusb_device_p_alias, + can_load_configuration: bool = ...) -> None: ... + def __del__(self) -> None: ... + def __len__(self) -> int: ... + def __getitem__(self, + index: int) -> USBConfiguration: ... + def __hash__(self) -> int: ... + def __eq__(self, + other: Any) -> bool: ... + def close(self) -> None: ... + def iterConfigurations(self) -> Iterator[USBConfiguration]: ... + def iterConfiguations(self) -> Iterator[USBConfiguration]: ... + def iterSettings(self) -> Iterator[USBInterfaceSetting]: ... + def getBusNumber(self) -> int: ... + def getPortNumber(self) -> int: ... + def getPortNumberList(self) -> List[int]: ... + def getDeviceAddress(self) -> int: ... + def getbcdUSB(self) -> int: ... + def getDeviceClass(self) -> int: ... + def getDeviceSubClass(self) -> int: ... + def getDeviceProtocol(self) -> int: ... + def getMaxPacketSize0(self) -> int: ... + def getMaxPacketSize(self, + endpoint: int) -> int: ... + def getMaxISOPacketSize(self, + endpoint: int) -> int: ... + def getVendorID(self) -> int: ... + def getProductID(self) -> int: ... + def getbcdDevice(self) -> int: ... + def getSupportedLanguageList(self) -> List[int]: ... + def getManufacturer(self) -> int: ... + def getManufacturerDescriptor(self) -> str: ... + def getProduct(self) -> str: ... + def getProductDescriptor(self) -> int: ... + def getSerialNumber(self) -> str: ... + def getSerialNumberDescriptor(self) -> int: ... + def getNumConfigurations(self) -> int: ... + def getDeviceSpeed(self) -> int: ... + def open(self) -> USBDeviceHandle: ... + +class USBContext: + def __init__(self) -> None: ... + def __del__(self) -> None: ... + def __enter__(self) -> USBContext: ... + def __exit__(self, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType]) -> None: ... + def open(self) -> USBContext: ... + def close(self) -> None: ... + def exit(self) -> None: ... + def getDeviceIterator(self, + skip_on_error: bool = ...) -> Iterator[USBDevice]: ... + def getDeviceList(self, + skip_on_access_error: bool = ..., + skip_on_error: bool = ...) -> List[USBDevice]: ... + def getByVendorIDAndProductID(self, + vendor_id: Any, + product_id: Any, + skip_on_access_error: bool = ..., + skip_on_error: bool = ...) -> Optional[USBDevice]: ... + def openByVendorIDAndProductID(self, + vendor_id: Any, + product_id: Any, + skip_on_access_error: bool = ..., + skip_on_error: bool = ...) -> Optional[USBDeviceHandle]: ... + def getPollFDList(self) -> Tuple[int, int]: ... + def handleEvents(self) -> None: ... + def handleEventsTimeout(self, + tv: int = ...) -> None: ... + def setPollFDNotifiers(self, + added_cb: Optional[FDNotifierCallback] = ..., + removed_cb: Optional[FDNotifierCallback] = ..., + user_data: Optional[UserData] = ...) -> None: ... + def getNextTimeout(self) -> int: ... + def setDebug(self, + level: int) -> None: ... + def tryLockEvents(self) -> int: ... + def lockEvents(self) -> None: ... + def lockEventWaiters(self) -> None: ... + def waitForEvent(self, + tv: int = ...) -> None: ... + def unlockEventWaiters(self) -> None: ... + def eventHandlingOK(self) -> int: ... + def unlockEvents(self) -> None: ... + def handleEventsLocked(self) -> None: ... + def eventHandlerActive(self) -> int: ... + @staticmethod + def hasCapability(capability: int) -> int: ... + def hotplugRegisterCallback(self, + callback: RegisterCallback, + events: int = ..., + flags: int = ..., + vendor_id: int = ..., + product_id: int = ..., + dev_class: int = ...) -> int: ... + def hotplugDeregisterCallback(self, + handle: int) -> None: ... + +def getVersion() -> Version: ... +def hasCapability(capability: int) -> int: ... + +class LibUSBContext(USBContext): + def __init__(self) -> None: ... + +# Enums +CLASS_PER_INTERFACE: int +CLASS_AUDIO: int +CLASS_COMM: int +CLASS_HID: int +CLASS_PHYSICAL: int +CLASS_PTP: int +CLASS_IMAGE = libusb1 +CLASS_PRINTER: int +CLASS_MASS_STORAGE: int +CLASS_HUB: int +CLASS_DATA: int +CLASS_SMART_CARD: int +CLASS_CONTENT_SECURITY: int +CLASS_VIDEO: int +CLASS_PERSONAL_HEALTHCARE: int +CLASS_DIAGNOSTIC_DEVICE: int +CLASS_WIRELESS: int +CLASS_APPLICATION: int +CLASS_VENDOR_SPEC: int + +DT_DEVICE: int +DT_CONFIG: int +DT_STRING: int +DT_INTERFACE: int +DT_ENDPOINT: int +DT_HID: int +DT_REPORT: int +DT_PHYSICAL: int +DT_HUB: int + +DT_DEVICE_SIZE: int +DT_CONFIG_SIZE: int +DT_INTERFACE_SIZE: int +DT_ENDPOINT_SIZE: int +DT_ENDPOINT_AUDIO_SIZE: int +DT_HUB_NONVAR_SIZE: int + +ENDPOINT_ADDRESS_MASK: int +USB_ENDPOINT_ADDRESS_MASK: int +ENDPOINT_DIR_MASK: int +USB_ENDPOINT_DIR_MASK: int + +ENDPOINT_IN: int +ENDPOINT_OUT: int + +TRANSFER_TYPE_MASK: int + +TRANSFER_TYPE_CONTROL: int +TRANSFER_TYPE_ISOCHRONOUS: int +TRANSFER_TYPE_BULK: int +TRANSFER_TYPE_INTERRUPT: int + +REQUEST_GET_STATUS: int +REQUEST_CLEAR_FEATURE: int +REQUEST_SET_FEATURE: int +REQUEST_SET_ADDRESS: int +REQUEST_GET_DESCRIPTOR: int +REQUEST_SET_DESCRIPTOR: int +REQUEST_GET_CONFIGURATION: int +REQUEST_SET_CONFIGURATION: int +REQUEST_GET_INTERFACE: int +REQUEST_SET_INTERFACE: int +REQUEST_SYNCH_FRAME: int + +REQUEST_TYPE_STANDARD: int +REQUEST_TYPE_CLASS: int +REQUEST_TYPE_VENDOR: int +REQUEST_TYPE_RESERVED: int +TYPE_STANDARD: int +TYPE_CLASS: int +TYPE_VENDOR: int +TYPE_RESERVED: int + +RECIPIENT_DEVICE: int +RECIPIENT_INTERFACE: int +RECIPIENT_ENDPOINT: int +RECIPIENT_OTHER: int + +ISO_SYNC_TYPE_MASK: int + +ISO_SYNC_TYPE_NONE: int +ISO_SYNC_TYPE_ASYNC: int +ISO_SYNC_TYPE_ADAPTIVE: int +ISO_SYNC_TYPE_SYNC: int + +ISO_USAGE_TYPE_MASK: int + +ISO_USAGE_TYPE_DATA: int +ISO_USAGE_TYPE_FEEDBACK: int +ISO_USAGE_TYPE_IMPLICIT: int + +CONTROL_SETUP_SIZE: int + +SPEED_UNKNOWN: int +SPEED_LOW: int +SPEED_FULL: int +SPEED_HIGH: int +SPEED_SUPER: int + +LOW_SPEED_OPERATION: int +FULL_SPEED_OPERATION: int +HIGH_SPEED_OPERATION: int +SUPER_SPEED_OPERATION: int + +SUCCESS: int +ERROR_IO: int +ERROR_INVALID_PARAM: int +ERROR_ACCESS: int +ERROR_NO_DEVICE: int +ERROR_NOT_FOUND: int +ERROR_BUSY: int +ERROR_TIMEOUT: int +ERROR_OVERFLOW: int +ERROR_PIPE: int +ERROR_INTERRUPTED: int +ERROR_NO_MEM: int +ERROR_NOT_SUPPORTED: int +ERROR_OTHER: int + +TRANSFER_COMPLETED: int +TRANSFER_ERROR: int +TRANSFER_TIMED_OUT: int +TRANSFER_CANCELLED: int +TRANSFER_STALL: int +TRANSFER_NO_DEVICE: int +TRANSFER_OVERFLOW: int + +TRANSFER_SHORT_NOT_OK: int +TRANSFER_FREE_BUFFER: int +TRANSFER_FREE_TRANSFER: int +TRANSFER_ADD_ZERO_PACKET: int + +CAP_HAS_CAPABILITY: int +CAP_HAS_HOTPLUG: int +CAP_HAS_HID_ACCESS: int +CAP_SUPPORTS_DETACH_KERNEL_DRIVER: int + +LOG_LEVEL_NONE: int +LOG_LEVEL_ERROR: int +LOG_LEVEL_WARNING: int +LOG_LEVEL_INFO: int +LOG_LEVEL_DEBUG: int + +HOTPLUG_EVENT_DEVICE_ARRIVED: int +HOTPLUG_EVENT_DEVICE_LEFT: int + +HOTPLUG_MATCH_ANY: int diff --git a/usb1/libusb1.pyi b/usb1/libusb1.pyi new file mode 100644 index 0000000..7d82616 --- /dev/null +++ b/usb1/libusb1.pyi @@ -0,0 +1,468 @@ +from ctypes import (Structure, c_int, c_uint8, pointer, c_uint32, c_char_p, c_ssize_t, c_uint16, c_uint, c_void_p, + py_object, LittleEndianStructure, CFUNCTYPE) +from typing import Any, Dict, List, Optional, Union + +class Enum: + forward_dict: Dict[str, int] = ... + reverse_dict: Dict[int, str] = ... + def __init__(self, + member_dict: Dict[str, int], + scope_dict: Dict[str, Any] = ...) -> None: ... + def __call__(self, + value: int) -> str: ... + def get(self, + value: int, + default: Optional[str] = ...) -> str: ... + +def buffer_at(address: c_int, + length: c_int) -> bytearray: ... +def newStruct(field_name_list: List[str]) -> LittleEndianStructure: ... +def newDescriptor(field_name_list: List[str]) -> LittleEndianStructure: ... + +class USBError(Exception): + value: Any = ... + def __init__(self, value: Optional[Any] = ...) -> None: ... + +def bswap16(x: int) -> int: ... +def libusb_cpu_to_le16(x: int) -> int: ... +def libusb_le16_to_cpu(x: int) -> int: ... + +# Enums +libusb_class_code: Enum +LIBUSB_CLASS_PER_INTERFACE: int +LIBUSB_CLASS_AUDIO: int +LIBUSB_CLASS_COMM: int +LIBUSB_CLASS_HID: int +LIBUSB_CLASS_PHYSICAL: int +LIBUSB_CLASS_PTP: int +LIBUSB_CLASS_IMAGE: int +LIBUSB_CLASS_PRINTER: int +LIBUSB_CLASS_MASS_STORAGE: int +LIBUSB_CLASS_HUB: int +LIBUSB_CLASS_DATA: int +LIBUSB_CLASS_SMART_CARD: int +LIBUSB_CLASS_CONTENT_SECURITY: int +LIBUSB_CLASS_VIDEO: int +LIBUSB_CLASS_PERSONAL_HEALTHCARE: int +LIBUSB_CLASS_DIAGNOSTIC_DEVICE: int +LIBUSB_CLASS_WIRELESS: int +LIBUSB_CLASS_APPLICATION: int +LIBUSB_CLASS_VENDOR_SPEC: int + +libusb_descriptor_type: Enum +LIBUSB_DT_DEVICE: int +LIBUSB_DT_CONFIG: int +LIBUSB_DT_STRING: int +LIBUSB_DT_INTERFACE: int +LIBUSB_DT_ENDPOINT: int +LIBUSB_DT_HID: int +LIBUSB_DT_REPORT: int +LIBUSB_DT_PHYSICAL: int +LIBUSB_DT_HUB: int + +LIBUSB_DT_DEVICE_SIZE: int +LIBUSB_DT_CONFIG_SIZE: int +LIBUSB_DT_INTERFACE_SIZE: int +LIBUSB_DT_ENDPOINT_SIZE: int +LIBUSB_DT_ENDPOINT_AUDIO_SIZE: int +LIBUSB_DT_HUB_NONVAR_SIZE: int + +LIBUSB_ENDPOINT_ADDRESS_MASK: int +USB_ENDPOINT_ADDRESS_MASK: int +LIBUSB_ENDPOINT_DIR_MASK: int +USB_ENDPOINT_DIR_MASK: int + +libusb_endpoint_direction: Enum +LIBUSB_ENDPOINT_IN: int +LIBUSB_ENDPOINT_OUT: int + +LIBUSB_TRANSFER_TYPE_MASK: int + +libusb_transfer_type: Enum +LIBUSB_TRANSFER_TYPE_CONTROL: int +LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: int +LIBUSB_TRANSFER_TYPE_BULK: int +LIBUSB_TRANSFER_TYPE_INTERRUPT: int + +libusb_standard_request: Enum +LIBUSB_REQUEST_GET_STATUS: int +LIBUSB_REQUEST_CLEAR_FEATURE: int +LIBUSB_REQUEST_SET_FEATURE: int +LIBUSB_REQUEST_SET_ADDRESS: int +LIBUSB_REQUEST_GET_DESCRIPTOR: int +LIBUSB_REQUEST_SET_DESCRIPTOR: int +LIBUSB_REQUEST_GET_CONFIGURATION: int +LIBUSB_REQUEST_SET_CONFIGURATION: int +LIBUSB_REQUEST_GET_INTERFACE: int +LIBUSB_REQUEST_SET_INTERFACE: int +LIBUSB_REQUEST_SYNCH_FRAME: int + +libusb_request_type: Enum +LIBUSB_REQUEST_TYPE_STANDARD: int +LIBUSB_TYPE_STANDARD: int +LIBUSB_REQUEST_TYPE_CLASS: int +LIBUSB_TYPE_CLASS: int +LIBUSB_REQUEST_TYPE_VENDOR: int +LIBUSB_TYPE_VENDOR: int +LIBUSB_REQUEST_TYPE_RESERVED: int +LIBUSB_TYPE_RESERVED: int + +libusb_request_recipient: Enum +LIBUSB_RECIPIENT_DEVICE: int +LIBUSB_RECIPIENT_INTERFACE: int +LIBUSB_RECIPIENT_ENDPOINT: int +LIBUSB_RECIPIENT_OTHER: int + +LIBUSB_ISO_SYNC_TYPE_MASK: int + +libusb_iso_sync_type: Enum +LIBUSB_ISO_SYNC_TYPE_NONE: int +LIBUSB_ISO_SYNC_TYPE_ASYNC: int +LIBUSB_ISO_SYNC_TYPE_ADAPTIVE: int +LIBUSB_ISO_SYNC_TYPE_SYNC: int + +LIBUSB_ISO_USAGE_TYPE_MASK: int + +libusb_iso_usage_type: Enum +LIBUSB_ISO_USAGE_TYPE_DATA: int +LIBUSB_ISO_USAGE_TYPE_FEEDBACK: int +LIBUSB_ISO_USAGE_TYPE_IMPLICIT: int + +LIBUSB_CONTROL_SETUP_SIZE: int + +libusb_speed: Enum +LIBUSB_SPEED_UNKNOWN: int +LIBUSB_SPEED_LOW: int +LIBUSB_SPEED_FULL: int +LIBUSB_SPEED_HIGH: int +LIBUSB_SPEED_SUPER: int + +libusb_supported_speed: Enum +LIBUSB_LOW_SPEED_OPERATION: int +LIBUSB_FULL_SPEED_OPERATION: int +LIBUSB_HIGH_SPEED_OPERATION: int +LIBUSB_5GBPS_OPERATION: int + +libusb_error: Enum +LIBUSB_SUCCESS: int +LIBUSB_ERROR_IO: int +LIBUSB_ERROR_INVALID_PARAM: int +LIBUSB_ERROR_ACCESS: int +LIBUSB_ERROR_NO_DEVICE: int +LIBUSB_ERROR_NOT_FOUND: int +LIBUSB_ERROR_BUSY: int +LIBUSB_ERROR_TIMEOUT: int +LIBUSB_ERROR_OVERFLOW: int +LIBUSB_ERROR_PIPE: int +LIBUSB_ERROR_INTERRUPTED: int +LIBUSB_ERROR_NO_MEM: int +LIBUSB_ERROR_NOT_SUPPORTED: int +LIBUSB_ERROR_OTHER: int + +libusb_transfer_status: Enum +LIBUSB_TRANSFER_COMPLETED: int +LIBUSB_TRANSFER_ERROR: int +LIBUSB_TRANSFER_TIMED_OUT: int +LIBUSB_TRANSFER_CANCELLED: int +LIBUSB_TRANSFER_STALL: int +LIBUSB_TRANSFER_NO_DEVICE: int +LIBUSB_TRANSFER_OVERFLOW: int + +libusb_transfer_flags: Enum +LIBUSB_TRANSFER_SHORT_NOT_OK: int +LIBUSB_TRANSFER_FREE_BUFFER: int +LIBUSB_TRANSFER_FREE_TRANSFER: int +LIBUSB_TRANSFER_ADD_ZERO_PACKET: int + +libusb_capability: Enum +LIBUSB_CAP_HAS_CAPABILITY: int +LIBUSB_CAP_HAS_HOTPLUG: int +LIBUSB_CAP_HAS_HID_ACCESS: int +LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER: int + +libusb_log_level: Enum +LIBUSB_LOG_LEVEL_NONE: int +LIBUSB_LOG_LEVEL_ERROR: int +LIBUSB_LOG_LEVEL_WARNING: int +LIBUSB_LOG_LEVEL_INFO: int +LIBUSB_LOG_LEVEL_DEBUG: int + +libusb_hotplug_flag: Enum +LIBUSB_HOTPLUG_ENUMERATE: int + +libusb_hotplug_event: Enum +LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: int +LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: int + +LIBUSB_HOTPLUG_MATCH_ANY: int + +# Structures +class timeval(Structure): ... +class libusb_device_descriptor(Structure): ... +class libusb_endpoint_descriptor(Structure): ... +class libusb_interface_descriptor(Structure): ... +class libusb_interface(Structure): ... +class libusb_config_descriptor(Structure): ... +class libusb_control_setup(Structure): ... +class libusb_context(Structure): ... +class libusb_device(Structure): ... +class libusb_device_handle(Structure): ... +class libusb_version(Structure): ... +class libusb_iso_packet_descriptor(Structure): ... +class libusb_transfer(Structure): ... +class libusb_pollfd(Structure): ... + +# Type Aliases +c_uchar = c_uint8 +c_int_p_alias = pointer[c_int] + +timeval_p_alias = pointer[timeval] +libusb_device_descriptor_p_alias = pointer[libusb_device_descriptor] +libusb_endpoint_descriptor_p_alias = pointer[libusb_endpoint_descriptor] +libusb_interface_descriptor_p_alias = pointer[libusb_interface_descriptor] +libusb_interface_p_alias = pointer[libusb_interface] +libusb_config_descriptor_p_alias = pointer[libusb_config_descriptor] +libusb_config_descriptor_p_p_alias = pointer[libusb_config_descriptor_p_alias] +libusb_control_setup_p_alias = pointer[libusb_control_setup] +libusb_context_p_alias = pointer[libusb_context] +libusb_context_p_p_alias = pointer[libusb_context_p_alias] +libusb_device_p_alias = pointer[libusb_device] +libusb_device_p_p_alias = pointer[libusb_device_p_alias] +libusb_device_p_p_p_alias = pointer[libusb_device_p_p_alias] +libusb_device_handle_p_alias = pointer[libusb_device_handle] +libusb_device_handle_p_p_alias = pointer[libusb_device_handle_p_alias] +libusb_version_p_alias = pointer[libusb_version] +libusb_iso_packet_descriptor_p_alias = pointer[libusb_iso_packet_descriptor] +libusb_transfer_p_alias = pointer[libusb_transfer] +libusb_transfer_cb_fn_p_alias = Any # TODO +libusb_pollfd_p_alias = pointer[libusb_pollfd] +libusb_pollfd_p_p_alias = pointer[libusb_pollfd_p_alias] +libusb_pollfd_added_cb_p_alias = Any # TODO +libusb_pollfd_removed_cb_p_alias = Any # TODO +libusb_hotplug_callback_handle_alias = c_int +libusb_hotplug_callback_handle_p_alias = pointer[libusb_hotplug_callback_handle_alias] +libusb_hotplug_callback_fn_p_alias = Any # TODO + +libusb_descriptor_alias = Union[libusb_config_descriptor_p_alias, + libusb_endpoint_descriptor_p_alias, + libusb_interface_descriptor_p_alias] + +# Type Variables +c_int_p: pointer[c_int] + +timeval_p: pointer[timeval] +libusb_device_descriptor_p: libusb_device_descriptor_p_alias +libusb_endpoint_descriptor_p: libusb_endpoint_descriptor_p_alias +libusb_interface_descriptor_p:libusb_interface_descriptor_p_alias +libusb_interface_p: libusb_interface_p_alias +libusb_config_descriptor_p: libusb_interface_p_alias +libusb_config_descriptor_p_p: pointer[libusb_interface_p_alias] +libusb_control_setup_p: libusb_control_setup_p_alias +libusb_context_p: libusb_context_p_alias +libusb_context_p_p: libusb_context_p_p_alias +libusb_device_p: libusb_device_p_alias +libusb_device_p_p: libusb_device_p_p_alias +libusb_device_p_p_p: libusb_device_p_p_p_alias +libusb_device_handle_p: libusb_device_handle_p_alias +libusb_device_handle_p_p: libusb_device_handle_p_p_alias +libusb_iso_packet_descriptor_p: libusb_iso_packet_descriptor_p_alias +libusb_transfer_p: libusb_transfer_p_alias +libusb_transfer_cb_fn_p: libusb_transfer_cb_fn_p_alias +libusb_pollfd_p: libusb_pollfd_p_alias +libusb_pollfd_p_p: libusb_pollfd_p_p_alias +libusb_pollfd_added_cb_p: libusb_pollfd_added_cb_p_alias +libusb_pollfd_removed_cb_p: libusb_pollfd_removed_cb_p_alias +libusb_hotplug_callback_handle: libusb_hotplug_callback_handle_alias +libusb_hotplug_callback_fn_p: libusb_hotplug_callback_fn_p_alias + + +# Functions +def libusb_init(ctx: libusb_context_p_alias) -> None: ... +def libusb_exit(ctx: libusb_context_p_alias) -> None: ... +def libusb_set_debug(ctx: libusb_context_p_alias, + level: c_int) -> None: ... +def libusb_get_version() -> libusb_version_p_alias: ... +def libusb_has_capability(capability: c_uint32) -> c_int: ... +def libusb_error_name(errcode: c_int) -> c_char_p: ... +def libusb_strerror(errcode: c_int) -> None: ... +def libusb_get_device_list(ctx: libusb_context_p_alias, + list: libusb_device_p_p_p_alias) -> c_ssize_t: ... +def libusb_free_device_list(list: libusb_device_p_p_alias, + unref_devices: c_int) -> None: ... +def libusb_ref_device(dev: libusb_device_p_alias) -> libusb_device_p_alias: ... +def libusb_unref_device(dev: libusb_device_p_alias) -> None: ... +def libusb_get_configuration(dev: libusb_device_handle_p_alias, + config: pointer[c_int]) -> c_int: ... +def libusb_get_device_descriptor(dev: libusb_device_p_alias, + desc: libusb_device_descriptor_p_alias) -> c_int: ... +def libusb_get_active_config_descriptor(dev: libusb_device_p_alias, + config: libusb_config_descriptor_p_alias) -> c_int: ... +def libusb_get_config_descriptor(dev: libusb_device_p_alias, + config_index: c_uint8, + config: libusb_config_descriptor_p_p_alias) -> None: ... +def libusb_get_config_descriptor_by_value(dev: libusb_device_p_alias, + bConfigurationValue: c_uint8, + config: libusb_config_descriptor_p_p_alias) -> c_int: ... +def libusb_free_config_descriptor(config: libusb_config_descriptor_p_alias) -> None: ... +def libusb_get_bus_number(dev: libusb_device_p_alias) -> c_uint8: ... +def libusb_get_port_number(dev: libusb_device_p_alias) -> c_uint8: ... +def libusb_get_port_numbers(dev: libusb_device_p_alias, + port_numbers: pointer[c_uint8], + port_numbers_len: c_int) -> c_int: ... +def libusb_get_parent(dev: libusb_device_p_alias) -> libusb_device_p_alias: ... +def libusb_get_device_address(dev: libusb_device_p_alias) -> c_uint8: ... +def libusb_get_device_speed(dev: libusb_device_p_alias) -> c_int: ... +def libusb_get_max_packet_size(dev: libusb_device_p_alias, + endpoint: c_uint8) -> c_int: ... +def libusb_get_max_iso_packet_size(dev: libusb_device_p_alias, + endpoint: c_uint8) -> c_int: ... +def libusb_open(dev: libusb_device_p_alias, + handle: libusb_device_handle_p_p_alias) -> c_int: ... +def libusb_close(dev: libusb_device_p_alias) -> None: ... +def libusb_get_device(dev_handle: libusb_device_handle_p_alias) -> libusb_device_p_alias: ... +def libusb_set_configuration(dev: libusb_device_handle_p_alias, + configuration: c_int) -> c_int: ... +def libusb_claim_interface(dev: libusb_device_handle_p_alias, + iface: c_int) -> c_int: ... +def libusb_release_interface(dev: libusb_device_handle_p_alias, + iface: c_int) -> c_int: ... +def libusb_open_device_with_vid_pid(ctx: libusb_context_p_alias, + vendor_id: c_uint16, + product_id: c_uint16) -> libusb_device_handle_p_alias: ... +def libusb_set_interface_alt_setting(dev: libusb_device_handle_p_alias, + interface_number: c_int, + alternate_setting: c_int) -> c_int: ... +def libusb_clear_halt(dev: libusb_device_handle_p_alias, + endpoint: c_uchar) -> c_int: ... +def libusb_reset_device(dev: libusb_device_handle_p_alias) -> c_int: ... +def libusb_kernel_driver_active(dev: libusb_device_handle_p_alias, + interface: c_int) -> c_int: ... +def libusb_detach_kernel_driver(dev: libusb_device_handle_p_alias, + interface: c_int) -> c_int: ... +def libusb_attach_kernel_driver(dev: libusb_device_handle_p_alias, + interface: c_int) -> c_int: ... +def libusb_set_auto_detach_kernel_driver(dev: libusb_device_handle_p_alias, + enable: c_int) -> c_int: ... +def libusb_control_transfer_get_data(transfer_p: libusb_transfer_p_alias) -> bytearray: ... +def libusb_control_transfer_get_setup(transfer_p: libusb_transfer_p_alias) -> libusb_control_setup_p_alias: ... +def libusb_fill_control_setup(setup_p: str, + bmRequestType: c_uint8, + bRequest: c_uint8, + wValue: c_uint16, + wIndex: c_uint16, + wLength: c_uint16) -> None: ... +def libusb_alloc_transfer(iso_packets: c_int) -> libusb_transfer_p_alias: ... +def libusb_submit_transfer(transfer: libusb_transfer_p_alias) -> c_int: ... +def libusb_cancel_transfer(transfer: libusb_transfer_p_alias) -> c_int: ... +def libusb_free_transfer(transfer: libusb_transfer_p_alias) -> None: ... +def libusb_fill_control_transfer(transfer_p: libusb_transfer_p_alias, + dev_handle: libusb_device_handle_p_alias, + buffer: c_void_p, + callback: libusb_transfer_cb_fn_p_alias, + user_data: c_void_p, + timeout: c_uint) -> None: ... +def libusb_fill_bulk_transfer(transfer_p: libusb_transfer_p_alias, + dev_handle: libusb_device_handle_p_alias, + endpoint: c_uchar, + buffer: c_void_p, + length: c_int, + callback: libusb_transfer_cb_fn_p_alias, + user_data: c_void_p, + timeout: c_uint) -> None: ... +def libusb_fill_interrupt_transfer(transfer_p: libusb_transfer_p_alias, + dev_handle: libusb_device_handle_p_alias, + endpoint: c_uchar, + buffer: c_void_p, + length: c_int, + callback: libusb_transfer_cb_fn_p_alias, + user_data: c_void_p, + timeout: c_uint) -> None: ... +def libusb_fill_iso_transfer(transfer_p: libusb_transfer_p_alias, + dev_handle: libusb_device_handle_p_alias, + endpoint: c_uchar, + buffer: c_void_p, + length: c_int, + num_iso_packets: c_int, + callback: libusb_transfer_cb_fn_p_alias, + user_data: c_void_p, + timeout: c_uint) -> None: ... +def get_iso_packet_list(transfer_p: libusb_transfer_p_alias) -> List[libusb_iso_packet_descriptor_p_alias]: ... +def get_iso_packet_buffer_list(transfer_p: libusb_transfer_p_alias) -> List[bytearray]: ... +def get_extra(descriptor: libusb_descriptor_alias) -> List[bytearray]: ... +def libusb_set_iso_packet_lengths(transfer_p: libusb_transfer_p_alias, + length: c_int) -> None: ... +def libusb_get_iso_packet_buffer(transfer_p: libusb_transfer_p_alias, + packet: c_int) -> bytearray: ... +def libusb_get_iso_packet_buffer_simple(transfer_p: libusb_transfer_p_alias, + packet: c_int) -> bytearray: ... +def libusb_control_transfer(dev_handle: libusb_device_handle_p_alias, + request_type: c_uint8, + request: c_uint8, + value: c_uint16, + index: c_uint16, + data: c_void_p, + length: c_uint16, + timeout: c_uint) -> c_int: ... +def libusb_bulk_transfer(dev_handle: libusb_device_handle_p_alias, + endpoint: c_uchar, + data: c_void_p, + length: c_int, + actual_length: c_int, + timeout: c_uint) -> c_int: ... +def libusb_interrupt_transfer(dev_handle: libusb_device_handle_p_alias, + endpoint: c_uchar, + data: c_void_p, + length: c_int, + actual_length: c_int, + timeout: c_uint) -> c_int: ... +def libusb_get_descriptor(dev: libusb_device_handle_p_alias, + desc_type: c_uint16, + desc_index: c_uint16, + data: c_void_p, + length: c_uint16) -> c_int: ... +def libusb_get_string_descriptor(dev: libusb_device_handle_p_alias, + desc_index: c_uint16, + langid: c_uint16, + data: c_void_p, + length: c_uint16) -> c_int: ... +def libusb_get_string_descriptor_ascii(dev: libusb_device_handle_p_alias, + index: c_uint8, + data: c_void_p, + length: c_int) -> c_int: ... +def libusb_try_lock_events(ctx: libusb_context_p_alias) -> c_int: ... +def libusb_lock_events(ctx: libusb_context_p_alias) -> c_int: ... +def libusb_unlock_events(ctx: libusb_context_p_alias) -> c_int: ... +def libusb_event_handling_ok(ctx: libusb_context_p_alias) -> c_int: ... +def libusb_event_handler_active(ctx: libusb_context_p_alias) -> c_int: ... +def libusb_lock_event_waiters(ctx: libusb_context_p_alias) -> None: ... +def libusb_unlock_event_waiters(ctx: libusb_context_p_alias) -> None: ... +def libusb_wait_for_event(ctx: libusb_context_p_alias, + tv: timeval_p_alias) -> c_int: ... +def libusb_handle_events_timeout(ctx: libusb_context_p_alias, + tv: timeval_p_alias) -> c_int: ... +def libusb_handle_events_timeout_completed(ctx: libusb_context_p_alias, + tv: timeval_p_alias, + completed: c_int_p_alias) -> c_int: ... +def libusb_handle_events(ctx: libusb_context_p_alias) -> c_int: ... +def libusb_handle_events_completed(ctx: libusb_context_p_alias, + completed: c_int_p_alias) -> c_int: ... +def libusb_handle_events_locked(ctx: libusb_context_p_alias, + tv: timeval_p_alias) -> c_int: ... +def libusb_get_next_timeout(ctx: libusb_context_p_alias, + tv: timeval_p_alias) -> c_int: ... +def libusb_get_pollfds(ctx: libusb_context_p_alias) -> libusb_pollfd_p_p_alias: ... +def libusb_set_pollfd_notifiers(ctx: libusb_context_p_alias, + added_cb: libusb_pollfd_added_cb_p_alias, + removed_cb: libusb_pollfd_removed_cb_p_alias, + user_data: c_void_p) -> None: ... +def libusb_hotplug_register_callback(ctx: libusb_context_p_alias, + events: int, + flag: int, + vendor_id: int, + product_id: int, + dev_class: int, + cb_fn: libusb_hotplug_callback_fn_p_alias, + user_data: c_void_p, + handle: libusb_hotplug_callback_handle_p_alias) -> c_int: ... +def libusb_hotplug_deregister_callback(ctx: libusb_context_p_alias, + handle: libusb_hotplug_callback_handle_alias) -> None: ... From 41beb189401a10f5e036dcb3c91343cd56f54806 Mon Sep 17 00:00:00 2001 From: Andrew Hawker Date: Tue, 8 Sep 2020 14:43:17 -0700 Subject: [PATCH 2/3] Add mypy config The _version.py and testUSB1 modules are both ignored by mypy since they're "internal" to the package. They can be included in the future by simply removing their 'ignore_errors' lines. --- mypy.ini | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 mypy.ini diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 0000000..a3d8f53 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,9 @@ +[mypy] +warn_return_any = True +warn_unused_configs = True + +[mypy-usb1._version] +ignore_errors = True + +[mypy-usb1.testUSB1] +ignore_errors = True From 143b56318a667e82c9be9279820dbd89bc9bdd67 Mon Sep 17 00:00:00 2001 From: Andrew Hawker Date: Tue, 8 Sep 2020 15:11:04 -0700 Subject: [PATCH 3/3] Add travis ci job for validating mypy type hints --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index a2e3e94..584c184 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,12 @@ python: - "3.5" - "3.6" - "pypy" +jobs: + include: + - python: '3.6' + install: pip install mypy + script: mypy --strict usb1 + env: LINT=1 addons: apt: packages: