diff options
author | Zuul <zuul@review.opendev.org> | 2023-05-04 09:04:30 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2023-05-04 09:04:30 +0000 |
commit | 44d9c2219fd6d17a0aec063331187f147ab4ab11 (patch) | |
tree | c8cae8e1c96e1213c1496738563f2572f56f33c1 /ironic_python_agent/utils.py | |
parent | c05fdf790c3cab6a18ca5b264e258c5c0016918d (diff) | |
parent | 9ed232e77e976fe22a5888d17a510374a812edd7 (diff) | |
download | ironic-python-agent-44d9c2219fd6d17a0aec063331187f147ab4ab11.tar.gz |
Merge "Add network interface speed to the inventory"
Diffstat (limited to 'ironic_python_agent/utils.py')
-rw-r--r-- | ironic_python_agent/utils.py | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/ironic_python_agent/utils.py b/ironic_python_agent/utils.py index c490b1af..c5889e9b 100644 --- a/ironic_python_agent/utils.py +++ b/ironic_python_agent/utils.py @@ -919,13 +919,28 @@ def rescan_device(device): 'to settle. Error: %s', e) -def find_in_lshw(lshw, by_id): +def _lshw_matches(item, by_id, fields): + lshw_id = item.get('id', '') + if isinstance(by_id, re.Pattern): + if by_id.match(lshw_id) is None: + return False + elif by_id is not None and by_id != lshw_id: + return False + + for key, value in fields.items(): + if item.get(key) != value: + return False + + return True + + +def find_in_lshw(lshw, by_id=None, by_class=None, recursive=False, **fields): """Yield all suitable records from lshw.""" + # Cannot really pass class=... in Python + if by_class is not None: + fields['class'] = by_class for child in lshw.get('children', ()): - lshw_id = child.get('id', '') - if isinstance(by_id, re.Pattern): - if by_id.match(lshw_id) is not None: - yield child - else: - if by_id == lshw_id: - yield child + if _lshw_matches(child, by_id, fields): + yield child + if recursive: + yield from find_in_lshw(child, by_id, recursive=True, **fields) |