Skip to content

Commit b97785c

Browse files
v0.2.2 release commit
1 parent 464b17c commit b97785c

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

src/dts_utils/filters.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
from typing import Optional, Any
1718
from .exceptions import InvalidPropertyValue, InvalidTemplateValueType, InvalidPropertyValueType
1819
from .node import Node
1920

@@ -198,3 +199,89 @@ def f_enabled(value: list[Node]) -> list[Node]:
198199
raise InvalidTemplateValueType("enabled", type(value), list)
199200

200201
return list(filter(lambda x: (is_enabled(x) != 0), value))
202+
203+
204+
def f_has_property(value: Node, node_property: str, expected_value: Optional[Any] = None) -> bool:
205+
"""has_property custom jinja filter.
206+
207+
Returns True if given node has the property.
208+
Optionally, check property value to expected if given.
209+
210+
Parameters
211+
----------
212+
value: Node
213+
DTS node to filter
214+
node_property: str
215+
Property name
216+
expected_value: Optional[Any]
217+
Property expected value, optional
218+
219+
Returns
220+
-------
221+
bool
222+
True if Node holds the given property
223+
If expected is provided, True if property value matches.
224+
False otherwise.
225+
226+
Raises
227+
------
228+
InvalidTemplateValueType
229+
if `value` is not of Node type
230+
if `node_property` is not of str type
231+
232+
Notes
233+
-----
234+
For boolean property, no expected value is needed, in devicetree, boolean property has
235+
no value, present means true, false otherwise.
236+
"""
237+
if not isinstance(value, Node):
238+
raise InvalidTemplateValueType("has_property", type(value), Node)
239+
if not isinstance(node_property, str):
240+
raise InvalidTemplateValueType("has_property", type(node_property), str)
241+
242+
prop = value._node.props.get(node_property)
243+
if not prop:
244+
return False
245+
elif not expected_value:
246+
# Property found but no expected value, return True
247+
return True
248+
else:
249+
# Otherwise, check expected
250+
return prop.value == expected_value
251+
252+
253+
def f_with_property(
254+
value: list[Node], node_property: str, expected_value: Optional[Any] = None
255+
) -> list[Node]:
256+
"""with_property custom jinja filter.
257+
258+
Filters the input list of Node and returns the list of Node with the given property
259+
260+
Parameters
261+
----------
262+
value: list[Node]
263+
DTS node list to filter
264+
node_property: str
265+
Property name
266+
expected_value: Optional[Any]
267+
Property expected value, optional
268+
269+
Returns
270+
-------
271+
list[Node]
272+
A filtered list w/ only nodes w/ `property_name` (optionally equals to expected)
273+
274+
Raises
275+
------
276+
InvalidTemplateValueType
277+
if `value` is not of Node type
278+
if `node_property` is not of str type
279+
"""
280+
if not isinstance(value, list):
281+
raise InvalidTemplateValueType("with_property", type(value), list)
282+
if not isinstance(node_property, str):
283+
raise InvalidTemplateValueType("with_property", type(node_property), str)
284+
285+
return list(
286+
filter(lambda node: (f_has_property(node, node_property, expected_value) != 0), value)
287+
)

0 commit comments

Comments
 (0)