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