Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path fix #58

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion osm/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import logging
import os
import re
import time
import types
from pathlib import Path
Expand Down Expand Up @@ -99,6 +100,32 @@ def compose_down():
print(f"Logs of docker containers are saved at {docker_log}")


def make_uid_path_safe(uid: str) -> str:
"""
Sanitizes a given string to make it safe for use as a file path.

Args:
- uid (str): The original string that needs to be sanitized.

Returns:
- str: A sanitized string safe for use as a file path.
"""
# Define a regex pattern to match unsafe characters for file paths
unsafe_characters_pattern = r'[\/\\:*?"<>|]'

# Replace unsafe characters with an underscore
safe_uid = re.sub(unsafe_characters_pattern, "_", uid)

# Remove leading/trailing whitespace
safe_uid = safe_uid.strip()

# Replace multiple consecutive spaces or underscores with a single underscore
safe_uid = re.sub(r"[\s_]+", "_", safe_uid)

# Return the sanitized UID
return safe_uid


def _setup(args):
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -114,7 +141,7 @@ def _setup(args):
"""
)
args.parser = ["no-op"]
metrics_path = _get_metrics_dir() / f"{args.uid}.json"
metrics_path = _get_metrics_dir() / f"{make_uid_path_safe(args.uid)}.json"
if metrics_path.exists():
raise FileExistsError(metrics_path)
# create logs directory if necessary
Expand Down Expand Up @@ -152,3 +179,10 @@ def flatten_dict(d):
else:
items.append((k, v))
return dict(items)


def camel_to_snake(name: str) -> str:
# Replace capital letters with underscore + lowercase letter
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
# Handle cases where a lowercase is followed by an uppercase letter
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
Loading