stdout CSV rows misalign when initial list items have different field sets
Describe the bug
--stdout-csv still produces data rows with a different width from the header when list items have different field sets on the first export refresh. The header includes each item's own keys, but header_field_names stores only the first item's keys and reuses those for every item.
This is a remaining case after #3606 / #3609, rather than the already-fixed case of an interface appearing or disappearing after the header.
Reproduction
On develop b3f1cf008e9ad28eb02e1bb63d74f980683fedf6:
from types import SimpleNamespace
from glances.outputs.glances_stdout_csv import GlancesStdoutCsv
output = GlancesStdoutCsv(args=SimpleNamespace(stdout_csv="network"))
stats = [
{"interface_name": "eth0", "key": "interface_name", "bytes_sent": 1},
{"interface_name": "wlan0", "key": "interface_name", "bytes_sent": 2,
"bytes_sent_rate_per_sec": 2.0},
]
print(output.build_header("network", None, stats))
print(output.build_data("network", None, stats))The header has 7 columns, while the data row has 6. The second interface's rate column is omitted. With the full and partial network snapshots from the existing test suite, the same case produces 21 header columns and 14 data columns. Reversing which interface has fewer fields produces extra columns instead.
Expected behavior
Lock the ordered field list separately for each item identity, so data rows use exactly the fields that were written for that item's header block. Missing fields should remain N/A, and items appearing later should remain omitted as before.
Environment
Glances 4.5.8.dev1 source checkout, Python 3.12.13, macOS. Reproduced by calling the CSV output with fixed network snapshots; no dependency on live network interfaces.
Source: nicolargo/glances