Coverage for openhcs/config_framework/global_config.py: 100.0%
17 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-01 18:33 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-10-01 18:33 +0000
1"""
2Generic global configuration context management.
4Provides thread-local storage for global configuration state.
5This is used as the base context for all lazy configuration resolution.
6"""
8import threading
9from typing import Dict, Type, Optional, Any
12# Simplified thread-local storage for non-UI usage
13_global_config_contexts: Dict[Type, threading.local] = {}
16def set_current_global_config(config_type: Type, config_instance: Any, *, caller_context: str = None) -> None:
17 """Set current global config for any dataclass type.
19 RESTRICTED USE: This should ONLY be called when actually editing the global config,
20 not for temporary context switching or orchestrator-specific operations.
22 Args:
23 config_type: The config type to set
24 config_instance: The config instance to set
25 caller_context: Optional context description for debugging inappropriate usage
26 """
27 import inspect
29 # Get caller information for debugging inappropriate usage
30 frame = inspect.currentframe().f_back
31 caller_file = frame.f_code.co_filename
32 caller_function = frame.f_code.co_name
33 caller_line = frame.f_lineno
35 # Set thread-local context
36 if config_type not in _global_config_contexts:
37 _global_config_contexts[config_type] = threading.local()
38 _global_config_contexts[config_type].value = config_instance
41def set_global_config_for_editing(config_type: Type, config_instance: Any) -> None:
42 """Set global config specifically for editing scenarios.
44 This is the ONLY function that should be used for legitimate global config modifications.
45 Use this when:
46 - User is editing global configuration through UI
47 - Application startup is loading cached global config
48 - Tests are setting up global config state
49 """
50 set_current_global_config(config_type, config_instance, caller_context="LEGITIMATE_GLOBAL_CONFIG_EDITING")
53def get_current_global_config(config_type: Type) -> Optional[Any]:
54 """Get current global config for any dataclass type.
56 Args:
57 config_type: The config type to retrieve
59 Returns:
60 Current config instance or None
61 """
62 context = _global_config_contexts.get(config_type)
63 return getattr(context, 'value', None) if context else None