Skip to content

Commit

Permalink
Delete tweak.override attributes with null value
Browse files Browse the repository at this point in the history
  • Loading branch information
kvid committed Sep 12, 2021
1 parent d094146 commit c73213b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
12 changes: 10 additions & 2 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def typecheck(name: str, value: Any, expect: type) -> None:
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):
Expand All @@ -367,7 +367,14 @@ def typecheck(name: str, value: Any, expect: type) -> None:
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}"'
Expand All @@ -377,6 +384,7 @@ def typecheck(name: str, value: Any, expect: type) -> None:
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:
Expand Down

0 comments on commit c73213b

Please sign in to comment.