Last active
August 14, 2018 17:32
-
-
Save arpruss/92fe34c376ff0a0195bdda64c0ca3c09 to your computer and use it in GitHub Desktop.
Process wildcard templates to generate Arduino board files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
dict = {} | |
overrideDict = {} | |
ignore = set() | |
unknown = [] | |
equalRE = re.compile(r'\s*([^<=\s]+)\s*\=\s*(.*)') | |
copyRE = re.compile(r'\s*([^<=\s]+)\s*\<\=\s*(.*)') | |
negRE = re.compile(r'\s*\!\s*([^\s]+)') | |
def process(lines, dict): | |
unknown = [] | |
output = [] | |
changed = False | |
def wildKey(copyTo, copyFrom, source): | |
if not copyTo.endswith(".*"): | |
if copyFrom.endswith(".*"): | |
raise Exception("wildcard must be present in both source and target, or in neither") | |
return source | |
if not copyFrom.endswith(".*"): | |
raise Exception("wildcard must be present in both source and target, or in neither") | |
return copyTo[:-2] + source[len(copyFrom)-2:] | |
def wildMatch(wild, key): | |
if not wild.endswith('.*'): | |
return wild == key | |
return wild[:-1] == key[:len(wild)-1] | |
def ignorable(key): | |
for ign in ignore: | |
if wildMatch(ign, key): | |
return True | |
return False | |
def updateDict(key, value): | |
if key not in dict or dict[key] != value: | |
dict[key] = value | |
return True | |
return False | |
for i,line in enumerate(lines): | |
clean = re.sub("\s*\#.*", "", line) | |
m = equalRE.match(clean) | |
if m: | |
changed = updateDict(m.group(1), m.group(2)) or changed | |
overrideDict[m.group(1)] = m.group(2) | |
if m.group(1)[0] != '$': | |
output.append(line) | |
else: | |
m = negRE.match(clean) | |
if m: | |
if m.group(1) not in ignore: | |
changed = True | |
ignore.add(m.group(1)) | |
else: | |
m = copyRE.match(clean) | |
if m: | |
if not m.group(2).endswith('.*'): | |
if m.group(2) in dict: | |
key = m.group(1) | |
if key[0] != '$': | |
output.append(key+" = "+dict[m.group(2)]) | |
changed = updateDict(key, dict[m.group(2)]) or changed | |
overrideDict[key] = m.group(2) | |
else: | |
output.append(line + " ## UNKNOWN") | |
unknown.append(i) | |
else: | |
for dictKey in sorted(dict): | |
if wildMatch(m.group(2), dictKey): | |
lineKey = wildKey(m.group(1), m.group(2), dictKey) | |
if lineKey not in overrideDict and not ignorable(lineKey): | |
if lineKey[0] != '$': | |
output.append(lineKey+" = "+dict[dictKey]) | |
changed = updateDict(lineKey, dict[dictKey]) or changed | |
else: | |
output.append(line) | |
return changed, output, unknown | |
with open(sys.argv[1]) as f: | |
lines = tuple(line.strip() for line in f) | |
iter = 10 | |
changed = True | |
while (changed or unknown) and iter: | |
changed, output, unknown = process(lines, dict) | |
iter-=1 | |
if unknown: | |
raise Exception("Cannot process lines: "+','.join((str(i) for i in unknown))+"\n") | |
sys.exit(1) | |
for line in output: | |
print(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment