Simplify helpers setting methods

This commit is contained in:
luukas 2022-05-25 16:21:34 +03:00
parent 0eac0721ea
commit 8e13b4e11d
No known key found for this signature in database
GPG Key ID: CC4915E8D71FC044

View File

@ -194,7 +194,6 @@ class Helpers:
return cmd_out
def get_setting(self, key, default_return=False):
try:
with open(self.settings_file, "r", encoding="utf-8") as f:
data = json.load(f)
@ -202,10 +201,8 @@ class Helpers:
if key in data.keys():
return data.get(key)
else:
logger.error(f"Config File Error: setting {key} does not exist")
Console.error(f"Config File Error: setting {key} does not exist")
return default_return
logger.error(f'Config File Error: Setting "{key}" does not exist')
Console.error(f'Config File Error: Setting "{key}" does not exist')
except Exception as e:
logger.critical(
@ -217,22 +214,19 @@ class Helpers:
return default_return
def set_setting(self, key, new_value, default_return=False):
def set_setting(self, key, new_value):
try:
with open(self.settings_file, "r", encoding="utf-8") as f:
data = json.load(f)
if key in data.keys():
data[key] = new_value
with open(self.settings_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
return True
else:
logger.error(f"Config File Error: setting {key} does not exist")
Console.error(f"Config File Error: setting {key} does not exist")
return default_return
with open(self.settings_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
logger.error(f'Config File Error: Setting "{key}" does not exist')
Console.error(f'Config File Error: Setting "{key}" does not exist')
except Exception as e:
logger.critical(
@ -241,6 +235,7 @@ class Helpers:
Console.critical(
f"Config File Error: Unable to read {self.settings_file} due to {e}"
)
return False
@staticmethod
def get_local_ip():