Skip to content

Commit 26710ac

Browse files
committed
Rename get_root() to find_root()
1 parent d3e9e4a commit 26710ac

4 files changed

Lines changed: 30 additions & 35 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# pyrootutils
22

3-
[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/tests.yaml/badge.svg)](https://github.com/ashleve/pyrootutils/actions/workflows/tests.yaml)
3+
[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
4+
[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml)
5+
[![Issues](https://img.shields.io/github/issues/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/issues)
6+
[![License](https://img.shields.io/github/license/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/blob/main/LICENSE)
7+
[![Release](https://img.shields.io/pypi/v/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)
8+
[![PyPi](https://img.shields.io/pypi/dm/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)
49

510
A simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables.
611

@@ -33,10 +38,10 @@ import pyrootutils
3338
# find absolute root path (searches for directory containing .project-root file)
3439
# search starts from current file and recursively goes over parent directories
3540
# returns pathlib object
36-
path = pyrootutils.get_root(search_from=__file__, indicator=".project-root")
41+
path = pyrootutils.find_root(search_from=__file__, indicator=".project-root")
3742

3843
# find absolute root path (searches for directory containing any of the files on the list)
39-
path = pyrootutils.get_root(search_from=__file__, indicator=[".git", "setup.cfg"])
44+
path = pyrootutils.find_root(search_from=__file__, indicator=[".git", "setup.cfg"])
4045

4146
# take advantage of the pathlib syntax
4247
data_dir = path / "data"
@@ -56,7 +61,7 @@ pyrootutils.set_root(
5661
import pyrootutils
5762

5863

59-
# combines get_root() and set_root() into one method
64+
# combines find_root() and set_root() into one method
6065
root = pyrootutils.setup_root(
6166
search_from=__file__,
6267
indicator=".git" # indicator of the root directory

pyrootutils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .pyrootutils import get_root, set_root, setup_root
1+
from .pyrootutils import find_root, set_root, setup_root
22

3-
__all__ = ["get_root", "set_root", "setup_root"]
3+
__all__ = ["find_root", "set_root", "setup_root"]

pyrootutils/pyrootutils.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def _pyrootutils_recursive_search(path: Path, indicators: Iterable[str]) -> Path
3030
return _pyrootutils_recursive_search(path.parent, indicators)
3131

3232

33-
def get_root(
34-
search_from: Union[str, Path],
33+
def find_root(
34+
search_from: Union[str, Path] = ".",
3535
indicator: Union[str, Iterable[str]] = ".project-root",
3636
) -> Path:
3737
"""Recursively searches for project root indicator(s), starting from given path.
@@ -87,7 +87,6 @@ def set_root(
8787
8888
Raises:
8989
FileNotFoundError: if root path does not exist.
90-
Exception: if all options are False.
9190
9291
Returns:
9392
None
@@ -97,12 +96,6 @@ def set_root(
9796
if not os.path.exists(path):
9897
raise FileNotFoundError("Project root path does not exist.")
9998

100-
if not pythonpath and not cwd and not project_root_env_var and not dotenv:
101-
raise Exception(
102-
"No options selected. \
103-
<pythonpath=False>, <cwd=False>, <project_root_env_var=False>, <dotenv=False>."
104-
)
105-
10699
if pythonpath:
107100
sys.path.insert(0, path)
108101

@@ -137,6 +130,6 @@ def setup_root(
137130
Returns:
138131
Path: path to project root.
139132
"""
140-
path = get_root(search_from, indicator)
133+
path = find_root(search_from, indicator)
141134
set_root(path, pythonpath, cwd, project_root_env_var, dotenv)
142135
return path

tests/test_pyrootutils.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,61 @@
44

55
import pytest
66

7-
from pyrootutils import get_root, set_root, setup_root
7+
from pyrootutils import find_root, set_root, setup_root
88

99

1010
def test_pyrootutils():
11-
assert get_root
11+
assert find_root
1212
assert set_root
1313
assert setup_root
1414

1515

16-
def test_get_root():
17-
path = get_root(__file__)
16+
def test_find_root():
17+
path = find_root(__file__)
1818
assert path.exists()
1919

20-
path = get_root(str(__file__))
20+
path = find_root(str(__file__))
2121
assert path.exists()
2222

23-
path = get_root(Path(__file__))
23+
path = find_root(Path(__file__))
2424
assert path.exists()
2525

26-
path = get_root(__file__, ".git")
26+
path = find_root(__file__, ".git")
2727
assert path.exists()
2828

29-
path = get_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"])
29+
path = find_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"])
3030
assert path.exists()
3131

32-
path = get_root("")
32+
path = find_root("")
3333
assert path.exists()
3434

35-
path = get_root(".")
35+
path = find_root(".")
3636
assert path.exists()
3737

3838
with pytest.raises(FileNotFoundError):
39-
path = get_root(__file__, indicator="abc")
39+
path = find_root(__file__, indicator="abc")
4040

4141
with pytest.raises(FileNotFoundError):
42-
path = get_root(__file__, indicator=["abc", "def", "fgh"])
42+
path = find_root(__file__, indicator=["abc", "def", "fgh"])
4343

4444
with pytest.raises(FileNotFoundError):
45-
path = get_root(Path(__file__).parent.parent.parent)
45+
path = find_root(Path(__file__).parent.parent.parent)
4646

4747
with pytest.raises(TypeError):
48-
path = get_root([])
48+
path = find_root([])
4949

5050
with pytest.raises(TypeError):
51-
path = get_root("", ["abs", "def", 42])
51+
path = find_root("", ["abs", "def", 42])
5252

5353

5454
def test_set_root():
5555

56-
path = get_root(__file__)
56+
path = find_root(__file__)
5757
assert path.exists()
5858

5959
os.chdir(path.parent)
6060
assert os.getcwd() != str(path)
6161

62-
with pytest.raises(Exception):
63-
set_root(path, pythonpath=False, cwd=False, project_root_env_var=False, dotenv=False)
64-
6562
assert "PROJECT_ROOT" not in os.environ
6663

6764
set_root(path, pythonpath=False, cwd=False, project_root_env_var=True, dotenv=False)

0 commit comments

Comments
 (0)