mirror of
https://github.com/Benexl/FastAnime.git
synced 2025-12-12 15:50:01 -08:00
feat: make config parseing more efficient
This commit is contained in:
@@ -72,21 +72,14 @@ def cli(ctx: click.Context, **options: "Unpack[Options]"):
|
||||
options["rich_traceback_theme"],
|
||||
)
|
||||
|
||||
loader = ConfigLoader(config_path=USER_CONFIG_PATH)
|
||||
config = AppConfig.model_validate({}) if options["no_config"] else loader.load()
|
||||
cli_overrides = {}
|
||||
param_lookup = {p.name: p for p in ctx.command.params}
|
||||
|
||||
# update app config with command line parameters
|
||||
for param_name, param_value in ctx.params.items():
|
||||
source = ctx.get_parameter_source(param_name)
|
||||
if (
|
||||
source == ParameterSource.ENVIRONMENT
|
||||
or source == ParameterSource.COMMANDLINE
|
||||
):
|
||||
parameter = None
|
||||
for param in ctx.command.params:
|
||||
if param.name == param_name:
|
||||
parameter = param
|
||||
break
|
||||
if source in (ParameterSource.ENVIRONMENT, ParameterSource.COMMANDLINE):
|
||||
parameter = param_lookup.get(param_name)
|
||||
|
||||
if (
|
||||
parameter
|
||||
and hasattr(parameter, "model_name")
|
||||
@@ -94,8 +87,15 @@ def cli(ctx: click.Context, **options: "Unpack[Options]"):
|
||||
):
|
||||
model_name = getattr(parameter, "model_name")
|
||||
field_name = getattr(parameter, "field_name")
|
||||
if hasattr(config, model_name):
|
||||
model_instance = getattr(config, model_name)
|
||||
if hasattr(model_instance, field_name):
|
||||
setattr(model_instance, field_name, param_value)
|
||||
|
||||
if model_name not in cli_overrides:
|
||||
cli_overrides[model_name] = {}
|
||||
cli_overrides[model_name][field_name] = param_value
|
||||
|
||||
loader = ConfigLoader(config_path=USER_CONFIG_PATH)
|
||||
config = (
|
||||
AppConfig.model_validate(cli_overrides)
|
||||
if options["no_config"]
|
||||
else loader.load(cli_overrides)
|
||||
)
|
||||
ctx.obj = config
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import textwrap
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
|
||||
from ...core.config import AppConfig
|
||||
@@ -71,6 +72,8 @@ def generate_config_ini_from_app_model(app_model: AppConfig) -> str:
|
||||
value_str = str(field_value)
|
||||
elif field_value is None:
|
||||
value_str = ""
|
||||
elif isinstance(field_value, Enum):
|
||||
value_str = field_value.value
|
||||
else:
|
||||
value_str = str(field_value)
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import configparser
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
import click
|
||||
from pydantic import ValidationError
|
||||
@@ -73,7 +75,7 @@ class ConfigLoader:
|
||||
|
||||
return app_config
|
||||
|
||||
def load(self) -> AppConfig:
|
||||
def load(self, update: Dict = {}) -> AppConfig:
|
||||
"""
|
||||
Loads the configuration and returns a populated, validated AppConfig object.
|
||||
|
||||
@@ -99,6 +101,10 @@ class ConfigLoader:
|
||||
section: dict(self.parser.items(section))
|
||||
for section in self.parser.sections()
|
||||
}
|
||||
if update:
|
||||
for key in config_dict:
|
||||
if key in update:
|
||||
config_dict[key].update(update[key])
|
||||
try:
|
||||
app_config = AppConfig.model_validate(config_dict)
|
||||
return app_config
|
||||
|
||||
Reference in New Issue
Block a user