Replace CONFIG_PARSER global with @lru_cache (#1147)

* remove global config_parser

* fix import order

* remove lambda

* remove unused block
This commit is contained in:
Harisreedhar
2026-06-09 12:16:42 +05:30
committed by GitHub
parent 7d1df3feac
commit 87f561b1e5
2 changed files with 16 additions and 26 deletions
+12 -21
View File
@@ -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()))
+4 -5
View File
@@ -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':
{