1313"""
1414
1515import threading
16+ from dataclasses import dataclass
1617from typing import Dict , Type , Optional , Any
1718
18-
1919# Dual thread-local storage: saved (for descendants/compiler) and live (for UI editing)
2020_saved_global_config_contexts : Dict [Type , threading .local ] = {}
2121_live_global_config_contexts : Dict [Type , threading .local ] = {}
@@ -33,6 +33,52 @@ def _validate_global_config_value(config_type: Type, config_instance: Any) -> No
3333 )
3434
3535
36+ def _assign_global_config_value (
37+ contexts : Dict [Type , threading .local ], config_type : Type , value : Any
38+ ) -> None :
39+ """One storage boundary for installing or unsetting a context projection."""
40+
41+ if value is None :
42+ local = contexts .get (config_type )
43+ if local is not None and hasattr (local , "value" ):
44+ del local .value
45+ return
46+ _validate_global_config_value (config_type , value )
47+ if config_type not in contexts :
48+ contexts [config_type ] = threading .local ()
49+ contexts [config_type ].value = value
50+
51+
52+ @dataclass (frozen = True )
53+ class GlobalContextValues :
54+ """Prepared saved/live projections, or exact local rollback context values."""
55+
56+ config_type : Type
57+ saved : Any
58+ live : Any
59+
60+ @classmethod
61+ def capture (cls , config_type : Type ) -> "GlobalContextValues" :
62+ return cls (
63+ config_type ,
64+ get_saved_global_config (config_type ),
65+ get_live_global_config (config_type ),
66+ )
67+
68+ def validate (self ) -> None :
69+ for value in (self .saved , self .live ):
70+ if value is not None :
71+ _validate_global_config_value (self .config_type , value )
72+
73+ def apply (self ) -> None :
74+ _assign_global_config_value (
75+ _saved_global_config_contexts , self .config_type , self .saved
76+ )
77+ _assign_global_config_value (
78+ _live_global_config_contexts , self .config_type , self .live
79+ )
80+
81+
3682def set_saved_global_config (config_type : Type , config_instance : Any ) -> None :
3783 """Set SAVED global config (what descendants/compiler see).
3884
@@ -46,9 +92,9 @@ def set_saved_global_config(config_type: Type, config_instance: Any) -> None:
4692 config_instance: The SAVED config instance
4793 """
4894 _validate_global_config_value (config_type , config_instance )
49- if config_type not in _saved_global_config_contexts :
50- _saved_global_config_contexts [ config_type ] = threading . local ()
51- _saved_global_config_contexts [ config_type ]. value = config_instance
95+ _assign_global_config_value (
96+ _saved_global_config_contexts , config_type , config_instance
97+ )
5298
5399
54100def set_live_global_config (config_type : Type , config_instance : Any ) -> None :
@@ -63,9 +109,9 @@ def set_live_global_config(config_type: Type, config_instance: Any) -> None:
63109 config_instance: The LIVE (unsaved) config instance
64110 """
65111 _validate_global_config_value (config_type , config_instance )
66- if config_type not in _live_global_config_contexts :
67- _live_global_config_contexts [ config_type ] = threading . local ()
68- _live_global_config_contexts [ config_type ]. value = config_instance
112+ _assign_global_config_value (
113+ _live_global_config_contexts , config_type , config_instance
114+ )
69115
70116
71117def get_saved_global_config (config_type : Type ) -> Optional [Any ]:
@@ -78,7 +124,7 @@ def get_saved_global_config(config_type: Type) -> Optional[Any]:
78124 Saved config instance or None
79125 """
80126 context = _saved_global_config_contexts .get (config_type )
81- return getattr (context , ' value' , None ) if context else None
127+ return getattr (context , " value" , None ) if context else None
82128
83129
84130def get_live_global_config (config_type : Type ) -> Optional [Any ]:
@@ -91,7 +137,7 @@ def get_live_global_config(config_type: Type) -> Optional[Any]:
91137 Live config instance or None
92138 """
93139 context = _live_global_config_contexts .get (config_type )
94- return getattr (context , ' value' , None ) if context else None
140+ return getattr (context , " value" , None ) if context else None
95141
96142
97143def set_global_config_for_editing (config_type : Type , config_instance : Any ) -> None :
@@ -109,7 +155,9 @@ def set_global_config_for_editing(config_type: Type, config_instance: Any) -> No
109155 set_live_global_config (config_type , config_instance )
110156
111157
112- def get_current_global_config (config_type : Type , use_live : bool = True ) -> Optional [Any ]:
158+ def get_current_global_config (
159+ config_type : Type , use_live : bool = True
160+ ) -> Optional [Any ]:
113161 """Get current global config.
114162
115163 Args:
0 commit comments