change os.path to pathlib.Path

pull/20910/head
Bowen Liang 11 months ago
parent 7f3709de4d
commit 14030b5700

@ -106,7 +106,7 @@ class DifyConfig(
TomlConfigSettingsSource( TomlConfigSettingsSource(
settings_cls=settings_cls, settings_cls=settings_cls,
toml_file=search_file_upwards( toml_file=search_file_upwards(
base_dir_path=os.path.dirname(Path(__file__)), base_dir_path=Path(__file__).parent,
target_file_name="pyproject.toml", target_file_name="pyproject.toml",
max_search_parent_depth=2, max_search_parent_depth=2,
), ),

@ -1,12 +1,13 @@
import os import os
from pathlib import Path
from typing import Optional from typing import Optional
def search_file_upwards( def search_file_upwards(
base_dir_path: str, base_dir_path: Path,
target_file_name: str, target_file_name: str,
max_search_parent_depth: int = 1, max_search_parent_depth: int,
) -> Optional[str]: ) -> Path:
""" """
Find a target file in the current directory or its parent directories up to a specified depth. Find a target file in the current directory or its parent directories up to a specified depth.
:param base_dir_path: Starting directory path to search from. :param base_dir_path: Starting directory path to search from.
@ -14,17 +15,18 @@ def search_file_upwards(
:param max_search_parent_depth: Maximum number of parent directories to search upwards. :param max_search_parent_depth: Maximum number of parent directories to search upwards.
:return: Path of the file if found, otherwise None. :return: Path of the file if found, otherwise None.
""" """
current_dir = base_dir_path current_path = base_dir_path.resolve()
for level in range(max_search_parent_depth): for _ in range(max_search_parent_depth):
candidate = os.path.join(current_dir, target_file_name) candidate_path = current_path / target_file_name
if os.path.isfile(candidate): if candidate_path.is_file():
return candidate return candidate_path
parent_dir = os.path.dirname(current_dir) parent_path = current_path.parent
if parent_dir == current_dir: if parent_path == current_path: # reached the root directory
break break
current_dir = parent_dir else:
current_path = parent_path
raise ValueError( raise ValueError(
f"File '{target_file_name}' not found in the directory '{base_dir_path}' or its parent directories" f"File '{target_file_name}' not found in the directory '{base_dir_path.resolve()}' or its parent directories"
f" in depth of {max_search_parent_depth}." f" in depth of {max_search_parent_depth}."
) )

Loading…
Cancel
Save