*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit b26adb48 authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Return a tuple of IDs rather than just the filename after upload

* knowing the container ID helps with subsequent operations if the
  container didn't exist before
* also, allow the recid in InvenioRDM to be omitted (in which case a new
  draft will be created)
parent 2bfff439
No related branches found
No related tags found
No related merge requests found
......@@ -30,13 +30,15 @@ class BaseWrapper(abc.ABC):
@abc.abstractmethod
def upload(
self, file_path: str, container_id: Optional[str | int], name: Optional[str]
) -> Optional[int | str]:
) -> Tuple[Optional[int | str], Optional[int | str]]:
"""Upload the local file under ``file_path`` to the repository.
If the ``container_id`` is specified, then the file will be uploaded to this
container.
Otherwise, a new container will be created.
A tuple with the container ID and file name/ID will be returned.
:param file_path: The local path of the file to upload.
:param container_id: The ID of the container to which to add the uploaded file.
:param name: Override for the name of the file after upload.
......
......@@ -201,10 +201,11 @@ class DBRepo(BaseWrapper):
file_path: str,
database_id: Optional[int] = None,
table_name: Optional[str] = None,
) -> Optional[int]:
) -> Tuple[Optional[int], Optional[int]]:
"""Uploads a table to the specified database.
Returns the ID of the created table.
If no database is specified, a new one will be created.
Returns the IDs of the database and the created table.
"""
sep = self._guess_separator_character(file_path)
analysis = self.client.analyse_datatypes(file_path, separator=sep, upload=True)
......@@ -224,7 +225,7 @@ class DBRepo(BaseWrapper):
for line_data in self._file_to_data_dicts(file_path, separator=sep):
self._create_table_data(database_id, table_id, line_data)
return table_id
return (database_id, table_id)
def download(self, database_id: str, table: str | int) -> Optional[str]:
"""Downloads specified table from the database.
......
......@@ -3,6 +3,7 @@
# Copyright (C) 2024 TU Wien.
#
import os.path
import pathlib
import re
import tempfile
......@@ -105,8 +106,21 @@ class InvenioRDM(BaseWrapper):
print(f"Draft update failed: {e}")
return False
def upload(self, file_path: str, record_pid: str, file_name: str) -> Optional[str]:
"""Uploads a file to the specified draft."""
def upload(
self,
file_path: str,
record_pid: Optional[str] = None,
file_name: Optional[str] = None,
) -> Optional[str]:
"""Uploads a file to the specified draft.
If no draft is specified, a new one will be created.
Returns the recid of the draft and name of the uploaded file.
"""
file_name = file_name or os.path.basename(file_path)
if not record_pid:
record_pid = self.create({"metadata": {"title": file_name}})
draft = self._resolve_draft(record_pid)
try:
......@@ -119,7 +133,7 @@ class InvenioRDM(BaseWrapper):
f.set_contents(OutgoingStream(data=stream))
f.commit()
return file.data["entries"][0]["key"]
return (record_pid, file.data["entries"][0]["key"])
except Exception as e:
print(f"File upload failed: {e}")
......@@ -138,7 +152,10 @@ class InvenioRDM(BaseWrapper):
return False
def download(self, record_pid: str, file_name: str) -> Optional[str]:
"""Downloads given file from the specified (published) record."""
"""Downloads given file from the specified (published) record.
Returns the path to the downloaded file.
"""
record = self.client.records(self._normalize_pid(record_pid))
try:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment