mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
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:
parent
5e527cb27d
commit
cef55459c6
@ -46,6 +46,21 @@ if not config_dir:
|
|||||||
config_file = config_dir / "config.json"
|
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():
|
def create_temp_config():
|
||||||
"""
|
"""
|
||||||
Creates a default instance for Red, so it can be ran
|
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_TYPE"] = "JSON"
|
||||||
default_dirs["STORAGE_DETAILS"] = {}
|
default_dirs["STORAGE_DETAILS"] = {}
|
||||||
|
|
||||||
with config_file.open("r", encoding="utf-8") as fs:
|
config = load_existing_config()
|
||||||
config = json.load(fs)
|
|
||||||
|
|
||||||
config[name] = default_dirs
|
config[name] = default_dirs
|
||||||
|
|
||||||
|
|||||||
@ -16,13 +16,13 @@ import pkg_resources
|
|||||||
from redbot import MIN_PYTHON_VERSION
|
from redbot import MIN_PYTHON_VERSION
|
||||||
from redbot.setup import (
|
from redbot.setup import (
|
||||||
basic_setup,
|
basic_setup,
|
||||||
load_existing_config,
|
|
||||||
remove_instance,
|
remove_instance,
|
||||||
remove_instance_interaction,
|
remove_instance_interaction,
|
||||||
create_backup,
|
create_backup,
|
||||||
)
|
)
|
||||||
from redbot.core import __version__, version_info as red_version_info, VersionInfo
|
from redbot.core import __version__, version_info as red_version_info, VersionInfo
|
||||||
from redbot.core.cli import confirm
|
from redbot.core.cli import confirm
|
||||||
|
from redbot.core.data_manager import load_existing_config
|
||||||
|
|
||||||
if sys.platform == "linux":
|
if sys.platform == "linux":
|
||||||
import distro # pylint: disable=import-error
|
import distro # pylint: disable=import-error
|
||||||
|
|||||||
@ -37,16 +37,7 @@ except PermissionError:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
config_file = config_dir / "config.json"
|
config_file = config_dir / "config.json"
|
||||||
|
|
||||||
|
instance_data = data_manager.load_existing_config()
|
||||||
def load_existing_config():
|
|
||||||
if not config_file.exists():
|
|
||||||
return {}
|
|
||||||
|
|
||||||
with config_file.open(encoding="utf-8") as fs:
|
|
||||||
return json.load(fs)
|
|
||||||
|
|
||||||
|
|
||||||
instance_data = load_existing_config()
|
|
||||||
if instance_data is None:
|
if instance_data is None:
|
||||||
instance_list = []
|
instance_list = []
|
||||||
else:
|
else:
|
||||||
@ -54,7 +45,7 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
def save_config(name, data, remove=False):
|
def save_config(name, data, remove=False):
|
||||||
_config = load_existing_config()
|
_config = data_manager.load_existing_config()
|
||||||
if remove and name in _config:
|
if remove and name in _config:
|
||||||
_config.pop(name)
|
_config.pop(name)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user