1# Copyright (c) 2019 Nordic Semiconductor ASA
2# Copyright (c) 2019 Linaro Limited
3# SPDX-License-Identifier: BSD-3-Clause
4
5"""
6Shared internal code. Do not use outside of the package.
7"""
8
9from typing import Any, Callable
10
11def _slice_helper(node: Any,    # avoids a circular import with dtlib
12                  prop_name: str, size: int, size_hint: str,
13                  err_class: Callable[..., Exception]):
14    # Splits node.props[prop_name].value into 'size'-sized chunks,
15    # returning a list of chunks. Raises err_class(...) if the length
16    # of the property is not evenly divisible by 'size'. The argument
17    # to err_class is a string which describes the error.
18    #
19    # 'size_hint' is a string shown on errors that gives a hint on how
20    # 'size' was calculated.
21
22    raw = node.props[prop_name].value
23    if len(raw) % size:
24        raise err_class(
25            f"'{prop_name}' property in {node!r} has length {len(raw)}, "
26            f"which is not evenly divisible by {size} (= {size_hint}). "
27            "Note that #*-cells properties come either from the parent node or "
28            "from the controller (in the case of 'interrupts').")
29
30    return [raw[i:i + size] for i in range(0, len(raw), size)]
31