Better error handling of non-ASCII chars in BOM https://github.com/rusefi/hd81/issues/53

This commit is contained in:
andreika-git 2024-08-23 00:00:03 +03:00
parent 66454b441f
commit 1aea4cba9d
3 changed files with 21 additions and 10 deletions

View File

@ -24,9 +24,9 @@ with open(bomFileName, 'rt') as f, open (mfrBomFileName, 'wt') as new_f:
if not pn:
continue
if rowIdx == 0:
writer = csv.writer(new_f, quoting=csv.QUOTE_NONE, quotechar='"', escapechar='', delimiter=',', lineterminator='\n')
writer = csv.writer(new_f, quoting=csv.QUOTE_NONE, quotechar='"', escapechar=None, delimiter=',', lineterminator='\n')
elif rowIdx == 1:
writer = csv.writer(new_f, quoting=csv.QUOTE_ALL, quotechar='"', escapechar='', delimiter=',', lineterminator='\n')
writer = csv.writer(new_f, quoting=csv.QUOTE_ALL, quotechar='"', escapechar=None, delimiter=',', lineterminator='\n')
writer.writerow(row)
rowIdx += 1

View File

@ -9,14 +9,24 @@ import csv, os, sys, re
include_pat = re.compile(r'#include\s+\"?([^\"]+)\"?$')
def checkNonAsciiSymbols(row):
for r in row:
try:
r.encode('ascii')
except UnicodeEncodeError:
print ("Error! The following line contains non-ASCII symbols:")
print (row)
sys.exit(5)
def read_repl_file(csv_name, repl_base_path, replList):
print ("Reading replacement list from the CSV file " + csv_name + "...")
with open(csv_name, 'rt') as f:
with open(csv_name, 'rt', errors='replace') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
# skip empty lines
if (len(row) < 1):
continue
checkNonAsciiSymbols(row)
# process includes
include = include_pat.match(row[0].strip())
if (include):
@ -59,10 +69,11 @@ rows = OrderedDict()
rowDes = OrderedDict()
emptyId = 1
with open(fileName, 'rt') as f:
with open(fileName, 'rt', errors='replace') as f:
reader = csv.reader(f, delimiter=',')
print ("Searching for duplicates...")
for row in reader:
checkNonAsciiSymbols(row)
row[3] = row[3].strip()
rowName = row[3]
row[1] = row[1].split(", ")
@ -150,9 +161,9 @@ with open (fileName, 'wt') as new_f:
#for idx,item in enumerate(rows[rowName]):
# print idx , ": ", item
if rowIdx == 0:
writer = csv.writer(new_f, quoting=csv.QUOTE_NONE, quotechar='"', escapechar='', delimiter=',', lineterminator='\n')
writer = csv.writer(new_f, quoting=csv.QUOTE_NONE, quotechar='"', escapechar=None, delimiter=',', lineterminator='\n')
elif rowIdx == 1:
writer = csv.writer(new_f, quoting=csv.QUOTE_ALL, quotechar='"', escapechar='', delimiter=',', lineterminator='\n')
writer = csv.writer(new_f, quoting=csv.QUOTE_ALL, quotechar='"', escapechar=None, delimiter=',', lineterminator='\n')
row = rows[rowName]
# restore empty names
if rowName[0] == '_':

View File

@ -356,9 +356,9 @@ try:
board_bom,
bom_replace_csv_path,
warnings_path], stderr=subprocess.STDOUT)
print (out.decode('ascii'))
print (out.decode('ascii', errors='replace'))
except subprocess.CalledProcessError as e:
print ("BOM processing error:\n" + e.output.decode('ascii'))
print ("BOM processing error:\n" + e.output.decode('ascii', errors='replace'))
sys.exit(2)
print ("Convert to the manufacturer's BOM...")
@ -366,9 +366,9 @@ try:
out = subprocess.check_output([sys.executable, "bin/convert_BOM_mfr.py",
board_bom,
board_bom_mfr], stderr=subprocess.STDOUT)
print (out.decode('ascii'))
print (out.decode('ascii', errors='replace'))
except subprocess.CalledProcessError as e:
print ("Mfr's BOM conversion error:\n" + e.output.decode('ascii'))
print ("Mfr's BOM conversion error:\n" + e.output.decode('ascii', errors='replace'))
sys.exit(2)
print ("Merging Schematics...")