forked from lyz-code/autoimport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
36 lines (24 loc) · 1 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Module to hold various utils."""
from pathlib import Path
from typing import Optional
PYPROJECT_FILENAME = "pyproject.toml"
def path_contains_pyproject(path: Path) -> bool:
"""Determine whether a `pyproject.toml` exists in the given path.
Args:
path (Path): the path in which to search for the `pyproject.toml`
Returns:
A boolean to indicate whether a `pyproject.toml` exists in the given path
"""
return (path / PYPROJECT_FILENAME).is_file()
def get_pyproject_path(starting_path: Optional[Path] = None) -> Optional[Path]:
"""Search for a `pyproject.toml` by traversing up the tree from a path.
Args:
starting_path (Path): an optional path from which to start searching
Returns:
The `Path` to the `pyproject.toml` if it exists or `None` if it doesn't
"""
start: Path = starting_path or Path.cwd()
for path in [start, *start.parents]:
if path_contains_pyproject(path):
return path / PYPROJECT_FILENAME
return None