fixed an error when loading the config file.
This commit is contained in:
@@ -1,28 +1,45 @@
|
|||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import toml
|
import toml
|
||||||
|
import shutil
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
|
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
|
||||||
config_file = f"{root_dir}/config.toml"
|
config_file = f"{root_dir}/config.toml"
|
||||||
if not os.path.isfile(config_file):
|
|
||||||
example_file = f"{root_dir}/config.example.toml"
|
|
||||||
if os.path.isfile(example_file):
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
shutil.copyfile(example_file, config_file)
|
|
||||||
logger.info(f"copy config.example.toml to config.toml")
|
|
||||||
|
|
||||||
logger.info(f"load config from file: {config_file}")
|
def load_config():
|
||||||
|
# fix: IsADirectoryError: [Errno 21] Is a directory: '/MoneyPrinterTurbo/config.toml'
|
||||||
|
if os.path.isdir(config_file):
|
||||||
|
shutil.rmtree(config_file)
|
||||||
|
|
||||||
try:
|
if not os.path.isfile(config_file):
|
||||||
_cfg = toml.load(config_file)
|
example_file = f"{root_dir}/config.example.toml"
|
||||||
except Exception as e:
|
if os.path.isfile(example_file):
|
||||||
logger.warning(f"load config failed: {str(e)}, try to load as utf-8-sig")
|
shutil.copyfile(example_file, config_file)
|
||||||
with open(config_file, mode="r", encoding='utf-8-sig') as fp:
|
logger.info(f"copy config.example.toml to config.toml")
|
||||||
_cfg_content = fp.read()
|
|
||||||
_cfg = toml.loads(_cfg_content)
|
|
||||||
|
|
||||||
|
logger.info(f"load config from file: {config_file}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
_config_ = toml.load(config_file)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"load config failed: {str(e)}, try to load as utf-8-sig")
|
||||||
|
with open(config_file, mode="r", encoding='utf-8-sig') as fp:
|
||||||
|
_cfg_content = fp.read()
|
||||||
|
_config_ = toml.loads(_cfg_content)
|
||||||
|
return _config_
|
||||||
|
|
||||||
|
|
||||||
|
def save_config():
|
||||||
|
with open(config_file, "w", encoding="utf-8") as f:
|
||||||
|
_cfg["app"] = app
|
||||||
|
_cfg["whisper"] = whisper
|
||||||
|
_cfg["pexels"] = pexels
|
||||||
|
f.write(toml.dumps(_cfg))
|
||||||
|
|
||||||
|
|
||||||
|
_cfg = load_config()
|
||||||
app = _cfg.get("app", {})
|
app = _cfg.get("app", {})
|
||||||
whisper = _cfg.get("whisper", {})
|
whisper = _cfg.get("whisper", {})
|
||||||
pexels = _cfg.get("pexels", {})
|
pexels = _cfg.get("pexels", {})
|
||||||
@@ -46,19 +63,3 @@ if imagemagick_path and os.path.isfile(imagemagick_path):
|
|||||||
ffmpeg_path = app.get("ffmpeg_path", "")
|
ffmpeg_path = app.get("ffmpeg_path", "")
|
||||||
if ffmpeg_path and os.path.isfile(ffmpeg_path):
|
if ffmpeg_path and os.path.isfile(ffmpeg_path):
|
||||||
os.environ["IMAGEIO_FFMPEG_EXE"] = ffmpeg_path
|
os.environ["IMAGEIO_FFMPEG_EXE"] = ffmpeg_path
|
||||||
|
|
||||||
|
|
||||||
# __cfg = {
|
|
||||||
# "hostname": hostname,
|
|
||||||
# "listen_host": listen_host,
|
|
||||||
# "listen_port": listen_port,
|
|
||||||
# }
|
|
||||||
# logger.info(__cfg)
|
|
||||||
|
|
||||||
|
|
||||||
def save_config():
|
|
||||||
with open(config_file, "w", encoding="utf-8") as f:
|
|
||||||
_cfg["app"] = app
|
|
||||||
_cfg["whisper"] = whisper
|
|
||||||
_cfg["pexels"] = pexels
|
|
||||||
f.write(toml.dumps(_cfg))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user