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 
22 from __future__ import print_function
23 from __future__ import unicode_literals
24 from __future__ import division
25 from __future__ import absolute_import
26 from builtins import int
27 from builtins import open
28 from builtins import object
29 from smolt_config import get_config_attr
30 
31 class myVendor(object):
32  def __init__(self):
33  self.name = ""
34  self.num = ""
35  self.devices = {}
36 
37 class myDevice(object):
38  def __init__(self):
39  self.name = ""
40  self.num = ""
41  self.subvendors = {}
42  self.vendor = ""
43 
44 class DeviceMap(object):
45  vendors = {}
46 
47  def __init__(self, bus='pci'):
48  self.vendors['pci'] = self.device_map('pci')
49  self.vendors['usb'] = self.device_map('usb')
50 
51  def device_map(self, bus='pci'):
52  import os
53  HWDATA_DIRS = [ get_config_attr("HWDATA_DIR"), '/usr/share/hwdata','/usr/share/misc','/usr/share/' ]
54  for hwd_file in HWDATA_DIRS:
55  fn = "%s/%s.ids" % (hwd_file, bus)
56  if os.path.isfile(fn + ".gz"):
57  import gzip
58  try:
59  # File isn't in the default python3 UTF-8, using latin1
60  fo = gzip.open(fn + ".gz", 'rt', encoding='latin1')
61  break
62  except IOError:
63  pass
64  else:
65  try:
66  fo = open(fn, 'rt', encoding='latin1')
67  break
68  except IOError:
69  pass
70  else:
71  raise Exception('Hardware data file not found. Please set the location HWDATA_DIR in config.py')
72 
73 
74 
75  vendors = {}
76  curvendor = None
77  curdevice = None
78  for line in fo.readlines():
79  if line.startswith('#') or not line.strip():
80  continue
81  elif not line.startswith('\t'):
82  curvendor = myVendor()
83  try:
84  curvendor.num = int(line[0:4], 16)
85  except:
86  continue
87  curvendor.name = line[6:-1]
88  vendors[curvendor.num] = curvendor
89  continue
90 
91  elif line.startswith('\t\t'):
92  line = line.replace('\t', '')
93  thisdev = myDevice()
94  try:
95  thisdev.vendor = int(line[0:4], 16)
96  except:
97  continue
98  try:
99  thisdev.num = int(line[5:9], 16)
100  except:
101  continue
102  thisdev.name = line[11:-1]
103  subvend = myVendor()
104  try:
105  subvend.num = thisdev.vendor
106  except:
107  continue
108  subvend.name = ""
109 
110  if subvend.num not in curdevice.subvendors:
111  curdevice.subvendors[subvend.num] = subvend
112  subvend.devices[thisdev.num] = thisdev
113  else:
114  subvend = curdevice.subvendors[subvend.num]
115  subvend.devices[thisdev.num] = thisdev
116 
117  continue
118 
119  elif line.startswith('\t'):
120  line = line.replace('\t', '')
121  curdevice = myDevice()
122  try:
123  curdevice.num = int(line[0:4], 16)
124  except:
125  continue
126  curdevice.name = line[6:-1]
127  try:
128  curdevice.vendor = int(curvendor.num)
129  except:
130  continue
131  curvendor.devices[curdevice.num] = curdevice
132  continue
133  else:
134  print(line)
135  continue
136  fo.close()
137  # This introduces a bug, will fix later.
138 # vendors[0] = myVendor()
139 # vendors[0].name = 'N/A'
140  return vendors
141 
142  def vendor(self, vend, subvend=None, alt='N/A', bus='pci'):
143  try:
144  vend = int(vend)
145  except:
146  return alt
147  if vend == 0:
148  return alt
149  try:
150  return self.vendors[bus][vend].devices[dev].subvendors[subvend].name + "a"
151  except:
152  try:
153  return self.vendors[bus][vend].name
154  except:
155  return alt
156 
157  def device(self, vend, dev, subvend=None, subdevice=None, alt='N/A', bus='pci'):
158  try:
159  vend = int(vend)
160  except:
161  pass
162  try:
163  dev = int(dev)
164  except:
165  pass
166  try:
167  subvend = int(subvend)
168  except:
169  pass
170  try:
171  subdevice = int(subdevice)
172  except:
173  pass
174  try:
175  return self.vendors[bus][vend].devices[dev].name
176  except:
177  try:
178  self.vendors[bus][vend].devices[dev].name
179  except:
180  return alt
181 
182  def subdevice(self, vend, dev, subvend, subdevice, alt='N/A', bus='pci'):
183 # return self.vendors[vend].devices[dev].name
184  try:
185  vend = int(vend)
186  except:
187  pass
188  if vend == 0:
189  return alt
190  try:
191  dev = int(dev)
192  except:
193  pass
194  try:
195  subvend = int(subvend)
196  except:
197  pass
198  try:
199  subdevice = int(subdevice)
200  except:
201  pass
202  try:
203  var = self.vendors[bus][vend].devices[dev].subvendors[subvend].devices[subdevice].name
204  return var
205  except:
206  try:
207  return self.vendors[bus][vend].devices[dev].name
208  except:
209  return alt
hardwareprofile.hwdata.DeviceMap
Definition: hwdata.py:44
hardwareprofile.hwdata.myVendor.devices
devices
Definition: hwdata.py:35
hardwareprofile.hwdata.myDevice.num
num
Definition: hwdata.py:40
hardwareprofile.hwdata.myVendor.num
num
Definition: hwdata.py:34
hardwareprofile.hwdata.myDevice
Definition: hwdata.py:37
hardwareprofile.hwdata.myVendor
Definition: hwdata.py:31
hardwareprofile.hwdata.DeviceMap.subdevice
def subdevice(self, vend, dev, subvend, subdevice, alt='N/A', bus='pci')
Definition: hwdata.py:182
print
static void print(const QList< uint > &raw_minimas, const QList< uint > &raw_maximas, const QList< float > &minimas, const QList< float > &maximas)
Definition: vbi608extractor.cpp:29
hardwareprofile.smolt_config.get_config_attr
def get_config_attr(attr, default="")
Definition: smolt_config.py:22
hardwareprofile.hwdata.myDevice.subvendors
subvendors
Definition: hwdata.py:41
hardwareprofile.hwdata.DeviceMap.vendor
def vendor(self, vend, subvend=None, alt='N/A', bus='pci')
Definition: hwdata.py:142
hardwareprofile.hwdata.myVendor.__init__
def __init__(self)
Definition: hwdata.py:32
hardwareprofile.hwdata.DeviceMap.device_map
def device_map(self, bus='pci')
Definition: hwdata.py:51
hardwareprofile.hwdata.DeviceMap.__init__
def __init__(self, bus='pci')
Definition: hwdata.py:47
hardwareprofile.hwdata.DeviceMap.vendors
dictionary vendors
Definition: hwdata.py:45
hardwareprofile.hwdata.myVendor.name
name
Definition: hwdata.py:33
hardwareprofile.hwdata.myDevice.name
name
Definition: hwdata.py:39
hardwareprofile.hwdata.myDevice.__init__
def __init__(self)
Definition: hwdata.py:38
hardwareprofile.hwdata.myDevice.vendor
vendor
Definition: hwdata.py:42
hardwareprofile.hwdata.DeviceMap.device
def device(self, vend, dev, subvend=None, subdevice=None, alt='N/A', bus='pci')
Definition: hwdata.py:157