-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
edtlib: extract _slice() code to new helper module
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
1 parent
40b1da3
commit 7366154
Showing
2 changed files
with
32 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters