kellyjonbrazil / jc

CLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
MIT License
7.81k stars 201 forks source link

[parsers /pci_ids.py] Only last [subvendor][subdevice] got loaded into result when multiple subdevices having the same subvendor id #558

Closed ssiuhk closed 3 months ago

ssiuhk commented 5 months ago

Description It was found that when parsing pci_ids, only the last subvendor/subdevice data is available in parsed data when there are multiple subdevices sharing the same subvendor id.

Steps to Reproduce the behavior

Python 3.9.18 (main, Jan 24 2024, 00:00:00)
[GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jc
>>> with open('/usr/share/hwdata/pci.ids', 'r', encoding='utf-8') as pcifp:
...     pci_data_str = pcifp.read()
...
>>> pci_ids = jc.parse('pci_ids', pci_data_str)
>>> from pprint import pprint
>>> pprint(pci_ids['vendors'].get("_168c", {}).get("_0012"))
{'_1186': {'_3a05': {'subsystem_name': 'AirPro DWL-AB520 Multimode Wireless '
                                       'PCI Adapter'}},
 '_126c': {'_8031': {'subsystem_name': '2201 Mobile Adapter'}},
 '_1385': {'_4400': {'subsystem_name': 'WAB501 802.11ab Wireless CardBus '
                                       'Card'}},
 '_1b47': {'_aa00': {'subsystem_name': '8460 802.11ab Wireless CardBus '
                                       'Adapter'}},
 'device_name': 'AR5211 Wireless Network Adapter [AR5001X 802.11ab]'}

===============================================================

168c  Qualcomm Atheros
        0012  AR5211 Wireless Network Adapter [AR5001X 802.11ab]
                1186 3a03  AirPro DWL-A650 Wireless Cardbus Adapter (rev.B)
                1186 3a04  AirPro DWL-AB650 Multimode Wireless Cardbus Adapter
                1186 3a05  AirPro DWL-AB520 Multimode Wireless PCI Adapter
                126c 8031  2201 Mobile Adapter
                1385 4400  WAB501 802.11ab Wireless CardBus Card
                1b47 aa00  8460 802.11ab Wireless CardBus Adapter

Expected behavior The output of pprint(pci_ids['vendors'].get("_168c", {}).get("_0012")) should be like

{'_1186': {'_3a03': {'subsystem_name': 'AirPro DWL-A650 Wireless Cardbus '
                                       'Adapter (rev.B)'},
           '_3a04': {'subsystem_name': 'AirPro DWL-AB650 Multimode Wireless '
                                       'Cardbus Adapter'},
           '_3a05': {'subsystem_name': 'AirPro DWL-AB520 Multimode Wireless '
                                       'PCI Adapter'}},
 '_126c': {'_8031': {'subsystem_name': '2201 Mobile Adapter'}},
 '_1385': {'_4400': {'subsystem_name': 'WAB501 802.11ab Wireless CardBus '
                                       'Card'}},
 '_1b47': {'_aa00': {'subsystem_name': '8460 802.11ab Wireless CardBus '
                                       'Adapter'}},
 'device_name': 'AR5211 Wireless Network Adapter [AR5001X 802.11ab]'}

Versions Report

(myenv) [ssiu@myhost tmp.hAUtVFADtV]$ pip list
Package          Version
---------------- -------
jc               1.25.2  <=======
pip              24.0
Pygments         2.17.2
ruamel.yaml      0.18.6
ruamel.yaml.clib 0.2.8
setuptools       53.0.0
xmltodict        0.13.0

Python 3.9.18 (main, Jan 24 2024, 00:00:00)
[GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] on linux
kellyjonbrazil commented 5 months ago

Thanks for reporting this and for the fix! I'll run it through some tests and update the docs.

kellyjonbrazil commented 4 months ago

The fix is now in the dev branch and will be in the next release.

https://github.com/kellyjonbrazil/jc/blob/dev/jc/parsers/pci_ids.py

You can test by copying the parser file above into your parser plugin folder:

kellyjonbrazil commented 3 months ago

Fixed in v1.25.3