Skip to content

Commit 3597cbd

Browse files
committed
Add get uri function to get tapis uri for a file location
1 parent 904074d commit 3597cbd

File tree

1 file changed

+52
-71
lines changed

1 file changed

+52
-71
lines changed

Diff for: dapi/components/files/__init__.py

+52-71
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99

1010

1111
class StorageSystem(Enum):
12-
"""Enumeration of DesignSafe storage systems."""
12+
"""Enumeration of available storage systems."""
1313

1414
MY_DATA = "designsafe.storage.default"
15-
COMMUNITY_DATA = "designsafe.storage.community"
16-
17-
@property
18-
def base_path(self) -> str:
19-
"""Get the base Jupyter path for this storage system."""
20-
return {
21-
StorageSystem.MY_DATA: "jupyter/MyData",
22-
StorageSystem.COMMUNITY_DATA: "jupyter/CommunityData",
23-
}[self]
15+
COMMUNITY = "designsafe.storage.community"
16+
PUBLISHED = "designsafe.storage.published"
17+
FRONTERA = "frontera.storage.default"
18+
LS6 = "ls6.storage.default"
2419

2520

2621
@dataclass
@@ -39,75 +34,61 @@ class FileInfo:
3934
class FilesComponent(BaseComponent):
4035
"""Component for managing files and directories in DesignSafe."""
4136

42-
def _get_project_uuid(self, project_id: str) -> str:
43-
"""Get the UUID for a project given its ID.
44-
45-
Args:
46-
project_id: The project ID
47-
48-
Returns:
49-
The project UUID
50-
51-
Raises:
52-
ValueError: If project not found
37+
def get_uri(self, path: str, system: Optional[str] = None) -> str:
5338
"""
54-
try:
55-
resp = self.tapis.get(
56-
f"https://designsafe-ci.org/api/projects/v2/{project_id}"
57-
)
58-
project_data = resp.json()
59-
return project_data["baseProject"]["uuid"]
60-
except Exception as e:
61-
raise ValueError(f"Error getting project UUID for {project_id}: {str(e)}")
62-
63-
def get_uri(self, path: str) -> str:
64-
"""Convert a local or Jupyter path to a Tapis URI.
39+
Convert a path to a Tapis URI based on specific directory patterns.
6540
6641
Args:
67-
path: Local filesystem or Jupyter path
42+
path (str): Path to convert to URI
43+
system (str, optional): Storage system to use (ignored as system is determined by path)
6844
6945
Returns:
70-
Tapis URI for the path
71-
72-
Examples:
73-
>>> ds.files.get_uri("jupyter/MyData/test.txt")
74-
'tapis://designsafe.storage.default/username/test.txt'
75-
76-
>>> ds.files.get_uri("jupyter/CommunityData/test.txt")
77-
'tapis://designsafe.storage.community/test.txt'
46+
str: Tapis URI for the given path
7847
79-
>>> ds.files.get_uri("jupyter/MyProjects/PRJ-1234/test.txt")
80-
'tapis://project-uuid/test.txt'
48+
Raises:
49+
ValueError: If no matching directory pattern is found
8150
"""
82-
path = str(path) # Convert Path objects to string
83-
84-
# Handle MyData paths
85-
if "MyData" in path or "mydata" in path:
86-
# Extract the relative path after MyData
87-
rel_path = path.split("MyData/")[-1]
88-
return f"tapis://{StorageSystem.MY_DATA.value}/{self.tapis.username}/{rel_path}"
89-
90-
# Handle CommunityData paths
91-
if "CommunityData" in path:
92-
rel_path = path.split("CommunityData/")[-1]
93-
return f"tapis://{StorageSystem.COMMUNITY_DATA.value}/{rel_path}"
94-
95-
# Handle Project paths
96-
if "MyProjects" in path or "projects" in path:
97-
# Extract project ID and relative path
98-
parts = path.split("/")
99-
for i, part in enumerate(parts):
100-
if part in ("MyProjects", "projects"):
101-
project_id = parts[i + 1]
102-
rel_path = "/".join(parts[i + 2 :])
103-
break
104-
else:
105-
raise ValueError("Could not parse project path")
106-
107-
project_uuid = self._get_project_uuid(project_id)
108-
return f"tapis://project-{project_uuid}/{rel_path}"
51+
# Define directory patterns and their corresponding storage systems and username requirements
52+
directory_patterns = [
53+
("jupyter/MyData", "designsafe.storage.default", True),
54+
("jupyter/mydata", "designsafe.storage.default", True),
55+
("jupyter/CommunityData", "designsafe.storage.community", False),
56+
("/MyData", "designsafe.storage.default", True),
57+
("/mydata", "designsafe.storage.default", True),
58+
]
59+
60+
# Check standard directory patterns
61+
for pattern, storage, use_username in directory_patterns:
62+
if pattern in path:
63+
path = path.split(pattern, 1)[1].lstrip("/")
64+
input_dir = f"{self.tapis.username}/{path}" if use_username else path
65+
input_uri = f"tapis://{storage}/{input_dir}"
66+
return input_uri.replace(" ", "%20")
67+
68+
# Check project patterns
69+
project_patterns = [
70+
("jupyter/MyProjects", "project-"),
71+
("jupyter/projects", "project-"),
72+
]
73+
74+
for pattern, prefix in project_patterns:
75+
if pattern in path:
76+
path = path.split(pattern, 1)[1].lstrip("/")
77+
project_id, *rest = path.split("/", 1)
78+
path = rest[0] if rest else ""
79+
80+
# Get project UUID using Tapis
81+
try:
82+
resp = self.tapis.get(
83+
f"https://designsafe-ci.org/api/projects/v2/{project_id}"
84+
)
85+
project_uuid = resp.json()["baseProject"]["uuid"]
86+
input_uri = f"tapis://{prefix}{project_uuid}/{path}"
87+
return input_uri.replace(" ", "%20")
88+
except Exception as e:
89+
raise ValueError(f"Could not resolve project UUID: {str(e)}")
10990

110-
raise ValueError(f"Could not determine storage system for path: {path}")
91+
raise ValueError(f"No matching directory pattern found for: {path}")
11192

11293
def list(
11394
self, path: str = None, recursive: bool = False, system: str = None

0 commit comments

Comments
 (0)