MythTV master
hwdata.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2
3# smolt - Fedora hardware profiler
4#
5# Copyright (C) 2010 Mike McGrath <mmcgrath@redhat.com>
6# Copyright (C) 2011 Alexandre Rostovtsev <tetromino@gmail.com>
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21
22from builtins import int
23from builtins import open
24from builtins import object
25from smolt_config import get_config_attr
26
27class myVendor(object):
28 def __init__(self):
29 self.name = ""
30 self.num = ""
31 self.devices = {}
32
33class myDevice(object):
34 def __init__(self):
35 self.name = ""
36 self.num = ""
37 self.subvendors = {}
38 self.vendor = ""
39
40class DeviceMap(object):
41 vendors = {}
42
43 def __init__(self, bus='pci'):
44 self.vendors['pci'] = self.device_map('pci')
45 self.vendors['usb'] = self.device_map('usb')
46
47 def device_map(self, bus='pci'):
48 import os
49 HWDATA_DIRS = [ get_config_attr("HWDATA_DIR"), '/usr/share/hwdata','/usr/share/misc','/usr/share/' ]
50 for hwd_file in HWDATA_DIRS:
51 fn = "%s/%s.ids" % (hwd_file, bus)
52 if os.path.isfile(fn + ".gz"):
53 import gzip
54 try:
55 # File isn't in the default python3 UTF-8, using latin1
56 fo = gzip.open(fn + ".gz", 'rt', encoding='latin1')
57 break
58 except IOError:
59 pass
60 else:
61 try:
62 fo = open(fn, 'rt', encoding='latin1')
63 break
64 except IOError:
65 pass
66 else:
67 raise Exception('Hardware data file not found. Please set the location HWDATA_DIR in config.py')
68
69
70
71 vendors = {}
72 curvendor = None
73 curdevice = None
74 for line in fo.readlines():
75 if line.startswith('#') or not line.strip():
76 continue
77 elif not line.startswith('\t'):
78 curvendor = myVendor()
79 try:
80 curvendor.num = int(line[0:4], 16)
81 except:
82 continue
83 curvendor.name = line[6:-1]
84 vendors[curvendor.num] = curvendor
85 continue
86
87 elif line.startswith('\t\t'):
88 line = line.replace('\t', '')
89 thisdev = myDevice()
90 try:
91 thisdev.vendor = int(line[0:4], 16)
92 except:
93 continue
94 try:
95 thisdev.num = int(line[5:9], 16)
96 except:
97 continue
98 thisdev.name = line[11:-1]
99 subvend = myVendor()
100 try:
101 subvend.num = thisdev.vendor
102 except:
103 continue
104 subvend.name = ""
105
106 if subvend.num not in curdevice.subvendors:
107 curdevice.subvendors[subvend.num] = subvend
108 subvend.devices[thisdev.num] = thisdev
109 else:
110 subvend = curdevice.subvendors[subvend.num]
111 subvend.devices[thisdev.num] = thisdev
112
113 continue
114
115 elif line.startswith('\t'):
116 line = line.replace('\t', '')
117 curdevice = myDevice()
118 try:
119 curdevice.num = int(line[0:4], 16)
120 except:
121 continue
122 curdevice.name = line[6:-1]
123 try:
124 curdevice.vendor = int(curvendor.num)
125 except:
126 continue
127 curvendor.devices[curdevice.num] = curdevice
128 continue
129 else:
130 print(line)
131 continue
132 fo.close()
133 # This introduces a bug, will fix later.
134# vendors[0] = myVendor()
135# vendors[0].name = 'N/A'
136 return vendors
137
138 def vendor(self, vend, subvend=None, alt='N/A', bus='pci'):
139 try:
140 vend = int(vend)
141 except:
142 return alt
143 if vend == 0:
144 return alt
145 try:
146 return self.vendors[bus][vend].devices[dev].subvendors[subvend].name + "a"
147 except:
148 try:
149 return self.vendors[bus][vend].name
150 except:
151 return alt
152
153 def device(self, vend, dev, subvend=None, subdevice=None, alt='N/A', bus='pci'):
154 try:
155 vend = int(vend)
156 except:
157 pass
158 try:
159 dev = int(dev)
160 except:
161 pass
162 try:
163 subvend = int(subvend)
164 except:
165 pass
166 try:
167 subdevice = int(subdevice)
168 except:
169 pass
170 try:
171 return self.vendors[bus][vend].devices[dev].name
172 except:
173 try:
174 self.vendors[bus][vend].devices[dev].name
175 except:
176 return alt
177
178 def subdevice(self, vend, dev, subvend, subdevice, alt='N/A', bus='pci'):
179# return self.vendors[vend].devices[dev].name
180 try:
181 vend = int(vend)
182 except:
183 pass
184 if vend == 0:
185 return alt
186 try:
187 dev = int(dev)
188 except:
189 pass
190 try:
191 subvend = int(subvend)
192 except:
193 pass
194 try:
195 subdevice = int(subdevice)
196 except:
197 pass
198 try:
199 var = self.vendors[bus][vend].devices[dev].subvendors[subvend].devices[subdevice].name
200 return var
201 except:
202 try:
203 return self.vendors[bus][vend].devices[dev].name
204 except:
205 return alt
def device_map(self, bus='pci')
Definition: hwdata.py:47
def subdevice(self, vend, dev, subvend, subdevice, alt='N/A', bus='pci')
Definition: hwdata.py:178
def vendor(self, vend, subvend=None, alt='N/A', bus='pci')
Definition: hwdata.py:138
def device(self, vend, dev, subvend=None, subdevice=None, alt='N/A', bus='pci')
Definition: hwdata.py:153
def __init__(self, bus='pci')
Definition: hwdata.py:43
def get_config_attr(attr, default="")
Definition: smolt_config.py:22
static void print(const QList< uint > &raw_minimas, const QList< uint > &raw_maximas, const QList< float > &minimas, const QList< float > &maximas)