<commit_before># _*_ coding:latin-1 _*_

import csv
import columns
from component import *
import os, shutil
from preferences import BomPref

"""
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]
net = netlist object
headings = [list of headings to display in the BoM file]
prefs = BomPref object
"""

def WriteCSV(filename, groups, net, headings, prefs):
    
    filename = os.path.abspath(filename)
    
    #delimeter is assumed from file extension
    if filename.endswith(".csv"):
        delimiter = ","
    elif filename.endswith(".tsv") or filename.endswith(".txt"):
        delimiter = "\t"
    else:
        return False
        
    nGroups = len(groups)
    nTotal = sum([g.getCount() for g in groups])
    nFitted = sum([g.getCount() for g in groups if g.isFitted()])
    nBuild = nFitted * prefs.boards
        
    with open(filename, "w") as f:
    
        writer = csv.writer(f, delimiter=delimiter, lineterminator="\n")
        
        if not prefs.hideHeaders:
            if prefs.numberRows:
                writer.writerow(["Component"] + headings)
            else:
                writer.writerow(headings)
            
        count = 0
        rowCount = 1
        
        for i, group in enumerate(groups):
            if prefs.ignoreDNF and not group.isFitted(): continue
            
            row = group.getRow(headings)
            
            if prefs.numberRows:
                row = [str(rowCount)] + row
                
            #deal with unicode characters
            #row = [el.decode('latin-1') for el in row]
            writer.writerow(row)
            
            try:<commit_msg>force csv/tsv output to use quotes<commit_after># _*_ coding:latin-1 _*_

import csv
import columns
from component import *
import os, shutil
from preferences import BomPref

"""
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]
net = netlist object
headings = [list of headings to display in the BoM file]
prefs = BomPref object
"""

def WriteCSV(filename, groups, net, headings, prefs):
    
    filename = os.path.abspath(filename)
    
    #delimeter is assumed from file extension
    if filename.endswith(".csv"):
        delimiter = ","
    elif filename.endswith(".tsv") or filename.endswith(".txt"):
        delimiter = "\t"
    else:
        return False
        
    nGroups = len(groups)
    nTotal = sum([g.getCount() for g in groups])
    nFitted = sum([g.getCount() for g in groups if g.isFitted()])
    nBuild = nFitted * prefs.boards
        
    with open(filename, "w") as f:
    
        writer = csv.writer(f, delimiter=delimiter, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
        
        if not prefs.hideHeaders:
            if prefs.numberRows:
                writer.writerow(["Component"] + headings)
            else:
                writer.writerow(headings)
            
        count = 0
        rowCount = 1
        
        for i, group in enumerate(groups):
            if prefs.ignoreDNF and not group.isFitted(): continue
            
            row = group.getRow(headings)
            
            if prefs.numberRows:
                row = [str(rowCount)] + row
                
            #deal with unicode characters
            #row = [el.decode('latin-1') for el in row]
            writer.writerow(row)
            
            try:
{'fraction_non_alphanumeric': 0.09779811266800115, 'fraction_numerical': 0.002287675150128682, 'mean_word_length': 1.9271966527196653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7177502', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1022, 'n_words': 313}