Skip to content

Commit

Permalink
edtlib: extract _slice() code to new helper module
Browse files Browse the repository at this point in the history
This will make it more convenient to use it from multiple different
places, which we will have a need for in the future.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
  • Loading branch information
mbolivar-nordic committed Feb 23, 2023
1 parent 40b1da3 commit 7366154
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
30 changes: 30 additions & 0 deletions scripts/dts/python-devicetree/src/devicetree/_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2019 Nordic Semiconductor ASA
# Copyright (c) 2019 Linaro Limited
# SPDX-License-Identifier: BSD-3-Clause

"""
Shared internal code. Do not use outside of the package.
"""

from typing import Any, Callable

def _slice_helper(node: Any, # avoids a circular import with dtlib
prop_name: str, size: int, size_hint: str,
err_class: Callable[..., Exception]):
# Splits node.props[prop_name].value into 'size'-sized chunks,
# returning a list of chunks. Raises err_class(...) if the length
# of the property is not evenly divisible by 'size'. The argument
# to err_class is a string which describes the error.
#
# 'size_hint' is a string shown on errors that gives a hint on how
# 'size' was calculated.

raw = node.props[prop_name].value
if len(raw) % size:
raise err_class(
f"'{prop_name}' property in {node!r} has length {len(raw)}, "
f"which is not evenly divisible by {size} (= {size_hint}). "
"Note that #*-cells properties come either from the parent node or "
"from the controller (in the case of 'interrupts').")

return [raw[i:i + size] for i in range(0, len(raw), size)]
16 changes: 2 additions & 14 deletions scripts/dts/python-devicetree/src/devicetree/edtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

from devicetree.dtlib import DT, DTError, to_num, to_nums, Type
from devicetree.grutils import Graph

from devicetree._private import _slice_helper

#
# Public classes
Expand Down Expand Up @@ -2954,19 +2954,7 @@ def _interrupt_cells(node):


def _slice(node, prop_name, size, size_hint):
# Splits node.props[prop_name].value into 'size'-sized chunks, returning a
# list of chunks. Raises EDTError if the length of the property is not
# evenly divisible by 'size'. 'size_hint' is a string shown on errors that
# gives a hint on how 'size' was calculated.

raw = node.props[prop_name].value
if len(raw) % size:
_err(f"'{prop_name}' property in {node!r} has length {len(raw)}, "
f"which is not evenly divisible by {size} (= {size_hint}). "
"Note that #*-cells properties come either from the parent node or "
"from the controller (in the case of 'interrupts').")

return [raw[i:i + size] for i in range(0, len(raw), size)]
return _slice_helper(node, prop_name, size, size_hint, EDTError)


def _check_dt(dt):
Expand Down

0 comments on commit 7366154

Please sign in to comment.