From 87f561b1e567d03440c91dc8908508ccd08c7162 Mon Sep 17 00:00:00 2001 From: Harisreedhar <46858047+harisreedhar@users.noreply.github.com> Date: Tue, 9 Jun 2026 12:16:42 +0530 Subject: [PATCH] Replace CONFIG_PARSER global with @lru_cache (#1147) * remove global config_parser * fix import order * remove lambda * remove unused block --- facefusion/config.py | 33 ++++++++++++--------------------- tests/test_config.py | 9 ++++----- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/facefusion/config.py b/facefusion/config.py index e8e307ce..63aab509 100644 --- a/facefusion/config.py +++ b/facefusion/config.py @@ -1,29 +1,20 @@ from configparser import ConfigParser +from functools import lru_cache from typing import List, Optional from facefusion import state_manager from facefusion.common_helper import cast_bool, cast_float, cast_int -CONFIG_PARSER = None - -def get_config_parser() -> ConfigParser: - global CONFIG_PARSER - - if CONFIG_PARSER is None: - CONFIG_PARSER = ConfigParser() - CONFIG_PARSER.read(state_manager.get_item('config_path'), encoding = 'utf-8') - return CONFIG_PARSER - - -def clear_config_parser() -> None: - global CONFIG_PARSER - - CONFIG_PARSER = None +@lru_cache +def get_static_config_parser() -> ConfigParser: + config_parser = ConfigParser() + config_parser.read(state_manager.get_item('config_path'), encoding = 'utf-8') + return config_parser def get_str_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[str]: - config_parser = get_config_parser() + config_parser = get_static_config_parser() if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): return config_parser.get(section, option) @@ -31,7 +22,7 @@ def get_str_value(section : str, option : str, fallback : Optional[str] = None) def get_int_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[int]: - config_parser = get_config_parser() + config_parser = get_static_config_parser() if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): return config_parser.getint(section, option) @@ -39,7 +30,7 @@ def get_int_value(section : str, option : str, fallback : Optional[str] = None) def get_float_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[float]: - config_parser = get_config_parser() + config_parser = get_static_config_parser() if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): return config_parser.getfloat(section, option) @@ -47,7 +38,7 @@ def get_float_value(section : str, option : str, fallback : Optional[str] = None def get_bool_value(section : str, option : str, fallback : Optional[str] = None) -> Optional[bool]: - config_parser = get_config_parser() + config_parser = get_static_config_parser() if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): return config_parser.getboolean(section, option) @@ -55,7 +46,7 @@ def get_bool_value(section : str, option : str, fallback : Optional[str] = None) def get_str_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[str]]: - config_parser = get_config_parser() + config_parser = get_static_config_parser() if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): return config_parser.get(section, option).split() @@ -65,7 +56,7 @@ def get_str_list(section : str, option : str, fallback : Optional[str] = None) - def get_int_list(section : str, option : str, fallback : Optional[str] = None) -> Optional[List[int]]: - config_parser = get_config_parser() + config_parser = get_static_config_parser() if config_parser.has_option(section, option) and config_parser.get(section, option).strip(): return list(map(int, config_parser.get(section, option).split())) diff --git a/tests/test_config.py b/tests/test_config.py index cae75778..ba821508 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,14 +1,13 @@ -from configparser import ConfigParser - import pytest -from facefusion import config +from facefusion import config, state_manager @pytest.fixture(scope = 'module', autouse = True) def before_all() -> None: - config.CONFIG_PARSER = ConfigParser() - config.CONFIG_PARSER.read_dict( + state_manager.init_item('config_path', 'facefusion.ini') + config_parser = config.get_static_config_parser() + config_parser.read_dict( { 'str': {