]>
Commit | Line | Data |
---|---|---|
6e9f5252 MT |
1 | # |
2 | # Copyright (c) 1999--2012 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 | ||
0061e840 MT |
18 | import sys |
19 | ||
6e9f5252 MT |
20 | # pylint: disable=misplaced-bare-raise |
21 | ||
22 | class USB(object): | |
23 | """ Interace to usb.ids from hwdata package """ | |
2b198178 | 24 | filename = '/usr/share/misc/usb.ids' |
6e9f5252 MT |
25 | devices = None |
26 | ||
27 | def __init__(self, filename=None): | |
28 | """ Load pci.ids from file to internal data structure. | |
29 | parameter 'filename' can specify location of this file | |
30 | """ | |
31 | if filename: | |
32 | self.filename = filename | |
33 | else: | |
34 | self.filename = USB.filename | |
35 | self.cache = 1 | |
36 | ||
37 | if self.cache and not USB.devices: | |
38 | # parse usb.ids | |
39 | USB.devices = {} | |
40 | try: | |
41 | f = open(self.filename, encoding='ISO8859-1') | |
42 | except IOError: | |
43 | return | |
44 | lineno = 0 | |
45 | vendor = None | |
46 | device = None | |
47 | for line in f.readlines(): | |
48 | lineno += 1 | |
49 | l = line.split() | |
50 | if line.startswith('#'): | |
51 | if line.startswith('# List of known device classes, subclasses and protocols'): | |
52 | break # end of database of devices, rest is protocols, types etc. | |
53 | else: | |
54 | continue | |
55 | elif len(l) == 0: | |
56 | continue | |
57 | elif line.startswith('\t\t'): | |
58 | interface_id = l[0].lower() | |
59 | if len(l) > 2: | |
60 | interface_name = ' '.join(l[1:]) | |
61 | else: | |
62 | interface_name = '' | |
63 | try: | |
64 | USB.devices[vendor][1][device][0][interface_id] = interface_name | |
65 | except TypeError: | |
66 | sys.stderr.write("Unknown line at line {0} in {1}.\n".format(lineno, self.filename)) | |
67 | elif line.startswith('\t'): | |
68 | device = l[0].lower() | |
69 | device_name = ' '.join(l[1:]) | |
70 | USB.devices[vendor][1][device] = [device_name, {}] | |
71 | else: | |
72 | vendor = l[0].lower() | |
73 | vendor_name = ' '.join(l[1:]) | |
74 | if vendor not in USB.devices: | |
75 | USB.devices[vendor] = [vendor_name, {}] | |
76 | else: # this should not happen | |
77 | USB.devices[vendor][0] = vendor_name | |
78 | ||
79 | def get_vendor(self, vendor): | |
80 | """ Return description of vendor. Parameter is two byte code in hexa. | |
81 | If vendor is unknown None is returned. | |
82 | """ | |
83 | vendor = vendor.lower() | |
84 | if self.cache: | |
85 | if vendor in USB.devices: | |
86 | return USB.devices[vendor][0] | |
87 | else: | |
88 | return None | |
89 | else: | |
90 | raise # not implemented yet | |
91 | ||
92 | def get_device(self, vendor, device): | |
93 | """ Return description of device. Parameters are two byte code variables in hexa. | |
94 | If device is unknown None is returned. | |
95 | """ | |
96 | vendor = vendor.lower() | |
97 | device = device.lower() | |
98 | if self.cache: | |
99 | if vendor in USB.devices: | |
100 | if device in USB.devices[vendor][1]: | |
101 | return USB.devices[vendor][1][device][0] | |
102 | else: | |
103 | return None | |
104 | else: | |
105 | return None | |
106 | else: | |
107 | raise # not implemented yet | |
108 | ||
109 | class PCI(object): | |
110 | """ Interace to pci.ids from hwdata package """ | |
2b198178 | 111 | filename = '/usr/share/misc/pci.ids' |
6e9f5252 MT |
112 | devices = None |
113 | ||
114 | def __init__(self, filename=None): | |
115 | """ Load pci.ids from file to internal data structure. | |
116 | parameter 'filename' can specify location of this file | |
117 | """ | |
118 | if filename: | |
119 | self.filename = filename | |
120 | else: | |
121 | self.filename = PCI.filename | |
122 | self.cache = 1 | |
123 | ||
124 | if self.cache and not PCI.devices: | |
125 | # parse pci.ids | |
126 | PCI.devices = {} | |
127 | try: | |
128 | f = open(self.filename, encoding='ISO8859-1') | |
129 | except IOError: | |
130 | return | |
131 | vendor = None | |
132 | for line in f.readlines(): | |
133 | l = line.split() | |
134 | if line.startswith('#'): | |
135 | continue | |
136 | elif len(l) == 0: | |
137 | continue | |
138 | elif line.startswith('\t\t'): | |
139 | continue | |
140 | elif line.startswith('\t'): | |
141 | device = l[0].lower() | |
142 | device_name = ' '.join(l[1:]) | |
143 | PCI.devices[vendor][1][device] = device_name | |
144 | else: | |
145 | vendor = l[0].lower() | |
146 | vendor_name = ' '.join(l[1:]) | |
147 | if not vendor in list(PCI.devices.keys()): | |
148 | PCI.devices[vendor] = [vendor_name, {}] | |
149 | else: # this should not happen | |
150 | PCI.devices[vendor][0] = vendor_name | |
151 | ||
152 | def get_vendor(self, vendor): | |
153 | """ Return description of vendor. Parameter is two byte code in hexa. | |
154 | If vendor is unknown None is returned. | |
155 | """ | |
156 | vendor = vendor.lower() | |
157 | if self.cache: | |
158 | if vendor in list(PCI.devices.keys()): | |
159 | return PCI.devices[vendor][0] | |
160 | else: | |
161 | return None | |
162 | else: | |
163 | raise # not implemented yet | |
164 | ||
165 | def get_device(self, vendor, device): | |
166 | """ Return description of device. Parameters are two byte code variables in hexa. | |
167 | If device is unknown None is returned. | |
168 | """ | |
169 | vendor = vendor.lower() | |
170 | device = device.lower() | |
171 | if self.cache: | |
172 | if vendor in list(PCI.devices.keys()): | |
173 | if device in list(PCI.devices[vendor][1].keys()): | |
174 | return PCI.devices[vendor][1][device] | |
175 | else: | |
176 | return None | |
177 | else: | |
178 | return None | |
179 | else: | |
180 | raise # not implemented yet | |
181 | ||
182 | class PNP(object): | |
183 | """ Interace to pnp.ids from hwdata package """ | |
184 | filename = '/usr/share/hwdata/pnp.ids' | |
185 | VENDORS = None | |
186 | ||
187 | def __init__(self, filename=None): | |
188 | """ Load pnp.ids from file to internal data structure. | |
189 | parameter 'filename' can specify location of this file | |
190 | """ | |
191 | if filename: | |
192 | self.filename = filename | |
193 | else: | |
194 | self.filename = PNP.filename | |
195 | self.cache = 1 | |
196 | ||
197 | if self.cache and not PNP.VENDORS: | |
198 | # parse pnp.ids | |
199 | PNP.VENDORS = {} | |
200 | try: | |
201 | f = open(self.filename, encoding='ISO8859-1') | |
202 | except IOError: | |
203 | return | |
204 | for line in f.readlines(): | |
205 | l = line.split() | |
206 | if line.startswith('#'): | |
207 | continue | |
208 | elif len(l) == 0: | |
209 | continue | |
210 | else: | |
211 | vendor_id = l[0].upper() | |
212 | PNP.VENDORS[vendor_id] = ' '.join(l[1:]) | |
213 | ||
214 | def get_vendor(self, vendor_id): | |
215 | """ Return description of vendor. Parameter is 3 character long id of vendor. | |
216 | If vendor is unknown None is returned. | |
217 | """ | |
218 | vendor_id = vendor_id.upper() | |
219 | if self.cache: | |
220 | if vendor_id in list(PNP.VENDORS.keys()): | |
221 | return PNP.VENDORS[vendor_id] | |
222 | else: | |
223 | return None | |
224 | else: | |
225 | raise # not implemented yet |