Skip to content

Commit

Permalink
reckless: avoid superfluous config rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
endothermicdev authored and ShahanaFarooqui committed Apr 15, 2023
1 parent b59b6b9 commit 6163138
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tools/reckless
Original file line number Diff line number Diff line change
Expand Up @@ -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() == '':
Expand All @@ -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)
Expand Down

0 comments on commit 6163138

Please sign in to comment.