# -*- coding: utf-8 -*- """ CSV Writer: This code is adapted from https://github.com/SchrodingersGat/KiBoM by Oliver Henry Walters. Generates a CSV, TSV or TXT file. """ import csv def write_csv(filename, ext, groups, headings, head_names, cfg): """ Write BoM out to a CSV file filename = path to output file (must be a .csv, .txt or .tsv file) groups = [list of ComponentGroup groups] headings = [list of headings to search for data in the BoM file] head_names = [list of headings to display in the BoM file] prefs = BomPref object """ # Delimeter is assumed from file extension # Override delimiter if separator specified if cfg.csv.separator: delimiter = cfg.csv.separator else: if ext == "csv": delimiter = "," elif ext == "tsv" or ext == "txt": delimiter = "\t" if cfg.csv.quote_all: quoting = csv.QUOTE_ALL else: quoting = csv.QUOTE_MINIMAL with open(filename, "wt") as f: writer = csv.writer(f, delimiter=delimiter, lineterminator="\n", quoting=quoting) # Headers writer.writerow(head_names) # Body for group in groups: if cfg.ignore_dnf and not group.is_fitted(): continue row = group.get_row(headings) writer.writerow(row) # PCB info if not (cfg.csv.hide_pcb_info and cfg.csv.hide_stats_info): # Add some blank rows for i in range(5): writer.writerow([]) # The info if not cfg.csv.hide_pcb_info: writer.writerow(["Project info:"]) writer.writerow(["Schematic:", cfg.source]) writer.writerow(["Variant:", ' + '.join(cfg.variant)]) writer.writerow(["Revision:", cfg.revision]) writer.writerow(["Date:", cfg.date]) writer.writerow(["KiCad Version:", cfg.kicad_version]) if not cfg.csv.hide_stats_info: writer.writerow(["Statistics:"]) writer.writerow(["Component Groups:", cfg.n_groups]) writer.writerow(["Component Count:", cfg.n_total]) writer.writerow(["Fitted Components:", cfg.n_fitted]) writer.writerow(["Number of PCBs:", cfg.number]) writer.writerow(["Total components:", cfg.n_build]) return True