Files
clip-annotator/src/clip_annotator/filesystem.py

36 lines
1.0 KiB
Python
Raw Normal View History

import os
_DEFAULT_ENDPOINT = "https://os.zhdk.cloud.switch.ch"
def make_fs(storage: str):
"""Return an S3FileSystem for storage='s3', or None for local."""
if storage != "s3":
return None
import s3fs
return s3fs.S3FileSystem(
key=os.environ["S3_ACCESS_KEY"],
secret=os.environ["S3_SECRET_ACCESS_KEY"],
client_kwargs={
"endpoint_url": os.environ.get("S3_ENDPOINT_URL", _DEFAULT_ENDPOINT)
},
)
def fsjoin(base, *parts: str) -> str:
"""Join path segments with forward slashes (works for both local and S3)."""
return "/".join([str(base).rstrip("/"), *[str(p).strip("/") for p in parts if p]])
def fsstem(path) -> str:
"""Filename stem (no extension) for local Path or S3 string."""
name = str(path).replace("\\", "/").split("/")[-1]
return name.rsplit(".", 1)[0] if "." in name else name
def fsname(path) -> str:
"""Filename component (with extension) for local Path or S3 string."""
return str(path).replace("\\", "/").split("/")[-1]