1"""Utility class representing the spec for a USB device. 2 3The file cfm_usb_devices.py lists all known USB device specs. 4""" 5 6class UsbDeviceSpec(object): 7 """Utility class representing the spec for a CfM USB device.""" 8 9 # Dictionary of all UsbDeviceSpec instance that have been created. 10 # Mapping from vid_pid to UsbDeviceSpec instance. 11 _all_specs = {} 12 13 def __init__(self, vid, pid, product, interfaces): 14 """ 15 Constructor. 16 17 @param vid: Vendor ID. String. 18 @param pid: Product ID. String. 19 @param product: Product description. String 20 @param interfaces: List of strings 21 """ 22 self._vid = vid 23 self._pid = pid 24 self._product = product 25 self._interfaces = interfaces 26 self.__class__._all_specs[self.vid_pid] = self 27 28 @classmethod 29 def get_usb_device_spec(cls, vid_pid): 30 """Looks up UsbDeviceSpec by vid_pid.""" 31 return cls._all_specs.get(vid_pid) 32 33 @property 34 def vendor_id(self): 35 """Returns the vendor id for this USB device.""" 36 return self._vid 37 38 @property 39 def product_id(self): 40 """Returns the product id for this USB device.""" 41 return self._pid 42 43 @property 44 def vid_pid(self): 45 """Return the <vendor_id>:<product_id> as a string.""" 46 return '%s:%s' % (self._vid, self._pid) 47 48 @property 49 def product(self): 50 """Returns the product name.""" 51 return self._product 52 53 @property 54 def interfaces(self): 55 """Returns the list of interfaces.""" 56 return self._interfaces 57 58 def __str__(self): 59 return self.__repr__() 60 61 def __repr__(self): 62 return "%s (%s)" % (self._product, self.vid_pid) 63