Skip to content

Commit

Permalink
Handle MyData paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kks32 committed Nov 26, 2024
1 parent 3597cbd commit faee45f
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions dapi/components/files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ class FileInfo:
class FilesComponent(BaseComponent):
"""Component for managing files and directories in DesignSafe."""

def get_uri(self, path: str, system: Optional[str] = None) -> str:
def get_storage_info(self, path: str) -> tuple[str, str]:
"""
Convert a path to a Tapis URI based on specific directory patterns.
Determine storage system and processed path based on path patterns.
Args:
path (str): Path to convert to URI
system (str, optional): Storage system to use (ignored as system is determined by path)
path (str): Input path
Returns:
str: Tapis URI for the given path
tuple: (storage_id, processed_path)
Raises:
ValueError: If no matching directory pattern is found
ValueError: If no matching pattern is found
"""
# Define directory patterns and their corresponding storage systems and username requirements
# Define directory patterns and their corresponding storage systems
directory_patterns = [
("jupyter/MyData", "designsafe.storage.default", True),
("jupyter/mydata", "designsafe.storage.default", True),
Expand All @@ -60,10 +59,10 @@ def get_uri(self, path: str, system: Optional[str] = None) -> str:
# Check standard directory patterns
for pattern, storage, use_username in directory_patterns:
if pattern in path:
path = path.split(pattern, 1)[1].lstrip("/")
input_dir = f"{self.tapis.username}/{path}" if use_username else path
input_uri = f"tapis://{storage}/{input_dir}"
return input_uri.replace(" ", "%20")
processed_path = path.split(pattern, 1)[1].lstrip("/")
if use_username and not processed_path.startswith(self.tapis.username):
processed_path = f"{self.tapis.username}/{processed_path}"
return storage, processed_path

# Check project patterns
project_patterns = [
Expand All @@ -75,21 +74,37 @@ def get_uri(self, path: str, system: Optional[str] = None) -> str:
if pattern in path:
path = path.split(pattern, 1)[1].lstrip("/")
project_id, *rest = path.split("/", 1)
path = rest[0] if rest else ""
remaining_path = rest[0] if rest else ""

# Get project UUID using Tapis
# Get project UUID
try:
resp = self.tapis.get(
f"https://designsafe-ci.org/api/projects/v2/{project_id}"
)
project_uuid = resp.json()["baseProject"]["uuid"]
input_uri = f"tapis://{prefix}{project_uuid}/{path}"
return input_uri.replace(" ", "%20")
return f"{prefix}{project_uuid}", remaining_path
except Exception as e:
raise ValueError(f"Could not resolve project UUID: {str(e)}")

raise ValueError(f"No matching directory pattern found for: {path}")

def get_uri(self, path: str, system: Optional[str] = None) -> str:
"""
Convert a path to a Tapis URI.
Args:
path (str): Path to convert
system (str, optional): Storage system to use (ignored as system is determined by path)
Returns:
str: Tapis URI for the given path
Raises:
ValueError: If path pattern is not recognized
"""
storage_id, processed_path = self.get_storage_info(path)
return f"tapis://{storage_id}/{processed_path}".replace(" ", "%20")

def list(
self, path: str = None, recursive: bool = False, system: str = None
) -> List[FileInfo]:
Expand Down

0 comments on commit faee45f

Please sign in to comment.