def load_pci_ids()

in pci_lib/pci_lib.py [0:0]


def load_pci_ids():
    db = {}
    pci_ids_locations = [
        "/usr/share/hwdata/pci.ids",
        "/usr/share/misc/pci.ids",
    ]
    pci_ids_location = None
    for loc in pci_ids_locations:
        if os.path.isfile(loc):
            pci_ids_location = loc
            break
    if not pci_ids_location:
        raise RuntimeError(
            "No pci.ids file avail in %r" % pci_ids_locations
        )

    with open(pci_ids_location) as f:
        vid = None
        did = None
        for line in f:
            if line.startswith('#'):
                continue
            if line.startswith('C '):
                vid = None
                continue
            if len(line.strip()) == 0:
                continue
            parts = line.split('  ', 1)
            if parts[0].startswith("\t\t"):
                if not (vid and did):
                    continue
                subvid, subdid = parts[0].split()
                subvid = int(subvid, 16)
                subdid = int(subdid, 16)
                name = parts[1].strip()
                db[(vid, did, subvid, subdid)] = name
                continue
            if parts[0].startswith("\t"):
                if not vid:
                    continue
                did = parts[0].strip()
                did = int(did, 16)
                name = parts[1].strip()
                db[(vid, did)] = name
                continue
            vid = parts[0]
            vid = int(vid, 16)
            name = parts[1].strip()
            db[vid] = name
            did = None
    return db