@@ -188,7 +188,58 @@ def get_config_loader(config_path: str) -> ConfigLoader:
188188
189189 return loaders [ext ]
190190
191- def load_configuration (config_path : str = 'config.yaml' ) -> ApplicationConfig :
191+ def find_config_file (config_path : Optional [str ] = None ) -> str :
192+ """設定ファイルを探索する
193+
194+ Args:
195+ config_path: コマンドラインで指定された設定ファイルパス
196+
197+ Returns:
198+ str: 見つかった設定ファイルの絶対パス
199+
200+ Raises:
201+ FileNotFoundError: 設定ファイルが見つからない場合
202+ """
203+ # -c オプションで指定された場合
204+ if config_path :
205+ if os .path .isabs (config_path ):
206+ if not os .path .exists (config_path ):
207+ raise FileNotFoundError (f"Specified configuration file not found: { config_path } " )
208+ return config_path
209+ else :
210+ # 相対パスの場合はカレントディレクトリからの相対で探す
211+ abs_path = os .path .join (os .getcwd (), config_path )
212+ if not os .path .exists (abs_path ):
213+ raise FileNotFoundError (f"Specified configuration file not found: { config_path } " )
214+ return abs_path
215+
216+ # デフォルトの探索順
217+ search_paths = []
218+
219+ # カレントディレクトリを探索
220+ search_paths .extend ([
221+ os .path .join (os .getcwd (), f"config.{ ext } " )
222+ for ext in ["yaml" , "toml" , "ini" ]
223+ ])
224+
225+ # スクリプトのあるディレクトリを探索
226+ script_dir = os .path .dirname (os .path .abspath (__file__ ))
227+ search_paths .extend ([
228+ os .path .join (script_dir , f"config.{ ext } " )
229+ for ext in ["yaml" , "toml" , "ini" ]
230+ ])
231+
232+ # 最初に見つかったファイルを返す
233+ for path in search_paths :
234+ if os .path .exists (path ):
235+ return path
236+
237+ raise FileNotFoundError (
238+ "No configuration file found. Searched in:\n " +
239+ "\n " .join (f"- { p } " for p in search_paths )
240+ )
241+
242+ def load_configuration (config_path : Optional [str ] = None ) -> ApplicationConfig :
192243 """Load application configuration from file
193244
194245 Args:
@@ -201,15 +252,13 @@ def load_configuration(config_path: str = 'config.yaml') -> ApplicationConfig:
201252 FileNotFoundError: If config file is not found
202253 ValueError: If format is not supported or required settings are missing
203254 """
204- if not os .path .exists (config_path ):
205- logger .warning (f"Configuration file not found: { config_path } " )
206- return ApplicationConfig .from_dict ({})
207-
208255 try :
209- loader = get_config_loader (config_path )
210- config_data = loader .load (config_path )
211- logger .info (f"Loaded configuration from { config_path } " )
256+ actual_config_path = find_config_file (config_path )
257+ logger .info (f"Using configuration file: { actual_config_path } " )
258+
259+ loader = get_config_loader (actual_config_path )
260+ config_data = loader .load (actual_config_path )
212261 return ApplicationConfig .from_dict (config_data )
213262 except Exception as e :
214263 logger .error (f"Error loading configuration: { e } " )
215- raise
264+ raise
0 commit comments