Files
Zordon/src/utilities/config.py
Brett 74dce5cc3d Windows path fixes (#129)
* Change uses of os.path to use Pathlib

* Add return types and type hints

* Add more docstrings

* Add missing import to api_server
2026-01-18 00:18:43 -06:00

76 lines
3.1 KiB
Python

import os
from pathlib import Path
import yaml
from src.utilities.misc_helper import current_system_os, copy_directory_contents
class Config:
# Initialize class variables with default values
upload_folder = "~/zordon-uploads/"
update_engines_on_launch = True
max_content_path = 100000000
server_log_level = 'debug'
log_buffer_length = 250
worker_process_timeout = 120
flask_log_level = 'error'
flask_debug_enable = False
queue_eval_seconds = 1
port_number = 8080
enable_split_jobs = True
download_timeout_seconds = 120
@classmethod
def load_config(cls, config_path):
with open(config_path, 'r') as ymlfile:
cfg = yaml.safe_load(ymlfile)
cls.upload_folder = str(Path(cfg.get('upload_folder', cls.upload_folder)).expanduser())
cls.update_engines_on_launch = cfg.get('update_engines_on_launch', cls.update_engines_on_launch)
cls.max_content_path = cfg.get('max_content_path', cls.max_content_path)
cls.server_log_level = cfg.get('server_log_level', cls.server_log_level)
cls.log_buffer_length = cfg.get('log_buffer_length', cls.log_buffer_length)
cls.worker_process_timeout = cfg.get('worker_process_timeout', cls.worker_process_timeout)
cls.flask_log_level = cfg.get('flask_log_level', cls.flask_log_level)
cls.flask_debug_enable = cfg.get('flask_debug_enable', cls.flask_debug_enable)
cls.queue_eval_seconds = cfg.get('queue_eval_seconds', cls.queue_eval_seconds)
cls.port_number = cfg.get('port_number', cls.port_number)
cls.enable_split_jobs = cfg.get('enable_split_jobs', cls.enable_split_jobs)
cls.download_timeout_seconds = cfg.get('download_timeout_seconds', cls.download_timeout_seconds)
@classmethod
def config_dir(cls) -> Path:
# Set up the config path
if current_system_os() == 'macos':
local_config_path = Path('~/Library/Application Support/Zordon').expanduser()
elif current_system_os() == 'windows':
local_config_path = Path(os.environ['APPDATA']) / 'Zordon'
else:
local_config_path = Path('~/.config/Zordon').expanduser()
return local_config_path
@classmethod
def setup_config_dir(cls):
# Set up the config path
local_config_dir = cls.config_dir()
if os.path.exists(local_config_dir):
return
try:
# Create the local configuration directory
os.makedirs(local_config_dir)
# Determine the template path
resource_environment_path = os.environ.get('RESOURCEPATH')
if resource_environment_path:
template_path = Path(resource_environment_path) / 'config'
else:
template_path = Path(__file__).resolve().parents[2] / 'config'
# Copy contents from the template to the local configuration directory
copy_directory_contents(template_path, local_config_dir)
except Exception as e:
print(f"An error occurred while setting up the config directory: {e}")
raise