From 61631384203fda2a3ef04407d88b53c1af589dde Mon Sep 17 00:00:00 2001 From: Alex Myers Date: Thu, 8 Dec 2022 13:10:34 -0600 Subject: [PATCH] reckless: avoid superfluous config rewrites --- tools/reckless | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tools/reckless b/tools/reckless index 425351aa3..21145f7f3 100755 --- a/tools/reckless +++ b/tools/reckless @@ -154,13 +154,17 @@ class Config(): parent_path) raise FileNotFoundError('invalid parent directory') - def editConfigFile(self, addline: str, removeline: str): + def editConfigFile(self, addline: Union[str, None], + removeline: Union[str, None]): + """Idempotent function to add and/or remove a single line each.""" remove_these_lines = [] with open(self.conf_fp, 'r') as reckless_conf: original = reckless_conf.readlines() empty_lines = [] + write_required = False for n, l in enumerate(original): - if l.strip() == removeline: + if removeline and l.strip() == removeline.strip(): + write_required = True remove_these_lines.append(n) continue if l.strip() == '': @@ -169,6 +173,13 @@ class Config(): # The white space is getting excessive. remove_these_lines.append(n) continue + if not addline: + return + # No write necessary if addline is already in config. + if not write_required: + for line in original: + if line.strip() == addline.strip(): + return with open(self.conf_fp, 'w') as conf_write: # no need to write if passed 'None' line_exists = not bool(addline)