Skip to content

Commit

Permalink
Added structured_data.StructuredDataSet.upload_files_located.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Jan 18, 2024
1 parent 9dfb506 commit 51ca5e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions dcicutils/structured_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
from functools import lru_cache
import glob
import json
from jsonschema import Draft7Validator as SchemaValidator
import os
Expand Down Expand Up @@ -111,6 +112,14 @@ def upload_files(self) -> List[str]:
result.append({"type": type_name, "file": file_name})
return result

def upload_files_located(self,
location: Union[str, Optional[List[str]]] = None, recursive: bool = False) -> List[str]:
upload_files = copy.deepcopy(self.upload_files)
for upload_file in upload_files:
if file_path := _search_for_file(upload_file["file"], location=location, recursive=recursive):
upload_file["path"] = file_path
return upload_files

def _load_file(self, file: str) -> None:
# Returns a dictionary where each property is the name (i.e. the type) of the data,
# and the value is array of dictionaries for the data itself. Handle these kinds of files:
Expand Down Expand Up @@ -641,3 +650,23 @@ def _split_dotted_string(value: str):

def _split_array_string(value: str, unique: bool = False):
return split_string(value, ARRAY_VALUE_DELIMITER_CHAR, ARRAY_VALUE_DELIMITER_ESCAPE_CHAR, unique=unique)


def _search_for_file(file: str,
location: Union[str, Optional[List[str]]] = None, recursive: bool = False) -> Optional[str]:
if isinstance(file, str) or not file:
if not location:
location = "."
if location:
if isinstance(location, str):
location = [location]
if isinstance(location, list):
for directory in location:
if isinstance(directory, str) and os.path.exists(os.path.join(directory, file)):
return os.path.normpath(os.path.join(directory, file))
if recursive:
if not file.startswith("**/"):
file = "**/" + file
files = glob.glob(file, recursive=recursive)
if files:
return files[0]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.7.0.1b28" # TODO: To become 8.7.1
version = "8.7.0.1b29" # TODO: To become 8.7.1
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 51ca5e7

Please sign in to comment.