dycw-utilities 0.138.8__py3-none-any.whl → 0.138.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-utilities
3
- Version: 0.138.8
3
+ Version: 0.138.10
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,4 +1,4 @@
1
- utilities/__init__.py,sha256=PNZtdWOozZol0q-8PP5HDXblyuTImCmKVhJDZ6aPzOM,60
1
+ utilities/__init__.py,sha256=KXSG109KauV-mzYqWH_b1979j4n9biCSq0kQFI5m5Z4,61
2
2
  utilities/aiolimiter.py,sha256=mD0wEiqMgwpty4XTbawFpnkkmJS6R4JRsVXFUaoitSU,628
3
3
  utilities/altair.py,sha256=HeZBVUocjkrTNwwKrClppsIqgNFF-ykv05HfZSoHYno,9104
4
4
  utilities/asyncio.py,sha256=dcGeKQzjLBXxKzZkVIk5oZsFXEcynVbRB9iNB5XEDZk,38526
@@ -42,7 +42,7 @@ utilities/optuna.py,sha256=C-fhWYiXHVPo1l8QctYkFJ4DyhbSrGorzP1dJb_qvd8,1933
42
42
  utilities/orjson.py,sha256=WWV2QukCIuwT8OAOtmKhLhxezXPVbeA_fQCucmGmbRA,37106
43
43
  utilities/os.py,sha256=yMNAKMyY8oFgQ1yN3TQYnwa5-A_FXz4tCDbhIctQHSs,3736
44
44
  utilities/parse.py,sha256=JcJn5yXKhIWXBCwgBdPsyu7Hvcuw6kyEdqvaebCaI9k,17951
45
- utilities/pathlib.py,sha256=NT0EzXBzkVMByO1GZdZYN1wLt0_pikAV7rLBtgLKF-Y,7291
45
+ utilities/pathlib.py,sha256=5p9MI8m7iUFksNVGiLavN4MZ3cj1CpBiLBlgaCR0LCs,8334
46
46
  utilities/period.py,sha256=6jEff_qAiE7xdFaQ1DnKgNf10D2wHhzt7hQXCBoKlgc,6842
47
47
  utilities/pickle.py,sha256=MBT2xZCsv0pH868IXLGKnlcqNx2IRVKYNpRcqiQQqxw,653
48
48
  utilities/platform.py,sha256=5uCKRf_ij7ukJDcbnNfhY2ay9fbrpiNLRO1t2QvcwqQ,2825
@@ -88,8 +88,8 @@ utilities/warnings.py,sha256=un1LvHv70PU-LLv8RxPVmugTzDJkkGXRMZTE2-fTQHw,1771
88
88
  utilities/whenever.py,sha256=R5d9UCNCdAOyjwLUmfH2Vn8Ykee8OHQi2skRTFfbZMM,20492
89
89
  utilities/zipfile.py,sha256=24lQc9ATcJxHXBPc_tBDiJk48pWyRrlxO2fIsFxU0A8,699
90
90
  utilities/zoneinfo.py,sha256=oEH-nL3t4h9uawyZqWDtNtDAl6M-CLpLYGI_nI6DulM,1971
