# Copyright (c) 1999--2012 Red Hat, Inc.
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
inPy3k = sys.version_info[0] == 3
""" Query hwdata database and return decription of vendor and/or device. """
# pylint: disable=misplaced-bare-raise
""" Interace to usb.ids from hwdata package """
filename = '/usr/share/hwdata/usb.ids'
def __init__(self, filename=None):
""" Load pci.ids from file to internal data structure.
parameter 'filename' can specify location of this file
self.filename = USB.filename
if self.cache and not USB.devices:
f = open(self.filename, encoding='ISO8859-1')
for line in f.readlines():
if line.startswith('# List of known device classes, subclasses and protocols'):
break # end of database of devices, rest is protocols, types etc.
elif line.startswith('\t\t'):
interface_id = l[0].lower()
interface_name = ' '.join(l[1:])
USB.devices[vendor][1][device][0][interface_id] = interface_name
sys.stderr.write("Unknown line at line {0} in {1}.\n".format(lineno, self.filename))
elif line.startswith('\t'):
device_name = ' '.join(l[1:])
USB.devices[vendor][1][device] = [device_name, {}]
vendor_name = ' '.join(l[1:])
if vendor not in USB.devices:
USB.devices[vendor] = [vendor_name, {}]
else: # this should not happen
USB.devices[vendor][0] = vendor_name
def get_vendor(self, vendor):
""" Return description of vendor. Parameter is two byte code in hexa.
If vendor is unknown None is returned.
if vendor in USB.devices:
return USB.devices[vendor][0]
raise # not implemented yet
def get_device(self, vendor, device):
""" Return description of device. Parameters are two byte code variables in hexa.
If device is unknown None is returned.
if vendor in USB.devices:
if device in USB.devices[vendor][1]:
return USB.devices[vendor][1][device][0]
raise # not implemented yet
""" Interace to pci.ids from hwdata package """
filename = '/usr/share/hwdata/pci.ids'
def __init__(self, filename=None):
""" Load pci.ids from file to internal data structure.
parameter 'filename' can specify location of this file
self.filename = PCI.filename
if self.cache and not PCI.devices:
f = open(self.filename, encoding='ISO8859-1')
for line in f.readlines():
elif line.startswith('\t\t'):
elif line.startswith('\t'):
device_name = ' '.join(l[1:])
PCI.devices[vendor][1][device] = device_name
vendor_name = ' '.join(l[1:])
if not vendor in list(PCI.devices.keys()):
PCI.devices[vendor] = [vendor_name, {}]
else: # this should not happen
PCI.devices[vendor][0] = vendor_name
def get_vendor(self, vendor):
""" Return description of vendor. Parameter is two byte code in hexa.
If vendor is unknown None is returned.
if vendor in list(PCI.devices.keys()):
return PCI.devices[vendor][0]
raise # not implemented yet
def get_device(self, vendor, device):
""" Return description of device. Parameters are two byte code variables in hexa.
If device is unknown None is returned.
if vendor in list(PCI.devices.keys()):
if device in list(PCI.devices[vendor][1].keys()):
return PCI.devices[vendor][1][device]
raise # not implemented yet
""" Interace to pnp.ids from hwdata package """
filename = '/usr/share/hwdata/pnp.ids'
def __init__(self, filename=None):
""" Load pnp.ids from file to internal data structure.
parameter 'filename' can specify location of this file
self.filename = PNP.filename
if self.cache and not PNP.VENDORS:
f = open(self.filename, encoding='ISO8859-1')
for line in f.readlines():
PNP.VENDORS[vendor_id] = ' '.join(l[1:])
def get_vendor(self, vendor_id):
""" Return description of vendor. Parameter is 3 character long id of vendor.
If vendor is unknown None is returned.
vendor_id = vendor_id.upper()
if vendor_id in list(PNP.VENDORS.keys()):
return PNP.VENDORS[vendor_id]
raise # not implemented yet