Skip to content

Commit f659bc7

Browse files
committed
Impl. #define for repl_csv #395
1 parent dd98942 commit f659bc7

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

bin/process_BOM.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import csv, os, sys, re
99

1010
include_pat = re.compile(r'#include\s+\"?([^\"]+)\"?$')
11+
define_pat = re.compile(r'#define\s+([A-Za-z0-9_]+)\s+([A-Za-z0-9_]+)$')
1112

1213
def checkNonAsciiSymbols(row):
1314
for r in row:
@@ -18,7 +19,7 @@ def checkNonAsciiSymbols(row):
1819
print (row)
1920
sys.exit(5)
2021

21-
def read_repl_file(csv_name, repl_base_path, replList):
22+
def read_repl_file(csv_name, repl_base_path, replList, defList):
2223
print ("Reading replacement list from the CSV file " + csv_name + "...")
2324
with open(csv_name, 'rt', errors='replace') as f:
2425
reader = csv.reader(f, delimiter=',')
@@ -32,7 +33,20 @@ def read_repl_file(csv_name, repl_base_path, replList):
3233
if (include):
3334
csv_sub_name = os.path.join(repl_base_path, include.group(1))
3435
if (csv_sub_name != csv_name):
35-
read_repl_file(csv_sub_name, repl_base_path, replList)
36+
read_repl_file(csv_sub_name, repl_base_path, replList, defList)
37+
continue
38+
# process defines
39+
define = define_pat.match(row[0].strip())
40+
if (define):
41+
old_value = define.group(1)
42+
new_value = define.group(2)
43+
44+
# Optional duplicate check
45+
if (old_value in defList and defList[old_value] != new_value):
46+
print (f'Warning: redefine {old_value}: {defList[old_value]} -> {new_value}')
47+
48+
defList[old_value] = new_value
49+
continue
3650
# skip comments (this is not strictly CSV-compliant, but useful for our purposes)
3751
if (row[0].startswith("#")):
3852
continue
@@ -42,7 +56,9 @@ def read_repl_file(csv_name, repl_base_path, replList):
4256
if (len(row) > 2):
4357
subrow.append(row[2])
4458
if (len(row) > 3):
45-
subrow.append(row[3])
59+
value = row[3].strip()
60+
value = defList.get(value, value)
61+
subrow.append(value)
4662
replList.append(subrow)
4763

4864
def printWarning(text):
@@ -101,7 +117,8 @@ def printWarning(text):
101117
# print idx , ": ", item
102118

103119
replList = list()
104-
read_repl_file(repl_csv, repl_base_path, replList)
120+
defList = dict()
121+
read_repl_file(repl_csv, repl_base_path, replList, defList)
105122

106123
print ("Processing the board replacements...")
107124
for r in replList:

0 commit comments

Comments
 (0)