1"""Utility functions not closely tied to other spec_tools types.""" 2# Copyright (c) 2018-2019 Collabora, Ltd. 3# Copyright 2013-2024 The Khronos Group Inc. 4# SPDX-License-Identifier: Apache-2.0 5 6 7def getElemName(elem, default=None): 8 """Get the name associated with an element, either a name child or name attribute.""" 9 name_elem = elem.find('name') 10 if name_elem is not None: 11 return name_elem.text 12 # Fallback if there is no child. 13 return elem.get('name', default) 14 15 16def getElemType(elem, default=None): 17 """Get the type associated with an element, either a type child or type attribute.""" 18 type_elem = elem.find('type') 19 if type_elem is not None: 20 return type_elem.text 21 # Fallback if there is no child. 22 return elem.get('type', default) 23 24 25def findFirstWithPredicate(collection, pred): 26 """Return the first element that satisfies the predicate, or None if none exist. 27 28 NOTE: Some places where this is used might be better served by changing to a dictionary. 29 """ 30 for elt in collection: 31 if pred(elt): 32 return elt 33 return None 34 35 36def findNamedElem(elems, name): 37 """Traverse a collection of elements with 'name' nodes or attributes, looking for and returning one with the right name. 38 39 NOTE: Many places where this is used might be better served by changing to a dictionary. 40 """ 41 return findFirstWithPredicate(elems, lambda elem: getElemName(elem) == name) 42 43 44def findTypedElem(elems, typename): 45 """Traverse a collection of elements with 'type' nodes or attributes, looking for and returning one with the right typename. 46 47 NOTE: Many places where this is used might be better served by changing to a dictionary. 48 """ 49 return findFirstWithPredicate(elems, lambda elem: getElemType(elem) == typename) 50 51 52def findNamedObject(collection, name): 53 """Traverse a collection of elements with 'name' attributes, looking for and returning one with the right name. 54 55 NOTE: Many places where this is used might be better served by changing to a dictionary. 56 """ 57 return findFirstWithPredicate(collection, lambda elt: elt.name == name) 58