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