Delete tweak.override attributes with null value

This commit is contained in:
KV 2021-09-12 23:19:44 +02:00
parent ba8f8423be
commit 4945d262a0
3 changed files with 13 additions and 4 deletions

View File

@ -345,10 +345,11 @@ Alternatively items can be added to just the BOM by putting them in the section
# Entries with an attribute containing HTML are
# not supported.
<str>: # leading string of .gv entry
<str> : <str> # attribute and its new value
<str> : <str/null> # attribute and its new value
# Any number of attributes can be overridden
# for each entry. Attributes not already existing
# in the entry will be appended to the entry.
# Use null as new value to delete an attribute.
append: <str/list> # string or list of strings to append to the .gv output
```

View File

@ -61,7 +61,7 @@ class Options:
@dataclass
class Tweak:
override: Optional[Dict[Designator, Dict[str, str]]] = None
override: Optional[Dict[Designator, Dict[str, Optional[str]]]] = None
append: Union[str, List[str], None] = None

View File

@ -357,7 +357,7 @@ class Harness:
typecheck(f'tweak.override.{k} value', d, dict)
for a, v in d.items():
typecheck(f'tweak.override.{k}.{a} key', a, str)
typecheck(f'tweak.override.{k}.{a} value', v, str)
typecheck(f'tweak.override.{k}.{a} value', v, (str, type(None)))
# Override generated attributes of selected entries matching tweak.override.
for i, entry in enumerate(dot.body):
@ -367,7 +367,14 @@ class Harness:
keyword = match and match[2]
if keyword in self.tweak.override.keys():
for attr, value in self.tweak.override[keyword].items():
# TODO?: If value is None: delete attr?
if value is None:
entry, n_subs = re.subn(f'( +)?{attr}=("[^"]*"|[^] ]*)(?(1)| *)', '', entry)
if n_subs < 1:
print(f'Harness.create_graph() warning: {attr} not found in {keyword}!')
elif n_subs > 1:
print(f'Harness.create_graph() warning: {attr} removed {n_subs} times in {keyword}!')
continue
if len(value) == 0 or ' ' in value:
value = value.replace('"', r'\"')
value = f'"{value}"'
@ -377,6 +384,7 @@ class Harness:
entry = re.sub(r'\]$', f' {attr}={value}]', entry)
elif n_subs > 1:
print(f'Harness.create_graph() warning: {attr} overridden {n_subs} times in {keyword}!')
dot.body[i] = entry
if self.tweak.append is not None: