Fix issues with loading config.json when it doesn't exist (#5416)

* catch and handle FileNotFoundError when using --no-instance when config.json does not already exist

* move load_existing_config to data_manager.py

* use load_existing_config in create_temp_config

* Fix import in redbot-launcher

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Vexed
2021-12-25 02:02:44 +00:00
committed by GitHub
parent 5e527cb27d
commit cef55459c6
3 changed files with 19 additions and 14 deletions

View File

@@ -46,6 +46,21 @@ if not config_dir:
config_file = config_dir / "config.json"
def load_existing_config():
"""Get the contents of the config file, or an empty dictionary if it does not exist.
Returns
-------
dict
The config data.
"""
if not config_file.exists():
return {}
with config_file.open(encoding="utf-8") as fs:
return json.load(fs)
def create_temp_config():
"""
Creates a default instance for Red, so it can be ran
@@ -61,8 +76,7 @@ def create_temp_config():
default_dirs["STORAGE_TYPE"] = "JSON"
default_dirs["STORAGE_DETAILS"] = {}
with config_file.open("r", encoding="utf-8") as fs:
config = json.load(fs)
config = load_existing_config()
config[name] = default_dirs