Skip to content

Commit 4a84a71

Browse files
committed
Expand config file searching.
1 parent 7554894 commit 4a84a71

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

tools/yaml2x/yaml2sheet/config_loader.py

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tools/yaml2x/yaml2sheet/yaml2sheet.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def parse_args() -> argparse.Namespace:
2424
parser.add_argument(
2525
'-c', '--config',
2626
type=str,
27-
default='config.yaml',
2827
help='Path to configuration file (supported formats: yaml, toml, ini)'
2928
)
3029

0 commit comments

Comments
 (0)