Created
January 13, 2026 06:34
-
-
Save SpareSimian/7ced6ec92eb6566e8a0acce5591af0b9 to your computer and use it in GitHub Desktop.
Parse PCI ID database and return as Python dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def read(): | |
| re_vendor = re.compile(r'(?P<vendor>[a-z0-9]{4})\s+(?P<vendor_name>.*)') | |
| re_device = re.compile(r'\t(?P<device>[a-z0-9]{4})\s+(?P<device_name>.*)') | |
| re_subsys = re.compile(r'\t\t(?P<subvendor>[a-z0-9]{4})\s+(?P<subdevice>[a-z0-9]{4})\s+(?P<subsystem_name>.*)') | |
| data = {} | |
| vendor = '' | |
| device = '' | |
| with open("pci.ids", "rt") as fp: | |
| for line in fp: | |
| m = re_vendor.match(line) | |
| if m: | |
| d = m.groupdict() | |
| d['devices'] = {} | |
| vendor = m.group('vendor') | |
| data[vendor] = d | |
| else: | |
| m = re_device.match(line) | |
| if m: | |
| d = m.groupdict() | |
| d['subdevices'] = [] | |
| device = m.group('device') | |
| data[vendor]['devices'][device] = d | |
| else: | |
| m = re_subsys.match(line) | |
| if m: | |
| data[vendor]['devices'][device]['subdevices'].append(m.groupdict()) | |
| return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment