<commit_before>import logging
import usb
from ant_stream_device import AntStreamDevice

_log = logging.getLogger("gat.ant_usb_device")


class AntUsbDevice(AntStreamDevice):
    
    def _read(self, timeout=100):
        """
        Read one block (message) from usb device.
        """
        return self._hardware.read(timeout=timeout)


class AntUsbHardware(object):
    """
    Provide read/write access to a USB ANT stick via libUSB.
    e.g. nRF24AP2-USB. This class does not support usb devices
    that use an ftdi serial bringe. Use a SerialAntHardware them.
    """
    
    def __init__(self, idVendor, idProduct, configuration=1, interface=0, altInterface=0, endpoint=0x01):
        """
        Create a new connection with USB device. idProduct, idVendor are required.
        Extended arguments allow for sellection of endpoints. This impelemnation
        only supports bulk transfers and no sort of message oriented approach.
        """
        self._dev = self._find_usb_device(idVendor, idProduct)
        if not self._dev:
            raise IOError("No USB Device could be found with vid=0x%04x pid=0x%04x." % (idVendor, idProduct))
        self._handle = self._dev.open() 
        self._handle.setConfiguration(configuration)
        self._handle.claimInterface(interface)
        self._handle.setAltInterface(altInterface)
        self._end_out = endpoint
        self._end_in = endpoint | 0x80
        self._handle.clearHalt(self._end_out)
        self._handle.clearHalt(self._end_in)

    def _find_usb_device(self, idVendor, idProduct):
        """
        Search usb busess for the first device matching vid/pid.
        """
        for bus in usb.busses():
            for dev in bus.devices:
                if dev.idProduct == idProduct and dev.idVendor == idVendor:
                    return dev<commit_msg>Support of system with more than on USB ANT stick.<commit_after>import logging
import usb
from ant_stream_device import AntStreamDevice

_log = logging.getLogger("gat.ant_usb_device")


class AntUsbDevice(AntStreamDevice):
    
    def _read(self, timeout=100):
        """
        Read one block (message) from usb device.
        """
        return self._hardware.read(timeout=timeout)


class AntUsbHardware(object):
    """
    Provide read/write access to a USB ANT stick via libUSB.
    e.g. nRF24AP2-USB. This class does not support usb devices
    that use an ftdi serial bringe. Use a SerialAntHardware them.
    """

    _dev = None

    def __init__(self, idVendor, idProduct, configuration=1, interface=0, altInterface=0, endpoint=0x01):
        """
        Create a new connection with USB device. idProduct, idVendor are required.
        Extended arguments allow for sellection of endpoints. This impelemnation
        only supports bulk transfers and no sort of message oriented approach.
        """
        for dev in self._find_usb_devices(idVendor, idProduct):
            # find the first usb device not already claimed
            try:
                self._handle = dev.open() 
                self._handle.setConfiguration(configuration)
                self._handle.claimInterface(interface)
                self._handle.setAltInterface(altInterface)
                self._end_out = endpoint
                self._end_in = endpoint | 0x80
                self._handle.clearHalt(self._end_out)
                self._handle.clearHalt(self._end_in)
                self._dev = dev
            except usb.USBError as e:
                if not [arg for arg in e.args if 'Device or resource busy' in arg]: raise
                else: _log.warn("Found device matching device, but already claimed. Will keep looking")
        if not self._dev:
            raise IOError("No avialible USB Device could be found with vid=0x%04x pid=0x%04x." % (idVendor, idProduct))

    def _find_usb_devices(self, idVendor, idProduct):
        """
        Search usb busess for the first device matching vid/pid.
        """
        for bus in usb.busses():
            for dev in bus.devices:
                if dev.idProduct == idProduct and dev.idVendor == idVendor:
                    yield dev
{'fraction_non_alphanumeric': 0.07878202029966168, 'fraction_numerical': 0.01014983083615273, 'mean_word_length': 2.384300899427637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7520212', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1129, 'n_words': 360}