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