From ba8f8423be8e61f4b7c646e4cf4b1c8ca40741e6 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 12 Sep 2021 23:14:09 +0200 Subject: [PATCH] Append non-existing tweak.override attributes --- docs/syntax.md | 3 ++- src/wireviz/Harness.py | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/syntax.md b/docs/syntax.md index 005883a..ce532b0 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -347,7 +347,8 @@ Alternatively items can be added to just the BOM by putting them in the section : # leading string of .gv entry : # attribute and its new value # Any number of attributes can be overridden - # for each entry + # for each entry. Attributes not already existing + # in the entry will be appended to the entry. append: # string or list of strings to append to the .gv output ``` diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 548a687..8e5a7f3 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -367,11 +367,16 @@ 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 len(value) == 0 or ' ' in value: value = value.replace('"', r'\"') value = f'"{value}"' - # TODO?: If value is None: delete attr, and if attr not found: append it? - entry = re.sub(f'{attr}=("[^"]*"|[^] ]*)', f'{attr}={value}', entry) + entry, n_subs = re.subn(f'{attr}=("[^"]*"|[^] ]*)', f'{attr}={value}', entry) + if n_subs < 1: + # If attr not found, then append it + 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: