-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor to align with rprojroot and here
- Loading branch information
1 parent
9fa6093
commit a315c57
Showing
9 changed files
with
295 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
from .pyprojroot import * | ||
|
||
__version__ = "0.2.0" | ||
from .criterion import * | ||
from .root import find_root, find_root_with_reason |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
This module is inspired by the `rprojroot` library for R. | ||
See https://github.com/r-lib/rprojroot. | ||
It is intended for interactive or programmatic only. | ||
""" | ||
|
||
import pathlib as _pathlib | ||
import typing | ||
from os import PathLike as _PathLike | ||
|
||
# TODO: It would be nice to have a class that encapsulates these checks, | ||
# so that we can implement methods like |, !, &, ^ operators | ||
|
||
# TODO: Refactor in a way that allows creation of reasons | ||
|
||
|
||
def as_root_criterion(criterion) -> typing.Callable: | ||
if callable(criterion): | ||
return criterion | ||
|
||
# criterion must be a Collection, rather than just Iterable | ||
if isinstance(criterion, _PathLike): | ||
criterion = [criterion] | ||
criterion = list(criterion) | ||
|
||
def f(path: _pathlib.Path) -> bool: | ||
for c in criterion: | ||
if isinstance(c, _PathLike): | ||
if (path / c).exists(): | ||
return True | ||
else: | ||
if c(path): | ||
return True | ||
return False | ||
|
||
return f | ||
|
||
|
||
def has_file(file: _PathLike) -> typing.Callable: | ||
""" | ||
Check that specified file exists in path. | ||
Note that a directory with that name will not match. | ||
""" | ||
|
||
def f(path: _pathlib.Path) -> bool: | ||
return (path / file).is_file() | ||
|
||
return f | ||
|
||
|
||
def has_dir(file: _PathLike) -> typing.Callable: | ||
""" | ||
Check that specified directory exists. | ||
Note that a regular file with that name will not match. | ||
""" | ||
|
||
def f(path: _pathlib.Path) -> bool: | ||
return (path / file).is_dir() | ||
|
||
return f | ||
|
||
|
||
def matches_glob(pat: str) -> typing.Callable: | ||
""" | ||
Check that glob has at least one match. | ||
""" | ||
|
||
def f(path: _pathlib.Path) -> bool: | ||
matches = path.glob(pat) | ||
try: | ||
# Only need to get one item from generator | ||
next(matches) | ||
except StopIteration: | ||
return False | ||
else: | ||
return True | ||
|
||
return f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
This module is inspired by the `here` library for R. | ||
See https://github.com/r-lib/here. | ||
It is intended for interactive use only. | ||
""" | ||
|
||
import pathlib as _pathlib | ||
import warnings as _warnings | ||
from os import PathLike as _PathLike | ||
|
||
from . import criterion | ||
from .root import find_root, find_root_with_reason | ||
|
||
CRITERIA = [ | ||
criterion.has_file(".here"), | ||
criterion.has_dir(".git"), | ||
criterion.matches_glob("*.Rproj"), | ||
criterion.has_file("requirements.txt"), | ||
criterion.has_file("setup.py"), | ||
criterion.has_dir(".dvc"), | ||
criterion.has_dir(".spyproject"), | ||
criterion.has_file("pyproject.toml"), | ||
criterion.has_dir(".idea"), | ||
criterion.has_dir(".vscode"), | ||
] | ||
|
||
|
||
def get_here(): | ||
# TODO: This should only find_root once per session | ||
start = _pathlib.Path.cwd() | ||
path, reason = find_root_with_reason(CRITERIA, start=start) | ||
return path, reason | ||
|
||
|
||
# TODO: Implement set_here | ||
|
||
|
||
def here(relative_project_path: _PathLike = "", warn_missing=False) -> _pathlib.Path: | ||
""" | ||
Returns the path relative to the projects root directory. | ||
:param relative_project_path: relative path from project root | ||
:param project_files: list of files to track inside the project | ||
:param warn_missing: warn user if path does not exist (default=False) | ||
:return: pathlib path | ||
""" | ||
path, reason = get_here() | ||
# TODO: Show reason when requested | ||
|
||
if relative_project_path: | ||
path = path / relative_project_path | ||
|
||
if warn_missing and not path.exists(): | ||
_warnings.warn(f"Path doesn't exist: {path!s}") | ||
return path |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
This module is inspired by the `rprojroot` library for R. | ||
See https://github.com/r-lib/rprojroot. | ||
It is intended for interactive or programmatic only. | ||
""" | ||
|
||
import pathlib as _pathlib | ||
import typing as _typing | ||
from os import PathLike as _PathLike | ||
|
||
from .criterion import as_root_criterion as _as_root_criterion | ||
|
||
|
||
def as_start_path(start: _PathLike) -> _pathlib.Path: | ||
if start is None: | ||
return _pathlib.Path.cwd() | ||
if not isinstance(start, _pathlib.Path): | ||
start = _pathlib.Path(start) | ||
# TODO: consider `start = start.resolve()` | ||
return start | ||
|
||
|
||
def find_root_with_reason( | ||
criterion, start: _PathLike = None | ||
) -> _typing.Tuple[_pathlib.Path, str]: | ||
""" | ||
Find directory matching root criterion with reason. | ||
Recursively search parents of start path for directory | ||
matching root criterion with reason. | ||
""" | ||
# TODO: Implement reasons | ||
|
||
# Prepare inputs | ||
criterion = _as_root_criterion(criterion) | ||
start = as_start_path(start) | ||
|
||
# Check start | ||
if start.is_dir() and criterion(start): | ||
return start, "Pass" | ||
|
||
# Iterate over all parents | ||
# TODO: Consider adding maximum depth | ||
# TODO: Consider limiting depth to path (e.g. "if p == stop: raise") | ||
for p in start.parents: | ||
if criterion(p): | ||
return p, "Pass" | ||
|
||
# Not found | ||
raise RuntimeError("Project root not found.") | ||
|
||
|
||
def find_root(criterion, start: _PathLike = None, **kwargs) -> _pathlib.Path: | ||
""" | ||
Find directory matching root criterion. | ||
Recursively search parents of start path for directory | ||
matching root criterion. | ||
""" | ||
try: | ||
root, _ = find_root_with_reason(criterion, start=start, **kwargs) | ||
except RuntimeError as ex: | ||
raise ex | ||
else: | ||
return root |
Oops, something went wrong.