Pass through json file writing to core for error handling

This commit is contained in:
SimplifiedPrivacy 2026-10-20 13:54:12 -04:00
parent 817b797701
commit 39925ad546
2 changed files with 14 additions and 19 deletions

View file

@ -1,9 +1,9 @@
import json
import logging
from core.utils.basic_operations.write_or_read_from_json import read_entire_json, write_json_to_file
import os
def setup_gui_directory(gui_cache_home, gui_config_home, gui_config_file):
def setup_gui_directory(gui_cache_home, gui_config_home, gui_config_file) -> bool:
os.makedirs(gui_cache_home, exist_ok=True)
os.makedirs(gui_config_home, exist_ok=True)
@ -14,24 +14,17 @@ def setup_gui_directory(gui_cache_home, gui_config_home, gui_config_file):
"log_level": "INFO"
}
}
with open(gui_config_file, 'w') as f:
json.dump(default_config, f, indent=4)
return write_json_to_file(data=default_config, filepath=gui_config_file)
return True
def load_gui_config(gui_config_file) -> str|None:
"""Pass through to core for better error handling, such as UnicodeDecodeError."""
return read_entire_json(gui_config_file)
def load_gui_config(gui_config_file):
try:
with open(gui_config_file, 'r') as f:
return json.load(f)
except FileNotFoundError:
return None
except json.JSONDecodeError:
logging.error("Invalid GUI config file format")
return None
def save_gui_config(gui_config_file, config):
with open(gui_config_file, 'w') as f:
json.dump(config, f, indent=4)
def save_gui_config(gui_config_file, config) -> bool:
"""Pass through to core for better error handling"""
return write_json_to_file(data=config, filepath=gui_config_file)
def default_gui_config():

View file

@ -102,8 +102,10 @@ def extract_endpoint_ip(profile):
wg_conf_path = f'{profile_path}/wg.conf.bak'
if not os.path.exists(wg_conf_path):
return None
with open(wg_conf_path, 'r') as f:
content = f.read()
for line in content.split('\n'):
if line.strip().startswith('Endpoint = '):
endpoint = line.strip().split(' = ')[1]