33
44import tomllib
55
6- from codegen .shared .configs .constants import CONFIG_PATH , SESSION_FILE
7- from codegen .shared .configs .models .global_session import GlobalSessionConfig
6+ from codegen .shared .configs .constants import CONFIG_PATH , GLOBAL_CONFIG_PATH , SESSION_FILE
7+ from codegen .shared .configs .models .global_config import GlobalConfig
88from codegen .shared .configs .models .session import SessionConfig
99
1010
11- def load_session_config (config_path : Path ) -> SessionConfig :
11+ def load_session_config (config_path : Path , base_config : SessionConfig | None = None ) -> SessionConfig :
1212 """Loads configuration from various sources."""
13- # Load from .env file
14- env_config = _load_from_env (config_path )
15-
16- # Load from .codegen/config.toml file
13+ base_config = base_config or global_config .global_session
1714 toml_config = _load_from_toml (config_path )
18-
19- # Merge configurations recursively
20- config_dict = _merge_configs (env_config .model_dump (), toml_config .model_dump ())
15+ config_dict = _merge_configs (base_config .model_dump (), toml_config )
2116 loaded_config = SessionConfig (** config_dict )
17+ loaded_config .file_path = str (config_path )
2218
2319 # Save the configuration to file if it doesn't exist
2420 if not config_path .exists ():
2521 loaded_config .save ()
2622 return loaded_config
2723
2824
25+ def _load_global_config () -> GlobalConfig :
26+ """Load configuration from the JSON file."""
27+ base_session = _load_from_env (GLOBAL_CONFIG_PATH )
28+ global_session = load_session_config (GLOBAL_CONFIG_PATH , base_config = base_session )
29+
30+ if SESSION_FILE .exists ():
31+ with open (SESSION_FILE ) as f :
32+ json_config = json .load (f )
33+ json_config ["global_session" ] = global_session .model_dump ()
34+ return GlobalConfig .model_validate (json_config , strict = False )
35+
36+ new_config = GlobalConfig (sessions = [], global_session = global_session )
37+ new_config .save ()
38+ return new_config
39+
40+
2941def _load_from_env (config_path : Path ) -> SessionConfig :
3042 """Load configuration from the environment variables."""
3143 return SessionConfig (file_path = str (config_path ))
3244
3345
34- def _load_from_toml (config_path : Path ) -> SessionConfig :
46+ def _load_from_toml (config_path : Path ) -> dict [ str , any ] :
3547 """Load configuration from the TOML file."""
3648 if config_path .exists ():
3749 with open (config_path , "rb" ) as f :
3850 toml_config = tomllib .load (f )
39- toml_config ["file_path" ] = str (config_path )
40- return SessionConfig .model_validate (toml_config , strict = False )
41-
42- return SessionConfig (file_path = str (config_path ))
51+ return toml_config
52+ return {}
4353
4454
4555def _merge_configs (base : dict , override : dict ) -> dict :
@@ -55,20 +65,8 @@ def _merge_configs(base: dict, override: dict) -> dict:
5565 return merged
5666
5767
58- def _load_global_config () -> GlobalSessionConfig :
59- """Load configuration from the JSON file."""
60- if SESSION_FILE .exists ():
61- with open (SESSION_FILE ) as f :
62- json_config = json .load (f )
63- return GlobalSessionConfig .model_validate (json_config , strict = False )
64-
65- new_config = GlobalSessionConfig (sessions = [])
66- new_config .save ()
67- return new_config
68-
69-
70- config = load_session_config (CONFIG_PATH )
7168global_config = _load_global_config ()
69+ config = load_session_config (CONFIG_PATH )
7270
7371
7472if __name__ == "__main__" :
0 commit comments