Skip to content

Commit 38ad910

Browse files
committed
Streamlined logic for 'secure_path', and added logic to preserve trailing underscores in file path parts
1 parent 24d6628 commit 38ad910

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/murfey/util/__init__.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,21 @@ 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):
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+
continue # Skip subsequent conditions and move to next part
35+
if keep_spaces:
3336
if " " in part:
3437
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-
]
38+
continue # Skip subsequent conditions and move to next part
39+
if part.endswith("_"):
40+
# Preserve all trailing underscores
41+
num_underscores = len(part) - len(part.rstrip("_"))
42+
secured_parts.append(secure_filename(part) + (num_underscores * "_"))
43+
else:
44+
secured_parts.append(secure_filename(part))
4845
return Path("/".join(secured_parts))
4946

5047

0 commit comments

Comments
 (0)