Coverage for torxtools/pathtools.py: 54%
36 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 22:02 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 22:02 +0000
1"""
2Functions for working with filesystem paths and finding files.
4The :func:`expandpath` does recursive shell-like expansion of paths from lists.
5"""
7import os
8import typing as t
10import boltons.pathutils
12try:
13 pass
14except ImportError:
15 pass
17__all__ = [
18 "cachedir",
19 "expandpath",
20 "find_pyproject",
21]
24def expandpath(path: t.Union[str, t.List[str], None]) -> t.Union[str, t.List[str], None]:
25 """
26 Recursive shell-like expansion of environment variables and tilde home directory.
28 Parameters
29 ----------
30 path: str, [str], None
31 a single path, a list of paths, or none.
33 Returns
34 -------
35 str, [str], None:
36 a single expanded path, a list of expanded path, or none
38 Example
39 -------
41 .. code-block:: python
43 import os
44 from torxtools import pathtools
46 os.environ["SPAM"] = "eggs"
47 assert pathtools.expandpath(["~/$SPAM/one", "~/$SPAM/two"]) == [
48 os.path.expanduser("~/eggs/one"),
49 os.path.expanduser("~/eggs/two"),
50 ]
52 See Also
53 --------
54 :py:func:`boltons:boltons.pathutils.expandpath`
55 """
57 def _expandpath(path):
58 if path is None:
59 return None
60 if isinstance(path, list):
61 return [_expandpath(p) for p in path]
62 return boltons.pathutils.expandpath(path)
64 return _expandpath(path)
67def cachedir(appname: str, path: str) -> str:
68 """
69 Find a suitable location for cache files.
71 Parameters
72 ----------
73 appname: str
74 Name of application. Used as last part of the cachedir path.
76 path: str
77 a single path, a list of paths, or none.
79 Returns
80 -------
81 str, None:
82 a suitable cachedir, created if not existing
83 """
85 def create_cachedir(path: str) -> str:
86 # Check that path exists and is correct type
87 if os.path.isdir(path):
88 return path
89 os.mkdir(path)
90 return path
92 # Path was passed, create it
93 if path:
94 return create_cachedir(path)
96 if not appname:
97 return None
99 # Root: use /var/cache
100 if os.geteuid() == 0:
101 path = expandpath(f"/var/cache/{appname}")
102 return create_cachedir(path)
104 # Non-Root: use xdg
105 path = expandpath(f"$XDG_CACHE_HOME/{appname}")
106 return create_cachedir(path)
109def find_pyproject(path: str) -> str:
110 """
111 Find location of "pyproject.toml"
113 Parameters
114 ----------
115 path: str
116 a single path to a directory to search down recursively.
118 Returns
119 -------
120 str:
121 a absolute path to the location of 'pyproject.toml'
123 Example
124 -------
126 .. code-block:: python
128 import os
129 from torxtools import pathtools
131 pyproject = pathtools.find_pyproject(os.path.dirname(__file__))
132 """
134 path = os.path.abspath(path)
135 while not os.path.exists(f"{path}/pyproject.toml"):
136 path = os.path.dirname(path)
137 if path == "/": 137 ↛ 138line 137 didn't jump to line 138 because the condition on line 137 was never true
138 raise FileNotFoundError("Failed to find 'pyproject.toml' file")
140 return f"{path}/pyproject.toml"