From 4945d262a021fc9eaa10016351dd4eca970ebea8 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 12 Sep 2021 23:19:44 +0200 Subject: [PATCH] Delete tweak.override attributes with null value --- docs/syntax.md | 3 ++- src/wireviz/DataClasses.py | 2 +- src/wireviz/Harness.py | 12 ++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/syntax.md b/docs/syntax.md index ce532b0..fa433c3 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -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. : # leading string of .gv entry - : # attribute and its new value + : # 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: # string or list of strings to append to the .gv output ``` diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index c25b248..95325d1 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -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 diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 8e5a7f3..f4937f7 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -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: