walac/pyusb — pull request #12 by Norberg (2012-12-25 21:50:38+00:00)
Title: Match **args before custom_match in usb.core.find()

Previous the custom_match was tested before any **args matching, this
resulted in unwanted behavior when the custom_match had to perform
a action towards the usb device that required permission that was not
necessary available for all usb devices.

Example use case where this is needed, want to match on idVendor and iManufacturer. To get iManufacturer device needs permission to use the device, under a normal linux system a non-root user dont have acess to all usb devices, but by limiting the custom_match critera to only devices matching the **args critera this problem is solved.

example program needing this commit to work as non-root:
import usb
import usb.util

def match_manufacturer(dev):
if usb.util.get_string(dev,126, dev.iManufacturer) == "pthread.se":
return True
return False

dev = usb.core.find(idVendor=0x16c0, custom_match=match_manufacturer)
dev.set_configuration()
print usb.util.get_string(dev,126, dev.iManufacturer)