Make the fact that the global configuration is a mutable global dictionary a bit more explicit

Instead of implicit through the use of lru_cache
This commit is contained in:
Grant Sanderson 2024-12-10 15:31:43 -06:00
parent d21fbd02bc
commit 24697377db

View file

@ -9,8 +9,6 @@ import sys
import yaml import yaml
from ast import literal_eval from ast import literal_eval
from functools import lru_cache
from manimlib.logger import log from manimlib.logger import log
from manimlib.utils.dict_ops import merge_dicts_recursively from manimlib.utils.dict_ops import merge_dicts_recursively
from manimlib.utils.init_config import init_customization from manimlib.utils.init_config import init_customization
@ -21,8 +19,7 @@ if TYPE_CHECKING:
from typing import Optional from typing import Optional
@lru_cache def initialize_global_config():
def get_global_config():
""" """
Return default configuration for various classes in manim, such as Return default configuration for various classes in manim, such as
Scene, Window, Camera, and SceneFileWriter, as well as configuration Scene, Window, Camera, and SceneFileWriter, as well as configuration
@ -31,9 +28,6 @@ def get_global_config():
The result is initially on the contents of default_config.yml in the manimlib directory, The result is initially on the contents of default_config.yml in the manimlib directory,
which can be further updated by a custom configuration file custom_config.yml. which can be further updated by a custom configuration file custom_config.yml.
It is further updated based on command line argument. It is further updated based on command line argument.
After the first time this function is called, it's result is cached
via lru cache, and subsequent calls return this result
""" """
args = parse_cli() args = parse_cli()
global_defaults_file = os.path.join(get_manim_dir(), "manimlib", "default_config.yml") global_defaults_file = os.path.join(get_manim_dir(), "manimlib", "default_config.yml")
@ -53,9 +47,6 @@ def get_global_config():
return config return config
# Functions used to initialized the configuration
def parse_cli(): def parse_cli():
try: try:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -322,36 +313,6 @@ def update_run_config(config: dict, args: Namespace):
) )
# Shortcuts for retrieving portions of global configuration
def get_window_config() -> dict:
""" Key word arguments for Window """
return get_global_config()["window"]
def get_camera_config() -> dict:
""" Key word arguments for Camera """
return get_global_config()["camera"]
def get_file_writer_config() -> dict:
""" Key word arguments for SceneFileWriter """
return get_global_config()["file_writer"]
def get_scene_config() -> dict:
""" Key word arguments for Scene """
return get_global_config()["scene"]
def get_run_config():
return get_global_config()["run"]
# Helpers for the functions above
def load_yaml(file_path: str): def load_yaml(file_path: str):
try: try:
with open(file_path, "r") as file: with open(file_path, "r") as file:
@ -411,3 +372,44 @@ def get_output_directory(args: Namespace, global_config: dict) -> str:
ext = ext[1:] ext = ext[1:]
output_directory = os.path.join(output_directory, ext) output_directory = os.path.join(output_directory, ext)
return output_directory return output_directory
# Create global configuration
GLOBAL_CONFIG = initialize_global_config()
def get_global_config():
global GLOBAL_CONFIG
return GLOBAL_CONFIG
# Shortcuts for retrieving portions of global configuration
def get_window_config() -> dict:
""" Key word arguments for Window """
return get_global_config()["window"]
def get_camera_config() -> dict:
""" Key word arguments for Camera """
return get_global_config()["camera"]
def get_file_writer_config() -> dict:
""" Key word arguments for SceneFileWriter """
return get_global_config()["file_writer"]
def get_scene_config() -> dict:
""" Key word arguments for Scene """
return get_global_config()["scene"]
def get_run_config():
return get_global_config()["run"]
# Helpers for the functions above