Skip to content

Commit

Permalink
Add list of defaults to indicator arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleve committed May 26, 2022
1 parent 6b9b9ef commit e8300af
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,6 @@ dmypy.json
# Pyre type checker
.pyre/

# VSCode
# IDE
.vscode
.idea
30 changes: 21 additions & 9 deletions pyrootutils/pyrootutils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
import sys
from pathlib import Path
from typing import Iterable, Union
from typing import Iterable, Optional, Union

from dotenv import load_dotenv


def _pyrootutils_recursive_search(path: Path, indicators: Iterable[str]) -> Path:
def _pyrootutils_recursive_search(path: Path, indicators: Iterable[str]) -> Optional[Path]:
"""Recursively search for files from the `indicators` list, starting from given path.
Args:
Expand All @@ -17,22 +17,28 @@ def _pyrootutils_recursive_search(path: Path, indicators: Iterable[str]) -> Path
FileNotFoundError: if root is not found.
Returns:
Path: path to folder containing at list one of the files from the list.
Optional[Path]: path to folder containing at list one of the files from the list.
"""
for file in indicators:
found = list(path.glob(file))
if len(found) > 0:
return path

if path.parent == path:
raise FileNotFoundError("Project root directory not found.")
return None

return _pyrootutils_recursive_search(path.parent, indicators)


def find_root(
search_from: Union[str, Path] = ".",
indicator: Union[str, Iterable[str]] = ".project-root",
indicator: Union[str, Iterable[str]] = (
".project-root",
"setup.cfg",
"setup.py",
".git",
"pyproject.toml",
),
) -> Path:
"""Recursively searches for project root indicator(s), starting from given path.
Expand Down Expand Up @@ -63,8 +69,8 @@ def find_root(

path = _pyrootutils_recursive_search(search_from, indicator)

if not path.exists():
raise FileNotFoundError("Project root directory not found.")
if not path or not path.exists():
raise FileNotFoundError(f"Project root directory not found. Indicators: {indicator}")

return path

Expand Down Expand Up @@ -94,7 +100,7 @@ def set_root(
path = str(path)

if not os.path.exists(path):
raise FileNotFoundError("Project root path does not exist.")
raise FileNotFoundError(f"Project root path does not exist: {path}")

if pythonpath:
sys.path.insert(0, path)
Expand All @@ -111,7 +117,13 @@ def set_root(

def setup_root(
search_from: Union[str, Path],
indicator: Union[str, Iterable[str]] = ".project-root",
indicator: Union[str, Iterable[str]] = (
".project-root",
"setup.cfg",
"setup.py",
".git",
"pyproject.toml",
),
pythonpath: bool = True,
cwd: bool = True,
project_root_env_var: bool = True,
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

setup(
name="pyrootutils",
version="1.0.1",
version="1.0.2",
license="MIT",
description="Simple package for setting up the root of the project.",
description="Simple package for easy project root setup",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/ashleve/pyrootutils",
Expand Down
15 changes: 9 additions & 6 deletions tests/test_pyrootutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def test_pyrootutils():


def test_find_root():
path = find_root()
assert path.exists()

path = find_root("")
assert path.exists()

path = find_root(".")
assert path.exists()

path = find_root(__file__)
assert path.exists()

Expand All @@ -29,12 +38,6 @@ def test_find_root():
path = find_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"])
assert path.exists()

path = find_root("")
assert path.exists()

path = find_root(".")
assert path.exists()

with pytest.raises(FileNotFoundError):
path = find_root(__file__, indicator="abc")

Expand Down

0 comments on commit e8300af

Please sign in to comment.