dtlib: wrap array properties in string representation

List each element of a property array in a different line to improve
the readability of the generated DTS file.
Update the test suite's expected outputs accordingly.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
This commit is contained in:
Luca Burelli 2025-05-02 11:20:54 +02:00 committed by Benjamin Cabé
commit 6b325daa2a
2 changed files with 31 additions and 13 deletions

View file

@ -207,7 +207,8 @@ class Node:
s += f"{self.name} {{\n"
for prop in self.props.values():
s += "\t" + str(prop) + "\n"
prop_str = textwrap.indent(str(prop), "\t")
s += prop_str + "\n"
for child in self.nodes.values():
s += textwrap.indent(child.__str__(), "\t") + "\n"
@ -588,6 +589,7 @@ class Property:
return s + ";"
s += " ="
newline = "\n" + " " * len(s)
for i, (pos, marker_type, ref) in enumerate(self._markers):
if i < len(self._markers) - 1:
@ -602,11 +604,11 @@ class Property:
# end - 1 to strip off the null terminator
s += f' "{_decode_and_escape(self.value[pos:end - 1])}"'
if end != len(self.value):
s += ","
s += f",{newline}"
elif marker_type is _MarkerType.PATH:
s += " &" + ref
if end != len(self.value):
s += ","
s += f",{newline}"
else:
# <> or []
@ -638,7 +640,7 @@ class Property:
s += _N_BYTES_TO_END_STR[elm_size]
if pos != len(self.value):
s += ","
s += f",{newline}"
return s + ";"

View file

@ -530,7 +530,9 @@ def test_property_offset_labels():
/dts-v1/;
/ {
a = l01: l02: < l03: &node l04: l05: 0x2 l06: l07: l08: >, [ l09: 03 l10: l11: 04 l12: l13: l14: ], "A";
a = l01: l02: < l03: &node l04: l05: 0x2 l06: l07: l08: >,
[ l09: 03 l10: l11: 04 l12: l13: l14: ],
"A";
b = < 0x0 l23: l24: >;
node: node {
phandle = < 0x1 >;
@ -610,8 +612,11 @@ def test_node_path_references():
/ {
a = &label;
b = [ 01 ], &label;
c = [ 01 ], &label, < 0x2 >;
b = [ 01 ],
&label;
c = [ 01 ],
&label,
< 0x2 >;
d = &{/abc};
label: abc {
e = &label;
@ -867,7 +872,12 @@ def test_mixed_assign():
/dts-v1/;
/ {
x = [ FF FF ], &abc, < 0xff &abc 0xff &abc >, &abc, [ FF FF ], "abc";
x = [ FF FF ],
&abc,
< 0xff &abc 0xff &abc >,
&abc,
[ FF FF ],
"abc";
abc: abc {
phandle = < 0x1 >;
};
@ -1180,7 +1190,8 @@ def test_omit_if_no_ref():
/dts-v1/;
/ {
x = < &{/referenced} >, &referenced2;
x = < &{/referenced} >,
&referenced2;
referenced {
phandle = < 0x1 >;
};
@ -1775,7 +1786,7 @@ def test_prop_type_casting():
"strings",
"expected property 'strings' on / in .* to be assigned with " +
re.escape("'strings = \"string\";', ")+
re.escape("not 'strings = \"foo\", \"bar\", \"baz\";'"))
"not 'strings = \"foo\",\\s*\"bar\",\\s*\"baz\";'")
verify_to_string_error_matches(
"invalid_string",
re.escape(r"value of property 'invalid_string' (b'\xff\x00') on / ") +
@ -2076,7 +2087,9 @@ def test_duplicate_labels():
/ {
label: foo {
x = &{/foo}, &label, < &label >;
x = &{/foo},
&label,
< &label >;
phandle = < 0x1 >;
};
};
@ -2175,7 +2188,8 @@ def test_names():
/dts-v1/;
/ {
aA0,._+*#?- = &_, &{/aA0,._+@-};
aA0,._+*#?- = &_,
&{/aA0,._+@-};
+ = [ 00 ];
* = [ 02 ];
- = [ 01 ];
@ -2232,7 +2246,9 @@ def test_dense_input():
/ {
l1: l2: foo {
l3: l4: bar {
l5: x = l6: [ l7: 01 l8: 02 l9: ], [ 03 ], "a";
l5: x = l6: [ l7: 01 l8: 02 l9: ],
[ 03 ],
"a";
};
};
};