Skip to content

Commit

Permalink
add dts flag to bulk_specification endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
briehl committed Nov 27, 2024
1 parent bd55791 commit ccfbc7f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 19 additions & 11 deletions staging_service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
parse_csv,
parse_excel,
parse_tsv,
parse_dts_manifest,
parse_dts_manifest
)
from .JGIMetadata import read_metadata_for
from .metadata import add_upa, dir_info, similar, some_metadata
Expand All @@ -47,8 +47,7 @@
_IMPSPEC_FILE_TO_PARSER = {
CSV: parse_csv,
TSV: parse_tsv,
EXCEL: parse_excel,
JSON: parse_dts_manifest,
EXCEL: parse_excel
}

_IMPSPEC_FILE_TO_WRITER = {
Expand Down Expand Up @@ -107,6 +106,15 @@ def _file_type_resolver(path: PathPy) -> FileTypeResolution:
ext = path.name
return FileTypeResolution(unsupported_type=ext)

def _extract_file_paths(params: dict[str, list|None], key: str, username: str) -> dict[PathPy, PathPy]:
files = params.get(key, [])
files = files[0].split(",") if files else []
files = [f.strip() for f in files if f.strip()]
paths = {}
for f in files:
p = Path.validate_path(username, f)
paths[PathPy(p.full_path)] = PathPy(p.user_path)
return paths

@routes.get("/bulk_specification/{query:.*}")
async def bulk_specification(request: web.Request) -> web.json_response:
Expand All @@ -122,17 +130,17 @@ async def bulk_specification(request: web.Request) -> web.json_response:
Data Transfer Service manifest.
"""
username = await authorize_request(request)
files = parse_qs(request.query_string).get("files", [])
files = files[0].split(",") if files else []
files = [f.strip() for f in files if f.strip()]
paths = {}
for f in files:
p = Path.validate_path(username, f)
paths[PathPy(p.full_path)] = PathPy(p.user_path)
params = parse_qs(request.query_string)
paths = _extract_file_paths(params, "files", username)
as_dts = params.get("dts") == "1"

# list(dict) returns a list of the dict keys in insertion order (py3.7+)
file_type_resolver = _file_type_resolver
if as_dts:
file_type_resolver = lambda: FileTypeResolution(parser=parse_dts_manifest) # noqa: E731
res = parse_import_specifications(
tuple(list(paths)),
_file_type_resolver,
file_type_resolver,
lambda e: logging.error("Unexpected error while parsing import specs", exc_info=e),
)
if res.results:
Expand Down
2 changes: 2 additions & 0 deletions staging_service/import_specifications/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def parse_import_specifications(
file_type_resolver - a callable that when given a file path, returns the type of the file or
a parser for the file.
log_error - callable for logging an exception.
dts_paths - JSON file paths that are specifically for the DTS importer and should not be
resolved any other way
"""
if not paths:
return ParseResults(errors=tuple([Error(ErrorType.NO_FILES_PROVIDED)]))
Expand Down

0 comments on commit ccfbc7f

Please sign in to comment.