ktsn-ud commited on
Commit
c5e9ffd
·
1 Parent(s): b1ec9f2

パス取得関数の仕様を変更し、階層構造に対応

Browse files
scripts/1_build_dict.py CHANGED
@@ -24,15 +24,17 @@ def main():
24
  log.info("SudachiPyのユーザー辞書をビルドします。")
25
 
26
  # 入力用のユーザー辞書CSVファイルのパス
27
- user_dict_path = get_file_path_from_config("user_dict", "resources/user_dict.csv")
 
 
28
  # 出力のユーザー辞書のパス
29
  output_path = get_file_path_from_config(
30
- "user_dict_generated", "data/generated/user.dic"
31
  )
32
 
33
  # sudachi.jsonから辞書の種類を取得
34
  sudachi_config_path = get_file_path_from_config(
35
- "sudachi_config", "scripts/sudachi.json"
36
  )
37
  with open(sudachi_config_path, "r") as f:
38
  try:
 
24
  log.info("SudachiPyのユーザー辞書をビルドします。")
25
 
26
  # 入力用のユーザー辞書CSVファイルのパス
27
+ user_dict_path = get_file_path_from_config(
28
+ "sudachi.user_dict", "resources/user_dict.csv"
29
+ )
30
  # 出力のユーザー辞書のパス
31
  output_path = get_file_path_from_config(
32
+ "sudachi.user_dict_generated", "data/generated/user.dic"
33
  )
34
 
35
  # sudachi.jsonから辞書の種類を取得
36
  sudachi_config_path = get_file_path_from_config(
37
+ "sudachi.sudachi_config", "scripts/sudachi.json"
38
  )
39
  with open(sudachi_config_path, "r") as f:
40
  try:
scripts/2_create_projects_data.py CHANGED
@@ -22,10 +22,10 @@ def main():
22
 
23
  log.info("企画データの生成を開始します。")
24
  original_csv_path = get_file_path_from_config(
25
- "original_csv", "resources/original_projects.csv"
26
  )
27
  output_json_path = get_file_path_from_config(
28
- "output_json", "data/generated/projects.json"
29
  )
30
 
31
  # CSVファイルの読み込み
 
22
 
23
  log.info("企画データの生成を開始します。")
24
  original_csv_path = get_file_path_from_config(
25
+ "projects.original_csv", "resources/original_projects.csv"
26
  )
27
  output_json_path = get_file_path_from_config(
28
+ "projects.output_json", "data/generated/projects.json"
29
  )
30
 
31
  # CSVファイルの読み込み
utils/path.py CHANGED
@@ -1,9 +1,42 @@
1
  import json
 
2
 
3
  FILE_CONFIG_PATH = "config/files.json"
4
 
5
 
6
- def get_file_path_from_config(key: str, default: str) -> str:
7
- with open(FILE_CONFIG_PATH, "r") as f:
8
- config: dict[str, str] = json.load(f)
9
- return config.get(key, default)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import json
2
+ from typing import Any
3
 
4
  FILE_CONFIG_PATH = "config/files.json"
5
 
6
 
7
+ def _get_nested(d: dict[str, Any], keys: list[str]) -> Any:
8
+ cur: Any = d
9
+ for k in keys:
10
+ if not isinstance(cur, dict) or k not in cur:
11
+ raise KeyError(k)
12
+ cur = cur[k]
13
+ return cur
14
+
15
+
16
+ def get_file_path_from_config(
17
+ key: str, default: str | None = None, sep: str = "."
18
+ ) -> str:
19
+ """
20
+ 設定ファイルからファイルのパスを取得する。
21
+ 階層構造がある場合は`sep`で区切って指定する。
22
+
23
+ example: `get_file_path_from_config("projects.original_csv")`
24
+ """
25
+ with open(FILE_CONFIG_PATH, "r", encoding="utf-8") as f:
26
+ config: dict[str, Any] = json.load(f)
27
+
28
+ keys = key.split(sep) if sep in key else [key]
29
+
30
+ try:
31
+ value = _get_nested(config, keys)
32
+ except KeyError:
33
+ if default is not None:
34
+ return default
35
+ raise KeyError(f"Key '{key}' not found in {FILE_CONFIG_PATH}")
36
+
37
+ if isinstance(value, str):
38
+ return value
39
+
40
+ if default is not None:
41
+ return default
42
+ raise TypeError(f"Value at '{key}' is not a string: {type(value).__name__}")