-
Notifications
You must be signed in to change notification settings - Fork 16
/
here.py
60 lines (44 loc) · 1.53 KB
/
here.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Convenience to find the path for a file or directory relative to the root.
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 warnings
from pathlib import Path
from typing import Tuple
from . import criterion
from .root import 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() -> Tuple[Path, str]:
"""Return a tuple with the root path and a reason"""
start = Path.cwd()
path, reason = find_root_with_reason(CRITERIA, start=start)
return path, reason
# TODO: Implement set_here
def here(
relative_project_path: criterion._PathType = "", warn_missing: bool = False
) -> Path:
"""
Returns the path relative to the projects root directory.
:param relative_project_path: relative path from project root
:param warn_missing: warn user if path does not exist (default=False)
:return: pathlib path
Note: `reason` is not yet implemented.
"""
path, reason = get_here()
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