diff --git a/src/darker/black_diff.py b/src/darker/black_diff.py index 8bf5a0c04..dbb9ed644 100644 --- a/src/darker/black_diff.py +++ b/src/darker/black_diff.py @@ -41,7 +41,6 @@ from black import FileMode as Mode from black import ( TargetVersion, - find_pyproject_toml, format_str, parse_pyproject_toml, re_compile_maybe_verbose, @@ -53,6 +52,7 @@ from black.files import gen_python_files from black.report import Report +from darker.files import find_pyproject_toml from darkgraylib.config import ConfigurationError from darkgraylib.utils import TextDocument diff --git a/src/darker/files.py b/src/darker/files.py new file mode 100644 index 000000000..ea42418a5 --- /dev/null +++ b/src/darker/files.py @@ -0,0 +1,27 @@ +from typing import Optional, Tuple + +from black import err, find_user_pyproject_toml + +from darkgraylib.files import find_project_root + + +def find_pyproject_toml( + path_search_start: Tuple[str, ...], stdin_filename: Optional[str] = None +) -> Optional[str]: + """Find the absolute filepath to a pyproject.toml if it exists""" + path_project_root, _ = find_project_root(path_search_start, stdin_filename) + path_pyproject_toml = path_project_root / "pyproject.toml" + if path_pyproject_toml.is_file(): + return str(path_pyproject_toml) + + try: + path_user_pyproject_toml = find_user_pyproject_toml() + return ( + str(path_user_pyproject_toml) + if path_user_pyproject_toml.is_file() + else None + ) + except (PermissionError, RuntimeError) as e: + # We do not have access to the user-level config directory, so ignore it. + err(f"Ignoring user configuration directory due to {e!r}") + return None