Skip to content

Commit c6c286e

Browse files
authored
Preserve trailing underscores (#676)
* Streamlined logic for 'secure_path', and added logic to preserve trailing underscores in file path parts * Added unit tests for 'secure_path'
1 parent 24d6628 commit c6c286e

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

src/murfey/util/__init__.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,18 @@ def sanitise_nonpath(in_string: str) -> str:
2727

2828

2929
def secure_path(in_path: Path, keep_spaces: bool = False) -> Path:
30-
if keep_spaces:
31-
secured_parts = []
32-
for p, part in enumerate(in_path.parts):
33-
if " " in part:
34-
secured_parts.append(part)
35-
elif ":" in part and not p:
36-
secured_parts.append(secure_filename(part) + ":")
37-
else:
38-
secured_parts.append(secure_filename(part))
39-
else:
40-
secured_parts = [
41-
(
42-
secure_filename(part) + ":"
43-
if p == 0 and ":" in part
44-
else secure_filename(part)
45-
)
46-
for p, part in enumerate(in_path.parts)
47-
]
30+
secured_parts = []
31+
for p, part in enumerate(in_path.parts):
32+
if p == 0 and ":" in part:
33+
secured_parts.append(secure_filename(part) + ":")
34+
elif " " in part and keep_spaces:
35+
secured_parts.append(part)
36+
elif part.endswith("_"):
37+
# Preserve all trailing underscores
38+
num_underscores = len(part) - len(part.rstrip("_"))
39+
secured_parts.append(secure_filename(part) + (num_underscores * "_"))
40+
else:
41+
secured_parts.append(secure_filename(part))
4842
return Path("/".join(secured_parts))
4943

5044

tests/util/test_init.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Test module for the functions located in murfey.util.__init__
3+
"""
4+
5+
from pathlib import Path
6+
7+
import pytest
8+
9+
from murfey.util import secure_path
10+
11+
secure_path_test_matrix = (
12+
# Keep spaces? | Input path | Expected output
13+
# Tomo workflow examples
14+
(
15+
False,
16+
"D:/User_22220202_090000_cm40000-1/Position_1_1_001_0.00_22220202_090000_EER.eer",
17+
"D:/User_22220202_090000_cm40000-1/Position_1_1_001_0.00_22220202_090000_EER.eer",
18+
),
19+
(
20+
False,
21+
"D:/User_22220202_090000_cm40000-1_/Position_1_1_001_0.00_22220202_090000_EER.eer",
22+
"D:/User_22220202_090000_cm40000-1_/Position_1_1_001_0.00_22220202_090000_EER.eer",
23+
),
24+
# CLEM workflow examples
25+
(
26+
True,
27+
"D:/Session/cm40000-1/images/My Sample/TileScan 1/Position 1--Stage00--Z00--C00.tif",
28+
"D:/Session/cm40000-1/images/My Sample/TileScan 1/Position 1--Stage00--Z00--C00.tif",
29+
),
30+
(
31+
True,
32+
"D:/Session/cm40000-1/images/My Sample_/TileScan 1/Position 1--Stage00--Z00--C00.tif",
33+
"D:/Session/cm40000-1/images/My Sample_/TileScan 1/Position 1--Stage00--Z00--C00.tif",
34+
),
35+
(
36+
True,
37+
"D:/Session/cm40000-1/images/My_Sample_/TileScan 1/Position 1--Stage00--Z00--C00.tif",
38+
"D:/Session/cm40000-1/images/My_Sample_/TileScan 1/Position 1--Stage00--Z00--C00.tif",
39+
),
40+
# Go wild
41+
(
42+
True,
43+
"D:/some path/to_/this/repo!/my_file.txt",
44+
"D:/some path/to_/this/repo/my_file.txt",
45+
),
46+
(
47+
True,
48+
"D:/some path__/to_/this/repo/my file.txt",
49+
"D:/some path__/to_/this/repo/my file.txt",
50+
),
51+
(
52+
False,
53+
"D:/some path__/to_/this/repo/my file.txt",
54+
"D:/some_path__/to_/this/repo/my_file.txt",
55+
),
56+
)
57+
58+
59+
@pytest.mark.parametrize("test_params", secure_path_test_matrix)
60+
def test_secure_path(test_params: tuple[bool, str, str]):
61+
# Unpack test params
62+
keep_spaces, input_path, expected_output = test_params
63+
assert secure_path(Path(input_path), keep_spaces) == Path(expected_output)

0 commit comments

Comments
 (0)