Skip to content

Commit 0a4e2e3

Browse files
pillo79danieldegrasse
authored andcommitted
scripts: dts: dtlib: improve formatting of long arrays
Split long arrays into multiple lines to improve readability of the output DTS file. A new line is started when the array data exceeds 80 characters. Add a few test entries to verify the new behavior. Note: the F821 linter suppression prevents flagging 'array_start' and 'array_newline' as undefined variables. This is because these variables are initialized when an opening brace is output, which is necessarily before any element in a byte or prop array. A sequence of markers not following this pattern would indicate a bug in the DTS parsing code. Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
1 parent f26a8c0 commit 0a4e2e3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

scripts/dts/python-devicetree/src/devicetree/dtlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,16 @@ def __str__(self):
641641
elif marker_type is _MarkerType.PHANDLE:
642642
s += " &" + ref
643643
pos += 4
644+
if pos != len(self.value) and len(s) - array_start > 80: # noqa: F821
645+
s += array_newline # noqa: F821
646+
array_start = len(s)
644647
# Subtle: There might be more data between the phandle and
645648
# the next marker, so we can't 'continue' here
646649
else: # marker_type is _MarkerType.UINT*
647650
elm_size = _TYPE_TO_N_BYTES[marker_type]
648651
s += _N_BYTES_TO_START_STR[elm_size]
652+
array_start = len(s)
653+
array_newline = "\n" + " " * array_start
649654

650655
while pos != end:
651656
num = int.from_bytes(self.value[pos:pos + elm_size],
@@ -656,6 +661,9 @@ def __str__(self):
656661
s += f" {hex(num)}"
657662

658663
pos += elm_size
664+
if pos != len(self.value) and len(s) - array_start > 80:
665+
s += array_newline
666+
array_start = len(s)
659667

660668
if (pos != 0
661669
and (not next_marker

scripts/dts/python-devicetree/tests/test_dtlib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def test_cell_parsing():
163163
j = /bits/ 32 < 0x10 0x20 (-1) >;
164164
k = /bits/ 64 < 0x10 0x20 (-1) >;
165165
l = < 'a' 'b' 'c' >;
166+
m = [ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b ];
167+
n = < 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >;
166168
};
167169
""",
168170
"""
@@ -181,6 +183,10 @@ def test_cell_parsing():
181183
j = < 0x10 0x20 0xffffffff >;
182184
k = /bits/ 64 < 0x10 0x20 0xffffffffffffffff >;
183185
l = < 0x61 0x62 0x63 >;
186+
m = [ 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A
187+
1B ];
188+
n = < 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 0x11 0x12 0x13
189+
0x14 >;
184190
};
185191
""")
186192

0 commit comments

Comments
 (0)