def __init__(self, device_specifier):
        """
        device_specifier: string or pyusb.core.Device: identifier of the \
            format usb://idVendor:idProduct/iSerialNumber or pyusb.core.Device instance.
        """

        self.dev = None
        self.read_timeout =    10. # ms
        self.write_timeout = 15000. # ms
        # strategy : try_twice or select
        self.strategy = 'try_twice'
        if isinstance(device_specifier, str):
            if device_specifier.startswith('usb://'):
                device_specifier = device_specifier[6:]
            vendor_product, _, serial = device_specifier.partition('/')
            vendor, _, product = vendor_product.partition(':')
            vendor, product = int(vendor, 16), int(product, 16)
            for result in list_available_devices():
                printer = result['instance']
                if printer.idVendor == vendor and printer.idProduct == product or (serial and printer.iSerialNumber == serial):
                    self.dev = printer
                    break
            if self.dev is None:
                raise ValueError('Device not found')
        elif isinstance(device_specifier, usb.core.Device):
            self.dev = device_specifier
        else:
            raise NotImplementedError('Currently the printer can be specified either via an appropriate string or via a usb.core.Device instance.')

        # Now we are sure to have self.dev around, start using it:

        try:
            assert self.dev.is_kernel_driver_active(0)
            self.dev.detach_kernel_driver(0)
            self.was_kernel_driver_active = True
        except (NotImplementedError, AssertionError):
            self.was_kernel_driver_active = False

        # set the active configuration. With no arguments, the first configuration will be the active one
        self.dev.set_configuration()

        cfg = self.dev.get_active_configuration()
        intf = usb.util.find_descriptor(cfg, bInterfaceClass=7)
        assert intf is not None

        ep_match_in  = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN
        ep_match_out = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT

        ep_in  = usb.util.find_descriptor(intf, custom_match=ep_match_in)
        ep_out = usb.util.find_descriptor(intf, custom_match=ep_match_out)

        assert ep_in  is not None
        assert ep_out is not None

        self.write_dev = ep_out
        self.read_dev  = ep_in
def __init__ ( self , device_specifier ) : self . dev = None self . read_timeout = 10. self . write_timeout = 15000. self . strategy = 'try_twice' if isinstance ( device_specifier , str ) : if device_specifier . startswith ( 'usb://' ) : device_specifier = device_specifier [ 6 : ] vendor_product , _ , serial = device_specifier . partition ( '/' ) vendor , _ , product = vendor_product . partition ( ':' ) vendor , product = int ( vendor , 16 ) , int ( product , 16 ) for result in list_available_devices ( ) : printer = result [ 'instance' ] if printer . idVendor == vendor and printer . idProduct == product or ( serial and printer . iSerialNumber == serial ) : self . dev = printer break if self . dev is None : raise ValueError ( 'Device not found' ) elif isinstance ( device_specifier , usb . core . Device ) : self . dev = device_specifier else : raise NotImplementedError ( 'Currently the printer can be specified either via an appropriate string or via a usb.core.Device instance.' ) try : assert self . dev . is_kernel_driver_active ( 0 ) self . dev . detach_kernel_driver ( 0 ) self . was_kernel_driver_active = True except ( NotImplementedError , AssertionError ) : self . was_kernel_driver_active = False self . dev . set_configuration ( ) cfg = self . dev . get_active_configuration ( ) intf = usb . util . find_descriptor ( cfg , bInterfaceClass = 7 ) assert intf is not None ep_match_in = lambda e : usb . util . endpoint_direction ( e . bEndpointAddress ) == usb . util . ENDPOINT_IN ep_match_out = lambda e : usb . util . endpoint_direction ( e . bEndpointAddress ) == usb . util . ENDPOINT_OUT ep_in = usb . util . find_descriptor ( intf , custom_match = ep_match_in ) ep_out = usb . util . find_descriptor ( intf , custom_match = ep_match_out ) assert ep_in is not None assert ep_out is not None self . write_dev = ep_out self . read_dev = ep_in
device_specifier: string or pyusb.core.Device: identifier of the \
            format usb://idVendor:idProduct/iSerialNumber or pyusb.core.Device instance.