Python scripting tracks / vias in PcbNew?

This may help - I think this creates a file with the constructed info, that is then added using append.

Yes, look at a kicad_pcb file and each track segment has a net-number, that is derived from the net-name table.
Without that, the DRC does not connect things properly.