PK
1 # 2 # Copyright (c) 1999--2010 Red Hat Inc. 3 # 4 # This software is licensed to you under the GNU General Public License, 5 # version 2 (GPLv2). There is NO WARRANTY for this software, express or 6 # implied, including the implied warranties of MERCHANTABILITY or FITNESS 7 # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 8 # along with this software; if not, see 9 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 10 # 11 # Red Hat trademarks are not licensed under GPLv2. No permission is 12 # granted to use or replicate Red Hat trademarks that are incorporated 13 # in this software or its documentation. 14 # 15 16 """ Query hwdata database and return decription of vendor and/or device. """ 17 18 -class USB: 19 """ Interace to usb.ids from hwdata package """ 20 filename = '/usr/share/hwdata/usb.ids' 21 devices = None 22 23 - def __init__(self, filename=None): 24 """ Load pci.ids from file to internal data structure. 25 parameter 'filename' can specify location of this file 26 """ 27 if filename: 28 self.filename = filename 29 else: 30 self.filename = USB.filename 31 self.cache = 1 32 33 if self.cache and not USB.devices: 34 # parse usb.ids 35 USB.devices = {} 36 for line in open(self.filename).readlines(): 37 l = line.split() 38 if line.startswith('#'): 39 if line.startswith('# List of known device classes, subclasses and protocols'): 40 break # end of database of devices, rest is protocols, types etc. 41 else: 42 continue 43 elif len(l) == 0: 44 continue 45 elif line.startswith('\t\t'): 46 interface_id = l[0].lower() 47 if len(l) > 2: 48 interface_name = ' '.join(l[1:]) 49 else: 50 interface_name = '' 51 USB.devices[vendor][1][device][0][interface_id] = interface_name 52 elif line.startswith('\t'): 53 device = l[0].lower() 54 device_name = ' '.join(l[1:]) 55 USB.devices[vendor][1][device] = [device_name, {}] 56 else: 57 vendor = l[0].lower() 58 vendor_name = ' '.join(l[1:]) 59 if not USB.devices.has_key(vendor): 60 USB.devices[vendor] = [vendor_name, {}] 61 else: # this should not happen 62 USB.devices[vendor][0] = vendor_name 63 64 - def get_vendor(self, vendor): 65 """ Return description of vendor. Parameter is two byte code in hexa. 66 If vendor is unknown None is returned. 67 """ 68 vendor = vendor.lower() 69 if self.cache: 70 if USB.devices.has_key(vendor): 71 return USB.devices[vendor][0] 72 else: 73 return None 74 else: 75 raise # not implemented yet 76 77 - def get_device(self, vendor, device): 78 """ Return description of device. Parameters are two byte code variables in hexa. 79 If device is unknown None is returned. 80 """ 81 vendor = vendor.lower() 82 device = device.lower() 83 if self.cache: 84 if USB.devices.has_key(vendor): 85 if USB.devices[vendor][1].has_key(device): 86 return USB.devices[vendor][1][device][0] 87 else: 88 return None 89 else: 90 return None 91 else: 92 raise # not implemented yet 93 94 -class PCI: 95 """ Interace to pci.ids from hwdata package """ 96 filename = '/usr/share/hwdata/pci.ids' 97 devices = None 98 99 - def __init__(self, filename=None): 100 """ Load pci.ids from file to internal data structure. 101 parameter 'filename' can specify location of this file 102 """ 103 if filename: 104 self.filename = filename 105 else: 106 self.filename = PCI.filename 107 self.cache = 1 108 109 if self.cache and not PCI.devices: 110 # parse pci.ids 111 pcirec = {} 112 PCI.devices = {} 113 for line in open(self.filename).readlines(): 114 l = line.split() 115 if line.startswith('#'): 116 continue 117 elif len(l) == 0: 118 continue 119 elif line.startswith('\t\t'): 120 subvendor = l[0].lower() 121 if len(l) > 2: 122 subdevice = l[1].lower() 123 else: 124 subdevice = '' 125 if len(l) > 3: 126 subsystem_name = ' '.join(l[2:]) 127 else: 128 subsystem_name = '' 129 if not PCI.devices.has_key(subvendor): 130 PCI.devices[subvendor] = [vendor_name, {subdevice: subsystem_name}] 131 else: # this should not happen 132 PCI.devices[subvendor][1][subdevice] = subsystem_name 133 elif line.startswith('\t'): 134 device = l[0].lower() 135 device_name = ' '.join(l[1:]) 136 PCI.devices[vendor][1][device] = device_name 137 else: 138 vendor = l[0].lower() 139 vendor_name = ' '.join(l[1:]) 140 if not PCI.devices.has_key(vendor): 141 PCI.devices[vendor] = [vendor_name, {}] 142 else: # this should not happen 143 PCI.devices[vendor][0] = vendor_name 144 145 - def get_vendor(self, vendor): 146 """ Return description of vendor. Parameter is two byte code in hexa. 147 If vendor is unknown None is returned. 148 """ 149 vendor = vendor.lower() 150 if self.cache: 151 if PCI.devices.has_key(vendor): 152 return PCI.devices[vendor][0] 153 else: 154 return None 155 else: 156 raise # not implemented yet 157 158 - def get_device(self, vendor, device): 159 """ Return description of device. Parameters are two byte code variables in hexa. 160 If device is unknown None is returned. 161 """ 162 vendor = vendor.lower() 163 device = device.lower() 164 if self.cache: 165 if PCI.devices.has_key(vendor): 166 if PCI.devices[vendor][1].has_key(device): 167 return PCI.devices[vendor][1][device] 168 else: 169 return None 170 else: 171 return None 172 else: 173 raise # not implemented yet 174