diff --git a/README.md b/README.md index bc048f3..b00e59b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ # pyrootutils -[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/tests.yaml/badge.svg)](https://github.com/ashleve/pyrootutils/actions/workflows/tests.yaml) +[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/) +[![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) +[![Issues](https://img.shields.io/github/issues/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/issues) +[![License](https://img.shields.io/github/license/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/blob/main/LICENSE) +[![Release](https://img.shields.io/pypi/v/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/) +[![PyPi](https://img.shields.io/pypi/dm/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/) A simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables. @@ -33,10 +38,10 @@ import pyrootutils # find absolute root path (searches for directory containing .project-root file) # search starts from current file and recursively goes over parent directories # returns pathlib object -path = pyrootutils.get_root(search_from=__file__, indicator=".project-root") +path = pyrootutils.find_root(search_from=__file__, indicator=".project-root") # find absolute root path (searches for directory containing any of the files on the list) -path = pyrootutils.get_root(search_from=__file__, indicator=[".git", "setup.cfg"]) +path = pyrootutils.find_root(search_from=__file__, indicator=[".git", "setup.cfg"]) # take advantage of the pathlib syntax data_dir = path / "data" @@ -56,7 +61,7 @@ pyrootutils.set_root( import pyrootutils -# combines get_root() and set_root() into one method +# combines find_root() and set_root() into one method root = pyrootutils.setup_root( search_from=__file__, indicator=".git" # indicator of the root directory diff --git a/pyrootutils/__init__.py b/pyrootutils/__init__.py index 7a0c188..7efaa3b 100644 --- a/pyrootutils/__init__.py +++ b/pyrootutils/__init__.py @@ -1,3 +1,3 @@ -from .pyrootutils import get_root, set_root, setup_root +from .pyrootutils import find_root, set_root, setup_root -__all__ = ["get_root", "set_root", "setup_root"] +__all__ = ["find_root", "set_root", "setup_root"] diff --git a/pyrootutils/pyrootutils.py b/pyrootutils/pyrootutils.py index 003994b..8d22d5d 100644 --- a/pyrootutils/pyrootutils.py +++ b/pyrootutils/pyrootutils.py @@ -30,8 +30,8 @@ def _pyrootutils_recursive_search(path: Path, indicators: Iterable[str]) -> Path return _pyrootutils_recursive_search(path.parent, indicators) -def get_root( - search_from: Union[str, Path], +def find_root( + search_from: Union[str, Path] = ".", indicator: Union[str, Iterable[str]] = ".project-root", ) -> Path: """Recursively searches for project root indicator(s), starting from given path. @@ -87,7 +87,6 @@ def set_root( Raises: FileNotFoundError: if root path does not exist. - Exception: if all options are False. Returns: None @@ -97,12 +96,6 @@ def set_root( if not os.path.exists(path): raise FileNotFoundError("Project root path does not exist.") - if not pythonpath and not cwd and not project_root_env_var and not dotenv: - raise Exception( - "No options selected. \ - , , , ." - ) - if pythonpath: sys.path.insert(0, path) @@ -137,6 +130,6 @@ def setup_root( Returns: Path: path to project root. """ - path = get_root(search_from, indicator) + path = find_root(search_from, indicator) set_root(path, pythonpath, cwd, project_root_env_var, dotenv) return path diff --git a/tests/test_pyrootutils.py b/tests/test_pyrootutils.py index 8ce5347..4216acd 100644 --- a/tests/test_pyrootutils.py +++ b/tests/test_pyrootutils.py @@ -4,64 +4,61 @@ import pytest -from pyrootutils import get_root, set_root, setup_root +from pyrootutils import find_root, set_root, setup_root def test_pyrootutils(): - assert get_root + assert find_root assert set_root assert setup_root -def test_get_root(): - path = get_root(__file__) +def test_find_root(): + path = find_root(__file__) assert path.exists() - path = get_root(str(__file__)) + path = find_root(str(__file__)) assert path.exists() - path = get_root(Path(__file__)) + path = find_root(Path(__file__)) assert path.exists() - path = get_root(__file__, ".git") + path = find_root(__file__, ".git") assert path.exists() - path = get_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"]) + path = find_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"]) assert path.exists() - path = get_root("") + path = find_root("") assert path.exists() - path = get_root(".") + path = find_root(".") assert path.exists() with pytest.raises(FileNotFoundError): - path = get_root(__file__, indicator="abc") + path = find_root(__file__, indicator="abc") with pytest.raises(FileNotFoundError): - path = get_root(__file__, indicator=["abc", "def", "fgh"]) + path = find_root(__file__, indicator=["abc", "def", "fgh"]) with pytest.raises(FileNotFoundError): - path = get_root(Path(__file__).parent.parent.parent) + path = find_root(Path(__file__).parent.parent.parent) with pytest.raises(TypeError): - path = get_root([]) + path = find_root([]) with pytest.raises(TypeError): - path = get_root("", ["abs", "def", 42]) + path = find_root("", ["abs", "def", 42]) def test_set_root(): - path = get_root(__file__) + path = find_root(__file__) assert path.exists() os.chdir(path.parent) assert os.getcwd() != str(path) - with pytest.raises(Exception): - set_root(path, pythonpath=False, cwd=False, project_root_env_var=False, dotenv=False) - assert "PROJECT_ROOT" not in os.environ set_root(path, pythonpath=False, cwd=False, project_root_env_var=True, dotenv=False)