Skip to content

Commit 6b325da

Browse files
pillo79kartben
authored andcommitted
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 <[email protected]>
1 parent 4b8be38 commit 6b325da

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def __str__(self):
207207
s += f"{self.name} {{\n"
208208

209209
for prop in self.props.values():
210-
s += "\t" + str(prop) + "\n"
210+
prop_str = textwrap.indent(str(prop), "\t")
211+
s += prop_str + "\n"
211212

212213
for child in self.nodes.values():
213214
s += textwrap.indent(child.__str__(), "\t") + "\n"
@@ -588,6 +589,7 @@ def __str__(self):
588589
return s + ";"
589590

590591
s += " ="
592+
newline = "\n" + " " * len(s)
591593

592594
for i, (pos, marker_type, ref) in enumerate(self._markers):
593595
if i < len(self._markers) - 1:
@@ -602,11 +604,11 @@ def __str__(self):
602604
# end - 1 to strip off the null terminator
603605
s += f' "{_decode_and_escape(self.value[pos:end - 1])}"'
604606
if end != len(self.value):
605-
s += ","
607+
s += f",{newline}"
606608
elif marker_type is _MarkerType.PATH:
607609
s += " &" + ref
608610
if end != len(self.value):
609-
s += ","
611+
s += f",{newline}"
610612
else:
611613
# <> or []
612614

@@ -638,7 +640,7 @@ def __str__(self):
638640

639641
s += _N_BYTES_TO_END_STR[elm_size]
640642
if pos != len(self.value):
641-
s += ","
643+
s += f",{newline}"
642644

643645
return s + ";"
644646

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ def verify_label2offset(label, expected_prop, expected_offset):
530530
/dts-v1/;
531531
532532
/ {
533-
a = l01: l02: < l03: &node l04: l05: 0x2 l06: l07: l08: >, [ l09: 03 l10: l11: 04 l12: l13: l14: ], "A";
533+
a = l01: l02: < l03: &node l04: l05: 0x2 l06: l07: l08: >,
534+
[ l09: 03 l10: l11: 04 l12: l13: l14: ],
535+
"A";
534536
b = < 0x0 l23: l24: >;
535537
node: node {
536538
phandle = < 0x1 >;
@@ -610,8 +612,11 @@ def test_node_path_references():
610612
611613
/ {
612614
a = &label;
613-
b = [ 01 ], &label;
614-
c = [ 01 ], &label, < 0x2 >;
615+
b = [ 01 ],
616+
&label;
617+
c = [ 01 ],
618+
&label,
619+
< 0x2 >;
615620
d = &{/abc};
616621
label: abc {
617622
e = &label;
@@ -867,7 +872,12 @@ def test_mixed_assign():
867872
/dts-v1/;
868873
869874
/ {
870-
x = [ FF FF ], &abc, < 0xff &abc 0xff &abc >, &abc, [ FF FF ], "abc";
875+
x = [ FF FF ],
876+
&abc,
877+
< 0xff &abc 0xff &abc >,
878+
&abc,
879+
[ FF FF ],
880+
"abc";
871881
abc: abc {
872882
phandle = < 0x1 >;
873883
};
@@ -1180,7 +1190,8 @@ def test_omit_if_no_ref():
11801190
/dts-v1/;
11811191
11821192
/ {
1183-
x = < &{/referenced} >, &referenced2;
1193+
x = < &{/referenced} >,
1194+
&referenced2;
11841195
referenced {
11851196
phandle = < 0x1 >;
11861197
};
@@ -1775,7 +1786,7 @@ def verify_to_string_error_matches(prop, expected_re):
17751786
"strings",
17761787
"expected property 'strings' on / in .* to be assigned with " +
17771788
re.escape("'strings = \"string\";', ")+
1778-
re.escape("not 'strings = \"foo\", \"bar\", \"baz\";'"))
1789+
"not 'strings = \"foo\",\\s*\"bar\",\\s*\"baz\";'")
17791790
verify_to_string_error_matches(
17801791
"invalid_string",
17811792
re.escape(r"value of property 'invalid_string' (b'\xff\x00') on / ") +
@@ -2076,7 +2087,9 @@ def test_duplicate_labels():
20762087
20772088
/ {
20782089
label: foo {
2079-
x = &{/foo}, &label, < &label >;
2090+
x = &{/foo},
2091+
&label,
2092+
< &label >;
20802093
phandle = < 0x1 >;
20812094
};
20822095
};
@@ -2175,7 +2188,8 @@ def test_names():
21752188
/dts-v1/;
21762189
21772190
/ {
2178-
aA0,._+*#?- = &_, &{/aA0,._+@-};
2191+
aA0,._+*#?- = &_,
2192+
&{/aA0,._+@-};
21792193
+ = [ 00 ];
21802194
* = [ 02 ];
21812195
- = [ 01 ];
@@ -2232,7 +2246,9 @@ def test_dense_input():
22322246
/ {
22332247
l1: l2: foo {
22342248
l3: l4: bar {
2235-
l5: x = l6: [ l7: 01 l8: 02 l9: ], [ 03 ], "a";
2249+
l5: x = l6: [ l7: 01 l8: 02 l9: ],
2250+
[ 03 ],
2251+
"a";
22362252
};
22372253
};
22382254
};

0 commit comments

Comments
 (0)