feat(config): split out parse_args and read_config logic from get_config

Having this all in the `get_config` function makes testing hard. Move these two functions to their own methods, and call them on app startup explicitly.
This commit is contained in:
psychedelicious 2024-03-12 00:02:53 +11:00
parent 77b86e9ad5
commit b9884a6166
2 changed files with 16 additions and 19 deletions

View File

@ -4,6 +4,8 @@
from invokeai.app.services.config.config_default import get_config
app_config = get_config()
app_config.parse_args()
app_config.read_config()
if True: # hack to make flake8 happy with imports coming after setting up the config
import asyncio

View File

@ -211,8 +211,20 @@ class InvokeAIAppConfig(BaseSettings):
def set_root(self, root: Path) -> None:
"""Set the runtime root directory. This is typically set using a CLI arg."""
assert isinstance(root, Path)
self._root = root
def parse_args(self) -> None:
"""Parse the CLI args and set the runtime root directory."""
opt = app_arg_parser.parse_args()
if root := getattr(opt, "root", None):
self.set_root(Path(root))
def read_config(self) -> None:
"""Read the config from the `invokeai.yaml` file, merging it into the singleton config."""
config_from_file = load_and_migrate_config(self.init_file_path)
self.update_config(config_from_file)
def _resolve(self, partial_path: Path) -> Path:
return (self.root_path / partial_path).resolve()
@ -385,22 +397,5 @@ def load_and_migrate_config(config_path: Path) -> InvokeAIAppConfig:
@lru_cache(maxsize=1)
def get_config() -> InvokeAIAppConfig:
"""Return the global singleton app config.
On first call, CLI args are parsed and the config file is read and merged in to the singleton config.
On subsequent calls, the singleton config is returned.
"""
# Singleton app config
config = InvokeAIAppConfig()
# User may have specified a `root` as a CLI arg
opt = app_arg_parser.parse_args()
if root := getattr(opt, "root", None):
config.set_root(Path(root))
else:
config.set_root(config.find_root())
# Load the config file and merge it in to the app config
config_from_file = load_and_migrate_config(config.init_file_path)
config.update_config(config_from_file)
return config
"""Return the global singleton app config"""
return InvokeAIAppConfig()