Skip to content

Commit

Permalink
Merge pull request #895 from MolSSI/split_attach
Browse files Browse the repository at this point in the history
Split download_view and download_attachment
  • Loading branch information
bennybp authored Feb 14, 2025
2 parents 8f463e8 + 0227f8c commit ca8b72e
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions qcportal/qcportal/dataset_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,38 @@ def delete_attachment(self, file_id: int):

self.fetch_attachments()

def download_attachment(
self,
attachment_id: int,
destination_path: Optional[str] = None,
overwrite: bool = True,
):
"""
Downloads an attachment
If destination path is not given, the file will be placed in the current directory, and the
filename determined by what is stored on the server.
Parameters
----------
attachment_id
ID of the attachment to download. See the `attachments` property
destination_path
Full path to the destination file (including filename)
overwrite
If True, any existing file will be overwritten
"""

attachment_map = {x.id: x for x in self.attachments}
if attachment_id not in attachment_map:
raise ValueError(f"File id {attachment_id} is not a valid attachment for this dataset")

if destination_path is None:
attachment_data = attachment_map[attachment_id]
destination_path = os.path.join(os.getcwd(), attachment_data.file_name)

self._client.download_external_file(attachment_id, destination_path, overwrite=overwrite)

#########################################
# View creation and use
#########################################
Expand Down Expand Up @@ -517,13 +549,9 @@ def download_view(

view_map = {x.id: x for x in self.list_views()}
if view_file_id not in view_map:
raise ValueError(f"File id {view_file_id} is not a valid ID for this dataset")

if destination_path is None:
view_data = view_map[view_file_id]
destination_path = os.path.join(os.getcwd(), view_data.file_name)
raise ValueError(f"File id {view_file_id} is not a valid view for this dataset")

self._client.download_external_file(view_file_id, destination_path, overwrite=overwrite)
self.download_attachment(view_file_id, destination_path, overwrite=overwrite)

def use_view_cache(
self,
Expand Down

0 comments on commit ca8b72e

Please sign in to comment.