91
- dycw_utilities-0.138.8.dist-info/METADATA,sha256=MVaYliyhueRarnK_LZuye5BRShl7DtclUDjY6wyVfdU,1638
92
- dycw_utilities-0.138.8.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
93
- dycw_utilities-0.138.8.dist-info/entry_points.txt,sha256=uLj5QWWVXv8tnMaRX3ZGYpt7w1xzLWU6LxbFhELEpkc,68
94
- dycw_utilities-0.138.8.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
95
- dycw_utilities-0.138.8.dist-info/RECORD,,
91
+ dycw_utilities-0.138.10.dist-info/METADATA,sha256=5_CxObrlGLTgwHzV1c8558AlnuS2yZx8skO-M6OisCM,1639
92
+ dycw_utilities-0.138.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
93
+ dycw_utilities-0.138.10.dist-info/entry_points.txt,sha256=uLj5QWWVXv8tnMaRX3ZGYpt7w1xzLWU6LxbFhELEpkc,68
94
+ dycw_utilities-0.138.10.dist-info/licenses/LICENSE,sha256=gppZp16M6nSVpBbUBrNL6JuYfvKwZiKgV7XoKKsHzqo,1066
95
+ dycw_utilities-0.138.10.dist-info/RECORD,,
utilities/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.138.8"
3
+ __version__ = "0.138.10"
utilities/pathlib.py CHANGED
@@ -71,8 +71,35 @@ def get_path(
71
71
  ##
72
72
 
73
73
 
74
- def get_root(*, path: MaybeCallablePathLike | None = None) -> Path:
75
- """Get the root of a path."""
74
+ def get_package_root(*, path: MaybeCallablePathLike | None = None) -> Path:
75
+ """Get the package root."""
76
+ path = get_path(path=path)
77
+ path_dir = path.parent if path.is_file() else path
78
+ all_paths = list(chain([path_dir], path_dir.parents))
79
+ try:
80
+ return next(
81
+ p.resolve()
82
+ for p in all_paths
83
+ if any(p_i.name == "pyproject.toml" for p_i in p.iterdir())
84
+ )
85
+ except StopIteration:
86
+ raise GetPackageRootError(path=path) from None
87
+
88
+
89
+ @dataclass(kw_only=True, slots=True)
90
+ class GetPackageRootError(Exception):
91
+ path: PathLike
92
+
93
+ @override
94
+ def __str__(self) -> str:
95
+ return f"Path is not part of a package: {self.path}"
96
+
97
+
98
+ ##
99
+
100
+
101
+ def get_repo_root(*, path: MaybeCallablePathLike | None = None) -> Path:
102
+ """Get the repo root."""
76
103
  path = get_path(path=path)
77
104
  path_dir = path.parent if path.is_file() else path
78
105
  try:
@@ -85,36 +112,52 @@ def get_root(*, path: MaybeCallablePathLike | None = None) -> Path:
85
112
  except CalledProcessError as error:
86
113
  # newer versions of git report "Not a git repository", whilst older
87
114
  # versions report "not a git repository"
88
- if not search("fatal: not a git repository", error.stderr, flags=IGNORECASE):
89
- raise # pragma: no cover
90
- root_git = None
115
+ if search("fatal: not a git repository", error.stderr, flags=IGNORECASE):
116
+ raise GetRepoRootError(path=path) from None
117
+ raise # pragma: no cover
91
118
  else:
92
- root_git = Path(output.strip("\n")).resolve()
93
- all_paths = list(chain([path_dir], path_dir.parents))
119
+ return Path(output.strip("\n"))
120
+
121
+
122
+ @dataclass(kw_only=True, slots=True)
123
+ class GetRepoRootError(Exception):
124
+ path: PathLike
125
+
126
+ @override
127
+ def __str__(self) -> str:
128
+ return f"Path is not part of a `git` repository: {self.path}"
129
+
130
+
131
+ ##
132
+
133
+
134
+ def get_root(*, path: MaybeCallablePathLike | None = None) -> Path:
135
+ """Get the root of a path."""
136
+ path = get_path(path=path)
94
137
  try:
95
- root_envrc = next(
96
- p.resolve()
97
- for p in all_paths
98
- if any(p_i.name == ".envrc" for p_i in p.iterdir())
99
- )
100
- except StopIteration:
101
- root_envrc = None
102
- match root_git, root_envrc:
138
+ repo = get_repo_root(path=path)
139
+ except GetRepoRootError:
140
+ repo = None
141
+ try:
142
+ package = get_package_root(path=path)
143
+ except GetPackageRootError:
144
+ package = None
145
+ match repo, package:
103
146
  case None, None:
104
147
  raise GetRootError(path=path)
105
148
  case Path(), None:
106
- return root_git
149
+ return repo
107
150
  case None, Path():
108
- return root_envrc
151
+ return package
109
152
  case Path(), Path():
110
- if root_git == root_envrc:
111
- return root_git
112
- if is_sub_path(root_git, root_envrc, strict=True):
113
- return root_git
114
- if is_sub_path(root_envrc, root_git, strict=True):
115
- return root_envrc
153
+ if repo == package:
154
+ return repo
155
+ if is_sub_path(repo, package, strict=True):
156
+ return repo
157
+ if is_sub_path(package, repo, strict=True):
158
+ return package
116
159
  raise ImpossibleCaseError( # pragma: no cover
117
- case=[f"{root_git=}", f"{root_envrc=}"]
160
+ case=[f"{repo=}", f"{package=}"]
118
161
  )
119
162
  case _ as never:
120
163
  assert_never(never)
@@ -259,10 +302,14 @@ def temp_cwd(path: PathLike, /) -> Iterator[None]:
259
302
 
260
303
  __all__ = [
261
304
  "PWD",
305
+ "GetPackageRootError",
306
+ "GetRepoRootError",
262
307
  "GetTailError",
263
308
  "ensure_suffix",
264
309
  "expand_path",
310
+ "get_package_root",
265
311
  "get_path",
312
+ "get_repo_root",
266
313
  "get_tail",
267
314
  "is_sub_path",
268
315
  "list_dir",