scitex-path 0.1.0__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.
- scitex_path/__init__.py +48 -0
- scitex_path/_clean.py +58 -0
- scitex_path/_find.py +90 -0
- scitex_path/_get_module_path.py +53 -0
- scitex_path/_get_spath.py +13 -0
- scitex_path/_getsize.py +31 -0
- scitex_path/_increment_version.py +80 -0
- scitex_path/_mk_spath.py +48 -0
- scitex_path/_path.py +12 -0
- scitex_path/_split.py +35 -0
- scitex_path/_symlink.py +295 -0
- scitex_path/_this_path.py +33 -0
- scitex_path/_version.py +108 -0
- scitex_path-0.1.0.dist-info/METADATA +83 -0
- scitex_path-0.1.0.dist-info/RECORD +18 -0
- scitex_path-0.1.0.dist-info/WHEEL +4 -0
- scitex_path-0.1.0.dist-info/entry_points.txt +2 -0
- scitex_path-0.1.0.dist-info/licenses/LICENSE +661 -0
scitex_path/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""scitex-path: Scientific project path utilities (find, split, symlink, versioning)."""
|
|
3
|
+
|
|
4
|
+
from ._clean import clean
|
|
5
|
+
from ._find import find_dir, find_file, find_git_root
|
|
6
|
+
from ._get_module_path import get_data_path_from_a_package
|
|
7
|
+
from ._get_spath import get_spath
|
|
8
|
+
from ._getsize import getsize
|
|
9
|
+
from ._increment_version import increment_version
|
|
10
|
+
from ._mk_spath import mk_spath
|
|
11
|
+
from ._path import get_this_path, this_path
|
|
12
|
+
from ._split import split
|
|
13
|
+
from ._symlink import (
|
|
14
|
+
create_relative_symlink,
|
|
15
|
+
fix_broken_symlinks,
|
|
16
|
+
is_symlink,
|
|
17
|
+
list_symlinks,
|
|
18
|
+
readlink,
|
|
19
|
+
resolve_symlinks,
|
|
20
|
+
symlink,
|
|
21
|
+
unlink_symlink,
|
|
22
|
+
)
|
|
23
|
+
from ._this_path import get_this_path, this_path
|
|
24
|
+
from ._version import find_latest, increment_version
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"clean",
|
|
28
|
+
"create_relative_symlink",
|
|
29
|
+
"find_dir",
|
|
30
|
+
"find_file",
|
|
31
|
+
"find_git_root",
|
|
32
|
+
"find_latest",
|
|
33
|
+
"fix_broken_symlinks",
|
|
34
|
+
"get_data_path_from_a_package",
|
|
35
|
+
"get_spath",
|
|
36
|
+
"get_this_path",
|
|
37
|
+
"getsize",
|
|
38
|
+
"increment_version",
|
|
39
|
+
"is_symlink",
|
|
40
|
+
"list_symlinks",
|
|
41
|
+
"mk_spath",
|
|
42
|
+
"readlink",
|
|
43
|
+
"resolve_symlinks",
|
|
44
|
+
"split",
|
|
45
|
+
"symlink",
|
|
46
|
+
"this_path",
|
|
47
|
+
"unlink_symlink",
|
|
48
|
+
]
|
scitex_path/_clean.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
# Time-stamp: "2025-05-15 00:55:30 (ywatanabe)"
|
|
4
|
+
# File: /data/gpfs/projects/punim2354/ywatanabe/scitex_repo/src/scitex/path/_clean.py
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def clean(path_string):
|
|
10
|
+
"""Cleans and normalizes a file system path string.
|
|
11
|
+
|
|
12
|
+
Example
|
|
13
|
+
-------
|
|
14
|
+
>>> clean('/home/user/./folder/../file.txt')
|
|
15
|
+
'/home/user/file.txt'
|
|
16
|
+
>>> clean('path/./to//file.txt')
|
|
17
|
+
'path/to/file.txt'
|
|
18
|
+
>>> clean('path with spaces')
|
|
19
|
+
'path_with_spaces'
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
path_string : str
|
|
24
|
+
File path to clean
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
str
|
|
29
|
+
Normalized path string
|
|
30
|
+
"""
|
|
31
|
+
# Convert Path objects to strings to avoid AttributeError
|
|
32
|
+
if hasattr(path_string, "__fspath__"): # Check if it's a path-like object
|
|
33
|
+
path_string = str(path_string)
|
|
34
|
+
|
|
35
|
+
if not path_string:
|
|
36
|
+
return ""
|
|
37
|
+
|
|
38
|
+
# Remember if path ends with a slash (indicating a directory)
|
|
39
|
+
is_directory = path_string.endswith("/")
|
|
40
|
+
|
|
41
|
+
# Replace spaces with underscores
|
|
42
|
+
path_string = path_string.replace(" ", "_")
|
|
43
|
+
|
|
44
|
+
# Use normpath to handle ../ and ./ references
|
|
45
|
+
cleaned_path = os.path.normpath(path_string)
|
|
46
|
+
|
|
47
|
+
# Replace multiple slashes with single slash
|
|
48
|
+
while "//" in cleaned_path:
|
|
49
|
+
cleaned_path = cleaned_path.replace("//", "/")
|
|
50
|
+
|
|
51
|
+
# Restore trailing slash if it was a directory
|
|
52
|
+
if is_directory and not cleaned_path.endswith("/"):
|
|
53
|
+
cleaned_path += "/"
|
|
54
|
+
|
|
55
|
+
return cleaned_path
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# EOF
|
scitex_path/_find.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_find.py
|
|
4
|
+
|
|
5
|
+
"""File and directory finding utilities."""
|
|
6
|
+
|
|
7
|
+
import fnmatch
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import List, Optional, Union
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def find_git_root() -> Path:
|
|
13
|
+
"""Find the root directory of the current git repository.
|
|
14
|
+
|
|
15
|
+
Returns
|
|
16
|
+
-------
|
|
17
|
+
Path
|
|
18
|
+
Path to the git repository root.
|
|
19
|
+
"""
|
|
20
|
+
import git
|
|
21
|
+
|
|
22
|
+
repo = git.Repo(".", search_parent_directories=True)
|
|
23
|
+
return Path(repo.working_tree_dir)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def find_dir(root_dir: Union[str, Path], exp: Union[str, List[str]]) -> List[Path]:
|
|
27
|
+
"""Find directories matching pattern."""
|
|
28
|
+
return _find(root_dir, type="d", exp=exp)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def find_file(root_dir: Union[str, Path], exp: Union[str, List[str]]) -> List[Path]:
|
|
32
|
+
"""Find files matching pattern."""
|
|
33
|
+
return _find(root_dir, type="f", exp=exp)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _find(
|
|
37
|
+
rootdir: Union[str, Path],
|
|
38
|
+
type: Optional[str] = "f",
|
|
39
|
+
exp: Union[str, List[str]] = "*",
|
|
40
|
+
) -> List[Path]:
|
|
41
|
+
"""Mimics the Unix find command.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
rootdir : str or Path
|
|
46
|
+
Root directory to search in.
|
|
47
|
+
type : str, optional
|
|
48
|
+
'f' for files, 'd' for directories, None for both.
|
|
49
|
+
exp : str or list of str
|
|
50
|
+
Pattern(s) to match.
|
|
51
|
+
|
|
52
|
+
Returns
|
|
53
|
+
-------
|
|
54
|
+
list of Path
|
|
55
|
+
Matching paths.
|
|
56
|
+
|
|
57
|
+
Example
|
|
58
|
+
-------
|
|
59
|
+
>>> _find('/path/to/search', "f", "*.txt")
|
|
60
|
+
"""
|
|
61
|
+
rootdir = Path(rootdir)
|
|
62
|
+
if isinstance(exp, str):
|
|
63
|
+
exp = [exp]
|
|
64
|
+
|
|
65
|
+
exclude_keys = ["/lib/", "/env/", "/build/"]
|
|
66
|
+
matches = []
|
|
67
|
+
|
|
68
|
+
for _exp in exp:
|
|
69
|
+
for path in rootdir.rglob("*"):
|
|
70
|
+
# Check type
|
|
71
|
+
if type == "f" and not path.is_file():
|
|
72
|
+
continue
|
|
73
|
+
if type == "d" and not path.is_dir():
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
# Check pattern match
|
|
77
|
+
if _exp and not fnmatch.fnmatch(path.name, _exp):
|
|
78
|
+
continue
|
|
79
|
+
|
|
80
|
+
# Check exclusions
|
|
81
|
+
path_str = str(path)
|
|
82
|
+
if any(ek in path_str for ek in exclude_keys):
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
matches.append(path)
|
|
86
|
+
|
|
87
|
+
return matches
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# EOF
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_get_module_path.py
|
|
4
|
+
|
|
5
|
+
"""Module path utilities."""
|
|
6
|
+
|
|
7
|
+
import importlib.util
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_data_path_from_a_package(package_str: str, resource: str) -> Path:
|
|
12
|
+
"""Get the path to a data file within a package.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
package_str : str
|
|
17
|
+
The name of the package as a string.
|
|
18
|
+
resource : str
|
|
19
|
+
The name of the resource file within the package's data directory.
|
|
20
|
+
|
|
21
|
+
Returns
|
|
22
|
+
-------
|
|
23
|
+
Path
|
|
24
|
+
The full path to the resource file.
|
|
25
|
+
|
|
26
|
+
Raises
|
|
27
|
+
------
|
|
28
|
+
ImportError
|
|
29
|
+
If the specified package cannot be found.
|
|
30
|
+
FileNotFoundError
|
|
31
|
+
If the resource file does not exist in the package's data directory.
|
|
32
|
+
"""
|
|
33
|
+
spec = importlib.util.find_spec(package_str)
|
|
34
|
+
if spec is None:
|
|
35
|
+
raise ImportError(f"Package '{package_str}' not found")
|
|
36
|
+
|
|
37
|
+
origin = Path(spec.origin)
|
|
38
|
+
data_dir = origin.parents[0]
|
|
39
|
+
while "src" not in str(data_dir) and data_dir != data_dir.parent:
|
|
40
|
+
data_dir = data_dir.parent
|
|
41
|
+
data_dir = data_dir.parent / "data"
|
|
42
|
+
|
|
43
|
+
resource_path = data_dir / resource
|
|
44
|
+
|
|
45
|
+
if not resource_path.exists():
|
|
46
|
+
raise FileNotFoundError(
|
|
47
|
+
f"Resource '{resource}' not found in package '{package_str}'"
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
return resource_path
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# EOF
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_get_spath.py
|
|
4
|
+
|
|
5
|
+
"""Save path utilities - deprecated, use _mk_spath.py instead."""
|
|
6
|
+
|
|
7
|
+
from ._mk_spath import mk_spath
|
|
8
|
+
|
|
9
|
+
# Alias for backward compatibility
|
|
10
|
+
get_spath = mk_spath
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# EOF
|
scitex_path/_getsize.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: scitex-path/src/scitex_path/_getsize.py
|
|
4
|
+
|
|
5
|
+
"""File size utilities."""
|
|
6
|
+
|
|
7
|
+
import math
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Union
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def getsize(path: Union[str, Path]) -> Union[int, float]:
|
|
13
|
+
"""Get file size in bytes.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
path : str or Path
|
|
18
|
+
Path to file.
|
|
19
|
+
|
|
20
|
+
Returns
|
|
21
|
+
-------
|
|
22
|
+
int or float
|
|
23
|
+
File size in bytes, or math.nan if file doesn't exist.
|
|
24
|
+
"""
|
|
25
|
+
path = Path(path)
|
|
26
|
+
if path.exists():
|
|
27
|
+
return path.stat().st_size
|
|
28
|
+
return math.nan
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# EOF
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_increment_version.py
|
|
4
|
+
|
|
5
|
+
"""File versioning utilities."""
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Union
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def increment_version(
|
|
13
|
+
dirname: Union[str, Path],
|
|
14
|
+
fname: str,
|
|
15
|
+
ext: str,
|
|
16
|
+
version_prefix: str = "_v",
|
|
17
|
+
) -> Path:
|
|
18
|
+
"""Generate the next version of a filename based on existing versioned files.
|
|
19
|
+
|
|
20
|
+
This function searches for files in the given directory that match the pattern:
|
|
21
|
+
{fname}{version_prefix}{number}{ext} and returns the path for the next version.
|
|
22
|
+
|
|
23
|
+
Parameters
|
|
24
|
+
----------
|
|
25
|
+
dirname : str or Path
|
|
26
|
+
The directory to search in and where the new file will be created.
|
|
27
|
+
fname : str
|
|
28
|
+
The base filename without version number or extension.
|
|
29
|
+
ext : str
|
|
30
|
+
The file extension, including the dot (e.g., '.txt').
|
|
31
|
+
version_prefix : str, optional
|
|
32
|
+
The prefix used before the version number. Default is '_v'.
|
|
33
|
+
|
|
34
|
+
Returns
|
|
35
|
+
-------
|
|
36
|
+
Path
|
|
37
|
+
The full path for the next version of the file.
|
|
38
|
+
|
|
39
|
+
Example
|
|
40
|
+
-------
|
|
41
|
+
>>> increment_version('/path/to/dir', 'myfile', '.txt')
|
|
42
|
+
Path('/path/to/dir/myfile_v004.txt')
|
|
43
|
+
|
|
44
|
+
Notes
|
|
45
|
+
-----
|
|
46
|
+
- If no existing versioned files are found, it starts with version 001.
|
|
47
|
+
- The version number is always formatted with at least 3 digits.
|
|
48
|
+
"""
|
|
49
|
+
dirname = Path(dirname)
|
|
50
|
+
|
|
51
|
+
version_pattern = re.compile(
|
|
52
|
+
rf"({re.escape(fname)}{re.escape(version_prefix)})(\d+)({re.escape(ext)})$"
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
glob_pattern = f"{fname}{version_prefix}*{ext}"
|
|
56
|
+
files = list(dirname.glob(glob_pattern))
|
|
57
|
+
|
|
58
|
+
highest_version = 0
|
|
59
|
+
base, suffix = None, None
|
|
60
|
+
|
|
61
|
+
for file in files:
|
|
62
|
+
match = version_pattern.search(file.name)
|
|
63
|
+
if match:
|
|
64
|
+
base, version_str, suffix = match.groups()
|
|
65
|
+
version_num = int(version_str)
|
|
66
|
+
if version_num > highest_version:
|
|
67
|
+
highest_version = version_num
|
|
68
|
+
|
|
69
|
+
if base is None or suffix is None:
|
|
70
|
+
base = f"{fname}{version_prefix}"
|
|
71
|
+
suffix = ext
|
|
72
|
+
highest_version = 0
|
|
73
|
+
|
|
74
|
+
next_version_number = highest_version + 1
|
|
75
|
+
next_version_str = f"{base}{next_version_number:03d}{suffix}"
|
|
76
|
+
|
|
77
|
+
return dirname / next_version_str
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# EOF
|
scitex_path/_mk_spath.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_mk_spath.py
|
|
4
|
+
|
|
5
|
+
"""Save path creation utilities."""
|
|
6
|
+
|
|
7
|
+
import inspect
|
|
8
|
+
import os
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Union
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def mk_spath(sfname: Union[str, Path], makedirs: bool = False) -> Path:
|
|
14
|
+
"""Create a save path based on the calling script's location.
|
|
15
|
+
|
|
16
|
+
Parameters
|
|
17
|
+
----------
|
|
18
|
+
sfname : str or Path
|
|
19
|
+
The name of the file to be saved.
|
|
20
|
+
makedirs : bool, optional
|
|
21
|
+
If True, create the directory structure for the save path.
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
Path
|
|
26
|
+
The full save path for the file.
|
|
27
|
+
|
|
28
|
+
Example
|
|
29
|
+
-------
|
|
30
|
+
>>> spath = mk_spath('output.txt', makedirs=True)
|
|
31
|
+
>>> print(spath)
|
|
32
|
+
Path('/path/to/current/script_out/output.txt')
|
|
33
|
+
"""
|
|
34
|
+
caller_file = inspect.stack()[1].filename
|
|
35
|
+
if "ipython" in caller_file.lower():
|
|
36
|
+
caller_file = f"/tmp/fake-{os.getenv('USER')}.py"
|
|
37
|
+
|
|
38
|
+
fpath = Path(caller_file)
|
|
39
|
+
sdir = fpath.parent / f"{fpath.stem}_out"
|
|
40
|
+
spath = sdir / sfname
|
|
41
|
+
|
|
42
|
+
if makedirs:
|
|
43
|
+
spath.parent.mkdir(parents=True, exist_ok=True)
|
|
44
|
+
|
|
45
|
+
return spath
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# EOF
|
scitex_path/_path.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_path.py
|
|
4
|
+
|
|
5
|
+
"""Path utilities - deprecated, use _this_path.py instead."""
|
|
6
|
+
|
|
7
|
+
from ._this_path import get_this_path, this_path
|
|
8
|
+
|
|
9
|
+
__all__ = ["this_path", "get_this_path"]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# EOF
|
scitex_path/_split.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Timestamp: "2026-01-08 02:00:00 (ywatanabe)"
|
|
3
|
+
# File: /home/ywatanabe/proj/scitex-code/src/scitex/path/_split.py
|
|
4
|
+
|
|
5
|
+
"""Path splitting utilities."""
|
|
6
|
+
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Tuple, Union
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def split(fpath: Union[str, Path]) -> Tuple[Path, str, str]:
|
|
12
|
+
"""Split a file path into directory, filename, and extension.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
fpath : str or Path
|
|
17
|
+
File path to split.
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
tuple of (Path, str, str)
|
|
22
|
+
(directory, filename without extension, extension)
|
|
23
|
+
|
|
24
|
+
Example
|
|
25
|
+
-------
|
|
26
|
+
>>> dirname, fname, ext = split('../data/01/day1/tt8-2.mat')
|
|
27
|
+
>>> print(dirname) # Path('../data/01/day1')
|
|
28
|
+
>>> print(fname) # 'tt8-2'
|
|
29
|
+
>>> print(ext) # '.mat'
|
|
30
|
+
"""
|
|
31
|
+
path = Path(fpath)
|
|
32
|
+
return path.parent, path.stem, path.suffix
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# EOF
|